BRO1407: Conditional expressions should declare precedence¶
&& and || aren't mixed without parentheses, and neither are the and and or patterns. Replaces StyleCop
SA1408.
Example¶
Before¶
public class Access
{
public bool CanEdit(bool admin, bool owner, bool locked, int level) =>
admin || owner && !locked || level is > 5 and < 9 or 10;
}
After¶
public class Access
{
public bool CanEdit(bool admin, bool owner, bool locked, int level) =>
admin || (owner && !locked) || level is (> 5 and < 9) or 10;
}
Why¶
&& binds tighter than ||, but readers often don't remember that, and a misread condition is a bug.
How the fix works¶
The && (or and) operand of an || (or or) gets parentheses. Nested operands get all their parentheses in one
pass.
This rule replaces the SDK's IDE0048, whose fix writes merge conflict markers in multi-targeted projects under
dotnet format (reported upstream as dotnet/roslyn#86000; see decisions).
Configuration¶
[*.cs]
# The SDK's setting: always_for_clarity (or not set) keeps this rule; never_if_unnecessary turns it off.
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity
Compared with StyleCop¶
Same positions and output for &&/||. The released StyleCop (1.2.0-beta.556) doesn't check patterns; its current
source does, and so does this rule.