Value and nullability analysis
JetBrains Rider performs value analysis to help you find possible 'null' dereferences as well as redundant boolean comparisons and null checks.
Different ways of analyzing values
JetBrains Rider can analyze values in several ways:
By understanding the behavior of language constructs.
In the example below, based on the fact that the parameter
objwas explicitly checked for nullability, JetBrains Rider reasonably supposes that the value of
objcan be indeed 'null' and displays the corresponding warning:
In the following example, the method
Baris marked with the
[CanBeNull]attribute, Using this information, JetBrains Rider warns you that the return value of
Barcan be null, and calling a method on it could lead to a 'null' dereference:
In C# 8.0 and later, JetBrains Rider can reuse results of the compiler analysis if Nullable reference types are enabled in the project.
Value and nullability analysis modes
JetBrains Rider can analyze values in different modes:
By default, value analysis mode is optimistic. In this mode, JetBrains Rider only warns about possible 'null' dereference if the value was explicitly checked for nullability, or if the symbol is marked with the
[ItemCanBeNull]attribute. The optimistic mode is demonstrated in the examples above.
In this mode, JetBrains Rider warns about possible 'null' dereference in all contexts where the value is not checked for nullability explicitly, unless the symbol is marked with the
To change value analysis mode or disable the analysis, configure the Value analysis mode preference on the page of JetBrains Rider settings Ctrl+Alt+S.
Nullable reference types
If nullable reference types (NRT) are enabled, JetBrains Rider reuses the results of C# compiler analysis.
NRT improve overall precision of the nullability analysis, but there are cases when NRT contracts can be violated, for example when values are coming from code without
#nullable context. In such cases you may get false positive warnings for nullability checks (for example, Expression is always 'true' or 'false') . In such cases, you can choose to ignore nullable API contracts and only report such cases when previous operations with the value in your code guarantee that it can or cannot be null.
You can change this behavior right from the Alt+Enter menu:
... or using the Nullable reference types' warnings mode option on the page of JetBrains Rider settings .
When JetBrains Rider ignores nullable API contracts, nullability analysis relies on the program control flow to report redundant null checks. For example:
To learn more about NRT and how JetBrains Rider supports them, watch this webinar recording:
Further examples of value analysis
Here are some more examples of JetBrains Rider's value and nullability analysis:
If a nullability check has been already done with a simple LINQ query, JetBrains Rider tells you that a further nullability check is redundant:
The same happens if you mark a collection with the
The next example illustrates how value analysis works when the pessimistic mode is enabled. The value of the list was checked for nullability, but not list items. Therefore JetBrains Rider warns here about possible 'null' dereference.