Rules / Suspicious / avoid-ignoring-return-values

avoid-ignoring-return-values

lint/suspicious/avoid-ignoring-return-values
recommended
Same as avoid-ignoring-return-values · Dart Code Metrics

Flags a call whose non-void return value is discarded.

A function that returns a value usually expects the caller to use it, so dropping the result often signals a bug — forgetting that immutable APIs like String.replaceAll or List.map return a new value instead of mutating in place, or ignoring a status code. When a project index is available the callee's declared return type decides: a known void return is safe to discard, a known non-void return is flagged, and only an unresolved return type falls back to a built-in allowlist of conventionally side-effecting names (logging, collection mutation, stream and sink writes, lifecycle hooks, navigation). Without an index every discarded call not on that allowlist is flagged. Assign the result, act on it, or make the discard explicit.

Invalid

example.dartdart
// Test cases for avoid-ignoring-return-values rule
// Lines with violations have an expectation annotation

String transform(String input) => input.toUpperCase();

int compute(int x, int y) => x + y;

List<int> buildList() => [1, 2, 3];

void badUsage() {
  transform('hello');
  compute(2, 3);
  buildList();
  'hello'.toUpperCase();
  [1, 2, 3].map((x) => x * 2);
}

class Counter {
  int value = 0;

  int increment() => ++value;

  void badMethodCall() {
    increment();
  }
}

// `save` is on the receiver-less side-effect allowlist, but the project index
// sees this declaration returns a value, so discarding it is now flagged — a
// precision win the allowlist alone could not make.
int save() => 42;

void resolvedNonVoidDiscard() {
  save();
}
The return value is not being used
10void badUsage() {
11 transform('hello');
The return value is not being used
11 transform('hello');
12 compute(2, 3);
The return value is not being used
12 compute(2, 3);
13 buildList();
The return value is not being used
13 buildList();
14 'hello'.toUpperCase();
The return value is not being used
14 'hello'.toUpperCase();
15 [1, 2, 3].map((x) => x * 2);
The return value is not being used
23 void badMethodCall() {
24 increment();
The return value is not being used
33void resolvedNonVoidDiscard() {
34 save();

Valid

example.dartdart
// Test cases for avoid-ignoring-return-values rule — no violations
// Return values are used or calls are void-typed.

String transform(String input) => input.toUpperCase();

int compute(int x, int y) => x + y;

List<int> buildList() => [1, 2, 3];

void goodUsage() {
  final result = transform('hello');
  final sum = compute(2, 3);
  final list = buildList();
  final upper = 'hello'.toUpperCase();
  final doubled = [1, 2, 3].map((x) => x * 2).toList();
  print('$result $sum $list $upper $doubled');
}

void voidCallsAreOk() {
  print('logging is fine');
  [1, 2, 3].forEach(print);
}

// `configure` is NOT on the side-effect allowlist, but the project index proves
// it returns void, so discarding its result is correctly not flagged — the
// resolver removes a false positive the allowlist would have raised.
void configure() {}

void resolvedVoidDiscard() {
  configure();
}

class Counter {
  int value = 0;

  int increment() => ++value;

  void goodMethodCall() {
    final newValue = increment();
    print(newValue);
  }
}

How to configure

Set the severity of avoid-ignoring-return-values in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "suspicious": {
        "avoid-ignoring-return-values": "error"
      }
    }
  }
}