avoid-unnecessary-nullable-return-type
Warns when the return type of a function or a method is declared nullable, but the function or method always return non-nullable value.
Functions with nullable return values usually require additional checks when using such values (for example, a separate code branch for a null
result). Removing unnecessary nullability can help reduce the number of checks and simplify the code.
Example
❌ Bad:
const value = '123';
// LINT: Declared return type is unnecessary marked as nullable. Try removing '?'.
String? function() {
return 'srt';
}
// LINT: Declared return type is unnecessary marked as nullable. Try removing '?'.
String? function1() {
return value;
}
// LINT: Declared return type is unnecessary marked as nullable. Try removing '?'.
String? function2() {
return nonNullable();
}
✅ Good:
const value = '123';
String function() { // Correct, the return value is always non-null
return 'srt';
}
String function1() {
return value;
}
String function2() {
return nonNullable();
}