Improper usage of $input as a function parameter is silently ignored
See original GitHub issueAs 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:
- Created 6 years ago
- Reactions:1
- Comments:7 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Although a breaking change, I think we can address this in 6.1.0
See this documentation issue for the necessary attendant change to the documentation.