Skip to content

Can we prevent / warn of nonsensical uses of parameter and validation attributes? #10614

@mklement0

Description

@mklement0

Summary of the new feature/enhancement

There are cases where incorrect use of parameter and validation attributes results in them getting quietly ignored, which is undesirable from a UX perspective.

Ideally, such cases would cause errors at parse time (though I don't know if that's feasible), ideally complemented with PSScriptAnalyzer warnings.

If feasible, the changes would technically be breaking; if that's a problem, PSScriptAnalyzer warnings will have to do.

Quietly ignored [Parameter()] attributes if accidentally used outside param(...)

& {
  # Note the missing param(...) enclosure.
    [Parameter(ValueFromPipeline)]  # quietly ignored
    $foo = 'bar'    # parsed as a  local variable, not as parameter
}

Quietly ignored [Validate*()] attributes if a variable isn't initialized:

& {
   # Missing `= <RHS>`, so the attribute is ignored and the 
   # value of $foo (if any) is returned
   [ValidateRange(1,10)] $foo
}

Pointless combination of [Parameter(Mandatory)] with a parameter default value:

Note that there's one case where a PSScriptAnalyzer warning alone is probably the right solution - and that warning already exists - given that existing code may still behave as designed (the default value may be a remnant from before a parameter was made mandatory):

& {
  param(
    [Parameter(Mandatory)]
    $foo = 'bar' # pointless, because `Mandatory` still prompts
  )
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or more

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions