question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improper usage of $input as a function parameter is silently ignored

See original GitHub issue

As I learned the hard way today, $input is PowerShell automatic variable.

PowerShell seems to ignore obviously improper usage of the $input, leaving the programmer confused about the parameter not being propagated further.

Steps to reproduce

There are two identical (non-optimal, I admit) implementations of Ensure-Array. They both differ only in parameter name, nothing more.

function Ensure-ArrayA($input)
{
    $result = @()
    if ($input -is [system.array])
    {
        $result = $input
    }
    else
    {
        $result = @($input)
    }
    return [Array]$result
}

function Ensure-ArrayB($Unknown)
{
    $result = @()
    if ($Unknown -is [system.array])
    {
        $result = $Unknown
    }
    else
    {
        $result = @($Unknown)
    }
    return [Array]$result
}

$x = Ensure-ArrayA 1
$y = Ensure-ArrayB 1

$x.Count
$y.Count

Expected behavior

I would presume PowerShell would fire an exception regarding improper usage of the $input as function parameter in Ensure-ArrayA.

Actual behavior

Instead, PowerShell silently ignores this incorrect code, pretending everything is all right. Of course, using $input will result in incorrect output as consequence.

The script above will output simply:

0
1

Environment data

> $PSVersionTable

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

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:1
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
SteveL-MSFTcommented, Aug 31, 2017

Although a breaking change, I think we can address this in 6.1.0

1reaction
mklement0commented, Aug 4, 2017

See this documentation issue for the necessary attendant change to the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What are the consequences of ignoring: warning: unused ...
It means you wrote a function that takes a parameter but doesn't use the parameter. It's harmless but it might indicate bugs in...
Read more >
Should I raise an exception/error when an optional ...
If an argument is mandatory, then you should raise an exception when it's not passed. However, if it's optional, then you should either...
Read more >
noUnusedParameters how can i skip uneeded parameters
If there are parameters on the left side of a used one, the unused error is silently ignored. 36
Read more >
Allow the parameter name _ (underscore) multiple times in a ...
One thing I've often noticed when implementing interfaces from third party libraries in Python is that I can't explicitly state that I won't ......
Read more >
Chapter 5: Error Handling
¶ When the program is running, and the function is called like that, the code that called it will get a string value,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found