JetBrains Rider 2024.3 Help

Code inspection: The pattern is redundant, it does not produce any runtime checks

C# or/and/not patterns can be useful to produce compact value checks without repeating the input value (compare value == 1 || value == 2 with value is 1 or 2). However, when several patterns are used together, their precedence is not that clear when one reads the expression, and this may introduce hard-to-find bugs.

Consider the following expression: value is not 0 or -1.

If the user intends to check the value for being not equal to both 0 or 1, this check will evaluate to true when value == -1 because is not would have higher priority than or. To fix the pattern for the intended check, we need to add parenthesis to change the precedence: value is not (0 or -1).

JetBrains Rider reports such patterns, but it does not suggest any quick-fixes because the pattern could be corrected in different ways according the user intent.

void Sample(int value) { if (value is not 0 or -1) { Console.WriteLine(value); } }
void Sample(int value) { if (value is not (0 or -1)) { Console.WriteLine(value); } }
Last modified: 08 April 2024