Skip to content

AvoidDefaultValueForMandatoryParameter : The rule is not be triggered when the value of the parameter 'Mandatory' is an integer #877

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

Closed
LaurentDardenne opened this issue Feb 7, 2018 · 2 comments · Fixed by #969

Comments

@LaurentDardenne
Copy link

If, for a parameter attribute, the value of the parameter 'Mandatory' is an integer, the rule will not be triggered:

Invoke-ScriptAnalyzer -ScriptDefinition @'
function Test3
{
    [CmdletBinding()]
    [OutputType([String])]
    param(
        # this one has default value
        [Parameter(Mandatory=1)]
        $Param31="String",

        # this parameter has default value but it is not mandatory
        [Parameter(Mandatory=0)]
        $Param32='Test'
    )
    $Param31
    $Param31 = "test"
}
'@

Even if this writing is not a good practice since the runtime allows it, the rule should be triggered.
Unless to create a rule to handle this type of writing :-)

(Get-Module -Name PSScriptAnalyzer).version.tostring()
#1.16.1

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14409.1012
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1.14409.1012}
BuildVersion                   10.0.14409.1012
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
@bergmeister
Copy link
Collaborator

Good spot. There are many scripts that the PowerShell language parser would accept although it will blow up at runtime. Adding extra validation in PSSA makes sense in my opinion. I think the special case that you pointed out is rather rare but I'm sure PSSA would accept a PR with this improvement and I would be happy to help/assist you as far as I can. In general I'm happy to add new features/improvements to PSSA but I think there are bigger fish to fry at the moment.

@LaurentDardenne
Copy link
Author

The method already exists, GetValue, which manages the three writings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants