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
AvoidDefaultValueForMandatoryParameter: The documentation is wrong #876
Comments
In the mentioned test file, for the following function : function BadFunc
{
[CmdletBinding()]
param(
# this one has default value
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Param1="String",
# this parameter has no default value
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$Param2
)
$Param1
$Param1 = "test"
} the parameter Param2 : # this parameter has no default value
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$Param2 should be it seems to me like this: # this parameter has default value but it is not mandatory
[Parameter(Mandatory=$false)]
[ValidateNotNullOrEmpty()]
[string]
$Param2='Test' And add a third use case: # this parameter has no default value and it is mandatory
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]
$Param3 |
@LaurentDardenne Thanks for the investigation and detailed report. Feel free to open a PR with a fix. I think we can also improve the implementation, I don't understand either why the author decided to trigger it only when using cmdletbinding. |
I think we can also improve the implementation, Feel free to open a PR with a fix. |
@LaurentDardenne After carefully going through all links, I agree now with you that the rule should warn irrespective if |
The documentation does not match the implementation, it appears to be the documentation of an old rule (PSProvideDefaultParameterValue) that has been removed and this one is always quoted in the documentation
See also this discussion around this rule.
I do not understand why the presence of the cmdletbinding attribute influences the rule.
Setting a default value to a mandatory parameter does not make sense in all cases.
From this test :
Or this:
Note that the error message is about cmdlet ...
Maybe the presence of the cmdletbinding attribute is another recommendation: Always Start With CmdletBinding
The text was updated successfully, but these errors were encountered: