PSScriptAnalyzer icon indicating copy to clipboard operation
PSScriptAnalyzer copied to clipboard

The triggering of the PSUseOutputTypeCorrectly rule does not consider the presence of the redirection to $null

Open LaurentDardenne opened this issue 4 years ago • 2 comments

Steps to reproduce

$s=@'
  Function Test{
    [CmdletBinding()]
    param()
        [System.Windows.Forms.MessageBox]::Show('Message', 'text',0, 16) > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s

Expected behavior

No error

Actual behavior

RuleName                            Severity     ScriptName Line  Message
--------                            --------     ---------- ----  -------
PSUseOutputTypeCorrectly            Information             4     The cmdlet 'Test' returns an object of type
                                                                  'System.Windows.Forms.DialogResult' but this type is
                                                                  not declared in the OutputType attribute.

Environment data

> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.18362.752
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.18362.752}
BuildVersion                   10.0.18362.752
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.0
1.18.3
1.18.1
1.18.0
1.17.1

Other tests with a type shortcut and a full type name, but with a property name has a different case

$s=@'
  Function Test{
    [CmdletBinding()]
    param()
        [datetime]::Now > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s
$s=@'
  Function Test{
    [CmdletBinding()]
    param()
        [datetime]::now > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s
$s=@'
  Function Test{
    [CmdletBinding()]
    param()
       [datetime]::FromFileTime(1) > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s

$s=@'
  Function Test{
    [CmdletBinding()]
    param()
        [System.Datetime]::now > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s
$s=@'
  Function Test{
    [CmdletBinding()]
    param()
        [System.Datetime]::Now > $null
  }
'@
Invoke-ScriptAnalyzer -ScriptDefinition $s

The code of the last two examples is identical except for the case of the property. The triggering of the PSUseOutputTypeCorrectly rule seems to be case sensitive.

LaurentDardenne avatar May 14 '20 17:05 LaurentDardenne

@LaurentDardenne Not all examples reproduce for me, like for example the first one but some do. I see this as low priority and have therefore marked it as up for grabs

bergmeister avatar May 14 '20 17:05 bergmeister

@bergmeister related to this, simply redirecting to a file, a common operation which does not actually produce an output, triggers the info as a false positive

image

Workaround: Use Out-File instead of >

JustinGrote avatar May 25 '21 20:05 JustinGrote