-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fix PSScriptAnalyzer warnings #4205
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
Fix PSScriptAnalyzer warnings #4205
Conversation
… Where-Object (?), Select-Object and a few others. Fixed some PSUseDeclaredVarsMoreThanAssignment warnings (one must be careful with them as some of those warnings are false positives)
…omparison is now correct and also removed unnecessary initialisation that was not needed for the test but made it difficult to know what was going. Enhanced the test. Fixed one typo in Dynamicparameters.Tests.ps1 that got introduced during the last refactoring and made 2 tests fail.
|
@bergmeister, |
iSazonov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As requested in #3791 we should add meta test with scriptanalyzer.
| $IsFedora = $LinuxInfo.ID -match 'fedora' -and $LinuxInfo.VERSION_ID -ge 24 | ||
| $IsOpenSUSE = $LinuxInfo.ID -match 'opensuse' | ||
| $IsOpenSUSE13 = $IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '13' | ||
| ${IsOpenSUSE42.1} = $IsOpenSUSE -and $LinuxInfo.VERSION_ID -match '42.1' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variables is unused - so Ok to remove.
build.psm1
Outdated
| if ($count -eq 0) | ||
| { | ||
| sleep 1 | ||
| Start-Sleep 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a parameter name.
demos/Azure/Azure-Demo.ps1
Outdated
| ### Install-Package -Name AzureRM.NetCore.Preview -Source https://www.powershellgallery.com/api/v2 -ProviderName NuGet -ExcludeVersion -Destination <Folder you want this to be installed> | ||
| ### | ||
| ### Ensure $env:PSModulePath is updated with the location you used to install. | ||
| ### Ensure $env:PSMODULEPATH is updated with the location you used to install. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should use "PSModulePath" - see PowerShell-Committee conclusion #3227 (comment)
| }} | ||
| if (-not $AsJob -and $job -ne $null) | ||
| if ($null -ne -not $AsJob -and $job) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Corrupted logic. Please see operator precedence
| $xmlWriter = SaveCDXMLHeader $xmlWriter $Uri $singletonName $singletonName $CmdletAdapter | ||
|
|
||
| if ($associatedEntityType.BaseType -eq $null -and $associatedEntityType.BaseTypeStr -ne $null -and $associatedEntityType.BaseTypeStr -ne '') | ||
| if ($null -eq $associatedEntityType.BaseTyp -and $null -ne $associatedEntityType.BaseTypeStr -and $associatedEntityType.BaseTypeStr -ne '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lost char: $associatedEntityType.BaseTyp
|
|
||
| It "Should be able to use the alias" { | ||
| { $dirObject | select } | Should Not Throw | ||
| { $dirObject | Select-Object} | Should Not Throw |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please revert the change.
| $gpp = Get-PackageProvider | ||
|
|
||
| $gpp | ?{ $_.name -eq "NuGet" } | should not BeNullOrEmpty | ||
| $gpp | Where-Object{ $_.name -eq "NuGet" } | should not BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object.
| $gpp | Where-Object{ $_.name -eq "NuGet" } | should not BeNullOrEmpty | ||
|
|
||
| $gpp | ?{ $_.name -eq "PowerShellGet" } | should not BeNullOrEmpty | ||
| $gpp | Where-Object{ $_.name -eq "PowerShellGet" } | should not BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object.
test/tools/OpenCover/OpenCover.psm1
Outdated
| $c1 = $r1.Assembly.ClassCoverage | ?{$_.ClassName -eq $Class } | ||
| $c2 = $r2.Assembly.ClassCoverage | ?{$_.ClassName -eq $Class } | ||
| $c1 = $r1.Assembly.ClassCoverage | Where-Object{$_.ClassName -eq $Class } | ||
| $c2 = $r2.Assembly.ClassCoverage | Where-Object{$_.ClassName -eq $Class } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object in both lines.
test/tools/OpenCover/OpenCover.psm1
Outdated
| } | ||
| $CoverageData.PSTypeNames.Insert(0,"OpenCover.CoverageData") | ||
| Add-Member -InputObject $CoverageData -MemberType ScriptMethod -Name GetClassCoverage -Value { param ( $name ) $this.assembly.classcoverage | ?{$_.classname -match $name } } | ||
| Add-Member -InputObject $CoverageData -MemberType ScriptMethod -Name GetClassCoverage -Value { param ( $name ) $this.assembly.classcoverage | Where-Object{$_.classname -match $name } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object.
…ludes spaces after Where-Object/Select-Object, 4 lines being reverted due to accidental changes and minor test refactoring. The test refactoring idea was basically to change '$null -eq $something | Should Be $true' to '$something | Should BeNullOrEmpty'
|
Thanks @iSazonov for taking such a detailed look and spotting the accidental mistakes that I made when going through the hundreds of refactorings. I have followed all your suggestions and submitted the fix as you can see above. The total number of warnings seems to have gone down by about 350. |
| td24.Members.Add("IPV4Address", | ||
| new ScriptPropertyData(@"IPV4Address", GetScriptBlock(@"$iphost = [System.Net.Dns]::GetHostEntry($this.address) | ||
| $iphost.AddressList | Where-Object{ $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork } | Select-Object -first 1"), null)); | ||
| $iphost.AddressList | Where-Object{ $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork } | Select-Object -first 1"), null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove extra space before $_ and add space after Where-Object and before {.
The same typo is below.
|
|
||
| $newacl = get-acl $directory | ||
| $newrule = $newacl.Access | Where-Object{ $accessrule.FileSystemRights -eq $_.FileSystemRights -and $accessrule.AccessControlType -eq $_.AccessControlType -and $accessrule.IdentityReference -eq $_.IdentityReference } | ||
| $newrule = $newacl.Access | Where-Object{ $accessrule.FileSystemRights -eq $_.FileSystemRights -and $accessrule.AccessControlType -eq $_.AccessControlType -and $accessrule.IdentityReference -eq $_.IdentityReference } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object and remove extra space before $accessrule.
|
|
||
| It "Temporary module should be automatically removed after runspace is closed" -Skip:$skipTest { | ||
| ($null -eq (Get-Module | Where-Object { $_.Path -eq $module.Path })) | Should Be $true | ||
| (Get-Module | Where-Object { $_.Path -eq $module.Path }) | Should BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation.
| $gpp = Get-PackageProvider | ||
|
|
||
| $gpp | Where-Object{ $_.name -eq "NuGet" } | should not BeNullOrEmpty | ||
| $gpp | Where-Object{ $_.name -eq "NuGet" } | should not BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove extra space.
| $gpp | Where-Object{ $_.name -eq "NuGet" } | should not BeNullOrEmpty | ||
|
|
||
| $gpp | Where-Object{ $_.name -eq "PowerShellGet" } | should not BeNullOrEmpty | ||
| $gpp | Where-Object{ $_.name -eq "PowerShellGet" } | should not BeNullOrEmpty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove extra space.
test/tools/OpenCover/OpenCover.psm1
Outdated
| } | ||
| $CoverageData.PSTypeNames.Insert(0,"OpenCover.CoverageData") | ||
| Add-Member -InputObject $CoverageData -MemberType ScriptMethod -Name GetClassCoverage -Value { param ( $name ) $this.assembly.classcoverage | Where-Object{$_.classname -match $name } } | ||
| Add-Member -InputObject $CoverageData -MemberType ScriptMethod -Name GetClassCoverage -Value { param ( $name ) $this.assembly.classcoverage | Where-Object{ $_.classname -match $name } } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add space after Where-Object.
| $xmlWriter = SaveCDXMLHeader $xmlWriter $Uri $singletonName $singletonName $CmdletAdapter | ||
|
|
||
| if ($null -eq $associatedEntityType.BaseTyp -and $null -ne $associatedEntityType.BaseTypeStr -and $associatedEntityType.BaseTypeStr -ne '') | ||
| if ($null -eq $associatedEntityType.BaseType -and $null -ne $associatedEntityType.BaseTypeStr -and $associatedEntityType.BaseTypeStr -ne '') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anmenaga - this looks like a bug that should get fixed in Windows PowerShell.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lzybkr : I'm not sure what you mean but the missing 'e' on 'BaseType' was an accidental deletion that happened in the first commit that I corrected
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see - this is a good example of why I'm not a fan of mixing minor changes in one big commit, because I do sometimes review commits by themselves instead of the full PR.
|
I'm seeing different sorts of changes in a single commit - and with this many changes, it would be easier to review and safer to keep each refactoring as a single commit. |
|
+1 for multiple commits. Maybe:
|
…econd pull request review
|
@iSazonov I have fixed your requested changes about the fine-tuning of the spacing. When I started out doing that, I initially wanted to fix only most PSPossibleIncorrectComparisonWithNull violations but then I came across lots of other things to be improved like e.g. the aliases. Because the contribution guide here recommended to have just one commit per change, I thought I'd try to put it all into one commit for the pull request. I am not sure how one would change the commit history now that the commits are already pushed to the remote. Sorry, maybe I should have rather checked with you before. |
|
You would rebase and split - see https://stackoverflow.com/questions/6217156/break-a-previous-commit-into-multiple-commits |
|
@iSazonov OK, thanks. I did not feel confident/adventurous enough to rebase commits that are already pushed to the remote, so I have created a new branch, pulled in my commit changes and made then the 4 separate commits as requested by you. I have created the new PR #4213 for that, so feel free to close this one. Sorry, if this PR turned out to be messier than initially anticipated. I hope you are satisfied with this cleaner version now and I could try to help improve your codebase a bit. |
…thNull. Essentially, $null has to be on the left hand side when using it for comparison. A Test in ParameterBinding.Tests.ps1 needed adapting as this test used to rely on the wrong null comparison It was also suggested in the initial PR PowerShell#4205 to replace a subset of tests of kind '($object -eq $null) | Should Be $true' with '($null -eq $object) | Should BeNullOrEmpty'
To help fixing #3791
Fixes a few hundred warnings of type PSPossibleIncorrectComparisonWithNull, PSAvoidUsingCmdletAliases (mostly for ForEach-Object, Where-Object, Select-Object) and some PSUseDeclaredVarsMoreThanAssignment warnings.
As part of this, it was also found that a test in ParameterBindingTests needed improvement because since the null comparison is now correct, the test failed initially.