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.
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
obj
was explicitly checked for nullability, JetBrains Rider reasonably supposes that the value ofobj
can be indeed 'null' and displays the corresponding warning:By relying on code annotation attributes ([CanBeNull], [NotNull], [ItemCanBeNull], [ItemNotNull]) when Nullable reference types are disabled.
In the following example, the method
Bar
is marked with the[CanBeNull]
attribute, Using this information, JetBrains Rider warns you that the return value ofBar
can be null, and calling a method on it could lead to a 'null' dereference:tip
If necessary, you can make code analysis even more precise with contract annotations. They help you define dependencies between arguments of a function and its return value.
In C# 8.0 and later, JetBrains Rider can reuse results of the compiler analysis if Nullable reference types are enabled in the project.
tip
You can instantly fix these problems (for example, automatically add a nullability check) using quick-fixes ( AltEnter).
JetBrains Rider helps you analyze the use of the equality operators (==
and !=
) in the following ways:
It reports equality comparison of floating point numbers.
In addition to compiler warnings of 'Possible unintended reference comparison' (CS0252 and CS0253), it also reports cases when only one of the compared types overrides
Equals()
.It provides the [CannotApplyEqualityOperator] attribute that helps detect unintended use of the equality operators for types that are designed to be compared using
Equals()
.
JetBrains Rider can analyze values in different modes:
- Optimistic
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
[CanBeNull]
or[ItemCanBeNull]
attribute. The optimistic mode is demonstrated in the examples above.
To change value analysis mode or disable the analysis, configure the Value analysis mode preference on the Editor | Inspection Settings page of JetBrains Rider settings CtrlAlt0S.
If nullable reference types (NRT) are enabled, JetBrains Rider reuses the results of C# compiler analysis.
tip
You can enable or disable NRT for a project in the project properties (press AltEnter on the project and choose Properties), or by using the
#nullable
directive in your source code.
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') . You can choose to ignore nullable API contracts and report a problem only 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 AltEnter menu:
![JetBrains Rider: Nullable reference types JetBrains Rider: Nullable reference types](https://resources.jetbrains.com/help/img/rider/2024.3/nrt_false_positive.png)
... or using the Nullable reference types' warnings mode option on the Editor | Inspection Settings 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:
var myString = ApiMethod();
if (myString is null)
throw new ApplicationException("the string is null");
// warning 'Expression is always true'
// 'myString' cannot be null because it's already checked for null in our code
if (myString != null)
Console.WriteLine(myString);
For more information about NRT and how JetBrains Rider supports them, watch this webinar recording:
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
[ItemNotNull]
attribute: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.