avoid-ignoring-return-values
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: Avoid ignoring return values.
final str = "Hello there";
str.substr(5); // LINT: Avoid ignoring return values.
final date = new DateTime(2018, 1, 13);
date.add(Duration(days: 1, hours: 23)); // LINT: Avoid ignoring return values.
}
✅ 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));
}