Skip to main content

avoid-ignoring-return-values

effort: 4m
teams+

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();
// LINT: Avoid ignoring return values.
// Try assigning this invocation to a variable and referencing it in your code.
foo();

final str = 'Hello there';
// LINT: Avoid ignoring return values.
// Try assigning this invocation to a variable and referencing it in your code.
str.substr(5);

final date = new DateTime(2018, 1, 13);
// LINT: Avoid ignoring return values.
// Try assigning this invocation to a variable and referencing it in your code.
date.add(Duration(days: 1, hours: 23));
}

✅ Good:

int foo() {
return 5;
}

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

void main() {
bar();
final f = foo(); // Correct, return value is assigned

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));
}