Value and nullability analysis in C#
JetBrains Fleet performs value analysis to help you find possible 'null' dereferences as well as redundant boolean comparisons and null checks.
Different ways of analyzing nullable values
JetBrains Fleet 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 Fleet 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 Fleet 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 Fleet can reuse results of the compiler analysis if Nullable reference types are enabled in the project.
Equality comparison analysis
JetBrains Fleet helps you analyze the use of the equality operators (
!=) in the following ways:
It reports equality comparison of floating point numbers.
It provides the [CannotApplyEqualityOperator] attribute that helps detect unintended use of the equality operators for types that are designed to be compared using
Nullable reference types
If nullable reference types (NRT) are enabled, JetBrains Fleet 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 report a problem only when previous operations with the value in your code guarantee that it can or cannot be null.
Further examples of value analysis
Here are some more examples of JetBrains Fleet's value and nullability analysis:
If a nullability check has been already done with a simple LINQ query, JetBrains Fleet tells you that a further nullability check is redundant:
The same happens if you mark a collection with the