Add new StrictMode version for confusing operator precedence #18134
Labels
Issue-Enhancement
the issue is more of a feature request than a bug
Needs-Triage
The issue is new and needs to be triaged by a work group.
Summary of the new feature / enhancement
There were multiple discussions about PowerShell's confusing operator order:
Just today @SteveL-MSFT accidentally found another example with
+
and,
in #18003 :If even "Principal Software Engineering Manager for PowerShell" (sorry Steve for dragging you into it) cannot get it right, then surely something has to be done.
My proposal to "fix" it without an enormous breaking change of changing operator precedence is to add a new StrictMode version, which emits and error any time a "confusing" operator precedence is encountered without parentheses. Since StrictMode versions are opt-in, this is not a breaking change. I believe this to be a perfect solution, as user simply has to add parentheses to make it explicit what order they expect:
Full list of which combination of operators in what order should emit the error is to be determined, but can be gathered from older issues and https://github.com/nightroman/PowerShellTraps
Proposed technical implementation details (optional)
Implementation should have as little performance impact as possible. I hope that simple pattern matching on the AST should be enough, e.g.
and/or
confusion should be detectable by checking for the patternx -or y -and z
(x -and y -or z
is not an issue, as it always executesand
first, both in PowerShell's same-precendence and other languages' and-before-or).If possible, it should be done at parsing stage, to also detect code in not-taken branches.
The text was updated successfully, but these errors were encountered: