Code inspection: Return type of a function can be made non-nullable
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.
#nullable enable
MyClass? GetMyClass() => new MyClass();
#nullable enable
MyClass GetMyClass() => new MyClass();
For more information about NRT and how ReSharper supports them, watch this webinar recording:
Last modified: 25 September 2024