Skip to main content

avoid-ignoring-return-values

added in: 1.6.0

Warns when a return value of a method or function invocation or a class instance property access is not used.

Silently ignoring such values may lead to a potential error especially when the invocation target is an immutable instance which has all its methods returning a new instance (for example, String or DateTime classes).

Example

❌ Bad:

int foo() {
return 5;
}

void bar() {
print('whatever');
}

void main() {
bar();
foo(); // LINT: return value is silently ignored

final str = "Hello there";
str.substr(5); // LINT: Strings are immutable and the return value should be handled

final date = new DateTime(2018, 1, 13);
date.add(Duration(days: 1, hours: 23))); // LINT: Return value ignored, DateTime is immutable
}

✅ Good:

int foo() {
return 5;
}

void bar() {
print('whatever');
}

void main() {
bar();
final f = foo();

final str = "Hello there";
final newString = str.substr(5);

final date = new DateTime(2018, 1, 13);
final newDate = date.add(Duration(days: 1, hours: 23)));
}