constant-on-right
What it does
Section titled “What it does”Reports a comparison with the constant on the left: null == x is x == null, 1 < x is x > 1. Without the type layer a constant is a literal, nameof, a member such as int.MaxValue, or an expression made of those; a named constant on the left is not recognised.
Why it matters
Section titled “Why it matters”“Yoda” conditions came from C, where x = null by mistake compiled. In C# an assignment in a condition does not compile for a non-boolean, so the form has no protection to give, and it reads backwards: x == null says what is tested first.
To fix, swap the sides and mirror the operator. The fix is safe and does it.
Example
Section titled “Example”... void F(object o, int i) {... var g = null == o || 1 < i;... }}... void F(object o, int i) {... var g = o == null || i > 1;... }}Configuration
Section titled “Configuration”csharp: lint: rules: constant-on-right: warn # off (default), warn, errorEnabled by the presets: csharp/stylecop, csharp/roslynator.
Suppress one line with sudsy-disable-next-line constant-on-right in a comment of the file’s language.
Upstream equivalents
Section titled “Upstream equivalents”The same check in other tools; Sudsy reports what each upstream rule reports.
| Tool | Rule | Match |
|---|---|---|
| Roslynator | RCS1098 |
full |
| StyleCop | SA1131 |
full |