The Notorious PSScriptAnalyzer
So I'm trying to tweak the notoriously dysfunctional/irritating PSSCriptAnalyzer in VSCode. All I want to do is suppress warnings for the whole "Hey you didn't use this declared variable :)" rule (which is actually called PSUseDeclaredVarsMoreThanAssignments). However, I only want the warnings to be suppressed for specific variables, and I'm so close to getting it to work, but I'm missing something.
My favorite part about this nonsense is that I'm getting warnings for a built-in preference variable, $ErrorView, even though the rest of the preference variables are fine.
The code so far...
I've managed to disable the rule completely by putting the following into Microsoft.PowerShell_profile.ps1:
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments')]
param($ErrorView)
Anything I put in the param() block made no difference, as long as it was there (empty or not), the whole rule was turned off.
Then I got even closer
But still not quite there.
While I was actually typing this post, I realized part of the problem was that it needs to be a function, then I can specify which variables I want the rule to ignore. And it almost worked. I know, obvious in hindsight, but it's been a long day. 🙃
What I have now:
# Microsoft.PowerShell_profile.ps1
function SuppressUnused() {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments')]
param($ErrorView)
}
Anyway, I thought I had finally gotten it to work when I got the desired result in my profile (in the attached picture), but when I tested it in a random .ps1 file, the $ErrorView variable was still being flagged as unused.