ReSharper
 
Get ReSharper
Get your hands on the new features ahead of the release by joining the Early Access Program for ReSharper 2025.1! Learn more

Code inspection: Return type of a function can be made non-nullable

Last modified: 25 September 2024

If nullable reference types (NRT) are enabled, this inspection reports redundant nullable reference types' annotations (?) on return types of methods whose return value is determined to never be null based on the control flow analysis.

In the example below, the return value of the GetMyClass() method will never be null because the method unconditionally returns a new instance of MyClass.

ReSharper suggests removing the ? annotation on the return type of the method to improve readability and to avoid unnecessary null checks for usages of the method.

For more information about NRT and how ReSharper supports them, watch this webinar recording: