GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — New rule - ReviewUnusedParameter by mattmcnabb · Pull Request #1382 · PowerShell/PSScriptAnalyzer Conversation
|
|
Hmm.... did adding this rule break the tests for AvoidAssignmentToAutomaticVariables? At a glance it looks like those tests need to use |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…the new rule as the parameter is not used in those tests
|
Those tests have function definitions that do not use their parameters, I simply excluded this rule for those tests. It's always a difficult debate whether the tests should test the rules individually or run all rules. There's pros and cons against either but James suggested to run all rules if possible because it provides better integration coverage, especially due to PSSA's multi threaded nature where complex things can happen if all rules execute at once |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…ToList() and minor style tweaks (Linq variable naming and trailing whitespace trim)
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I fixed the other test failures that were not your fault and made some small tweaks. Very happy with this PR and one of the main reasons why I am so eager to come back and get this PR merged is because I am really looking forward to using it
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Thanks for the fixes. I'm eager to get this in there as well! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
| { | ||
| // compare the list of variables to the parameter name | ||
| // there should be at least two matches of the variable name since the parameter declaration counts as one | ||
| int matchCount = variables |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
At this point, the algorithm becomes O(n^2) in the number of parameters, which is undesirable.
Instead, it should be possible to do the following:
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Rob Holt (@rjmholt) Forgive me, but I'm failing to see how that will reduce the complexity. Isn't the current code doing essentially what you've described?
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
It almost does, but currently:
HashSet<string>.Where(variable => variable == parameterAst.Name.VariablePath.ToString())). Instead it should be case insensitive. This is easy to do with new HashSet<string>(StringComparer.OrdinalIgnoreCase).
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it's definitely case-sensitive at the moment. I'll take a look at implementing the hashset - I think I can come up with something. Thanks!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Matt McNabb (@mattmcnabb) It seems we have a merge conflict here (probably just a simple one like the number of rules), please resolve |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Matt McNabb (@mattmcnabb) In your last commit, you resolved the merge conflict by taking |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
well that was rookie! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Rob Holt (@rjmholt) Matt McNabb (@mattmcnabb) I pushed one last commit to use |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Close and re-open to rerun ci |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Code looks good. Just left a comment on the test, but it's an edge case so not blocking.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
|
||
| It "has no violations when parameter is called in child scope" -skip { | ||
| $ScriptDefinition = 'function Param { param ($Param1) function Child { $Param1 } }' | ||
| $ScriptDefinition = 'function foo { param ($Param1) function Child { $Param1 } }' |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I would think of this as a violation. Since PowerShell has dynamic, rather than lexical, scope, Child's $Param1 reference is not guaranteed to be foo's $Param1 parameter.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I'd rather avoid violations that are going to be false positives in most cases in order to avoid similar problems to PSUseDeclaredVarsMoreThanAssignments
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I think fair enough we want to not emit when at the boundary of our heuristic. Ideally we'd change this to an actual false case, but it's not that important.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| IDictionary<string, int> variableCount = scriptBlockAst.FindAll(oneAst => oneAst is VariableExpressionAst, false) | ||
| .Select(variableExpressionAst => ((VariableExpressionAst)variableExpressionAst).VariablePath.UserPath) | ||
| .GroupBy(variableName => variableName, StringComparer.OrdinalIgnoreCase) | ||
| .ToDictionary(variableName => variableName.Key, variableName => variableName.Count(), StringComparer.OrdinalIgnoreCase); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Rob Holt (@rjmholt) Your recently added complex.psm1 test actually caught an edge case that happened when I first added StringComparer.OrdinalIgnoreCase only on .ToDictionary, which caused items of different cases (due to groupby originally being case sensitive) to to be added to the case insensitive dictionary, which then gave the error that the same item had already been added. Therefore I had to add it to GroupBy as well. Chapeau 👏
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
Makes me think that PowerShell and other PowerShell tools should have a few real-world test cases too
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Well, it looks like I'm no longer needed here... I'ma head to bed now - let me know if you guys need anything 🤣 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Christoph Bergmeister (@bergmeister) Rob Holt (@rjmholt) thanks for the fixes on this! I wasn't able to devote much time to reviewing this after the start of the new year. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.