Note: This may be an exotic scenario that is easily avoided (no reason to ever use return this way), but I just came across it on Stack Overflow, and the behavior is surprising and unhelpful.
Steps to reproduce
# The assignment is *ignored* due to `return`, and the output goes straight to the success output stream.
$var = return 'foo'
$var | Should -BeExactly 'foo'
Expected behavior
The test should succeed.
Actual behavior
The test fails, because the assignment is ignored (the variable isn't even created) due to use of return.
The output goes straight to the success output stream and therefore prints to the display here.
Environment data
PowerShell Core 7.1.0-preview.5
Note: This may be an exotic scenario that is easily avoided (no reason to ever use
returnthis way), but I just came across it on Stack Overflow, and the behavior is surprising and unhelpful.Steps to reproduce
Expected behavior
The test should succeed.
Actual behavior
The test fails, because the assignment is ignored (the variable isn't even created) due to use of
return.The output goes straight to the success output stream and therefore prints to the display here.
Environment data