PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

Missing try{} block not reported as a problem

Open bytepl opened this issue 3 years ago • 1 comments

Steps to reproduce

Write-Output "Test"
catch {
    Continue
}

save it as a file, then run Invoke-ScriptAnalyzer against it.

Expected behavior

An error should be displayed, saying that there's no 'try' block, just 'catch'.

Actual behavior

No error is returned and the script throws an error when executed.

catch : The term 'catch' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a p
 again.
At C:\Users\mchojnac\Desktop\test.ps1:2 char:1
+ catch {
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (catch:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.1023
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.1023
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.1

bytepl avatar Jul 22 '21 10:07 bytepl

This is a fairly strange one — I'm not sure how many people are accidentally using just a catch block. And by itself, catch is a valid command name.

With that said, it might be useful to warn about commands that could also be keywords. That could also solve https://github.com/PowerShell/PSScriptAnalyzer/issues/1571.

However, given we don't know there's much demand for this and that we're not working on feature requests like this currently, I'll mark this issue as up-for-grabs.

rjmholt avatar Aug 03 '21 22:08 rjmholt