-
Notifications
You must be signed in to change notification settings - Fork 392
false variable is assigned but never used recommendation #1354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'm seeing the same thing, but in my case it is in the Begin section of Foreach-Object. Shows the message "The variable 'Test' is assigned but never used." on the first part where I assign $Test. |
Also seeing this issue with variable set in Foreach-Object -Begin scriptblock file content 0..5 | ForEach-Object -Begin { $n = 100 } -Process { $n++ } ❯ Invoke-ScriptAnalyzer . RuleName : PSUseDeclaredVarsMoreThanAssignments
Severity : Warning
Line : 1
Column : 32
Message : The variable 'n' is assigned but never used. ❯ $PSVersionTable Name Value
---- -----
PSVersion 7.0.0
PSEdition Core
GitCommitId 7.0.0
OS Darwin 19.3.0 Darwin Kernel Version 19.3.0: Thu Jan 9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0 ❯ Get-Module PSScriptAnalyzer Name : PSScriptAnalyzer
Path : /Users/jason.chester/.local/share/powershell/Modules/PSScriptAnalyzer/1.18.3/PSScriptAnalyzer.psm1
Description : PSScriptAnalyzer provides script analysis and checks for potential code defects in the scripts by applying a group of built-in or customized rules on the
scripts being analyzed.
Guid : d6245802-193d-4068-a631-8863a4342a18
Version : 1.18.3
ModuleBase : /Users/jason.chester/.local/share/powershell/Modules/PSScriptAnalyzer/1.18.3
ModuleType : Script
PrivateData : {PSData}
AccessMode : ReadWrite
ExportedAliases : {}
ExportedCmdlets : {[Get-ScriptAnalyzerRule, Get-ScriptAnalyzerRule], [Invoke-Formatter, Invoke-Formatter], [Invoke-ScriptAnalyzer, Invoke-ScriptAnalyzer]}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules : {Microsoft.Windows.PowerShell.ScriptAnalyzer} |
@FireInWinter @jasonchester Your use cases is already reported in issues #1031 |
@bergmeister If it is limited to one script block, I would say the rule is fairly useless and should be removed until a solution is found. I see so many false positives on this rule, that it makes using PSScriptAnalyzer annoying to use. Is there at least an easy way to disable the rule without editing files in the mod? If I could set something up in my profile to ignore the rule that would at least make PSScriptAnalyzer useful until this is solved. |
@FireInWinter We've had this discussion over and over again. Yes, there are some false positives but the rule is still very useful in flagging unused variables. It's just up to you to suppress when you know it is a false positive. If you do not want to keep up with that, use the @{
ExcludeRules = @(
'PSAvoidUsingCmdletAliases'
)
} |
How would I disable the rule for some specific variables / code sections in a PS script that I know trigger a false positive, rather than disabling the rule globally? |
@sba923 The Readme has a section describing how to suppress them for individual functions or scripts: https://github.com/powershell/psscriptanalyzer#suppressing-rules function foo {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', 'unused',
Justification = 'Reason why it is a false positive')]
param()
$unused = $null
} |
Thanks a bunch for sharing this! |
How do you suppress it in a |
@joeskeen You could put a Param() at the top of the Pester file and put the suppression attributes on top of that. I know, not great terms of scoping, but it works at least |
Duplicate of #1163 |
Issue Type: Bug
I'm getting "The variable 'results' is assigned but never used.", but I'm actually using the variable. (recommendation appears on line with "$results = $content ..."
[for my reference - line 304 in autopilot.psm1]
Extension version: 2019.9.0
VS Code version: Code 1.39.2 (6ab598523be7a800d7f3eb4d92d7ab9a66069390, 2019-10-15T15:35:18.241Z)
OS version: Windows_NT x64 10.0.17763
The text was updated successfully, but these errors were encountered: