JetBrains Rider 2023.1 Help

Code Inspection: Expression is always 'true' or 'false' according to nullable reference types' annotations

This inspection works in C# 8.0 and later when nullable reference types (NRT) are enabled, and reports expressions that are always true or always false based on the NRT contracts.

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

You can change this behavior right from the Alt+Enter menu:

JetBrains Rider: Nullable reference types

... 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. It will use another inspection for that. 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);

To learn more about NRT and how JetBrains Rider supports them, watch this webinar recording:

Last modified: 06 October 2022