ReSharper 2024.1 Help

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

Category

Potential Code Quality Issues

ID

PatternIsRedundant

EditorConfig

resharper_pattern_is_redundant_highlighting

Default severity

Warning

Language

C#

Requires SWA

No

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).

ReSharper 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: 15 April 2024