Function literal that only forwards its parameters to one call

ID

dart.performance.unnecessary_lambdas

Severity

info

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Redundancy

Language

Dart

Description

Reports a function literal whose whole body is a single call receiving the literal’s own parameters, in the same order and with nothing added - (v) ⇒ scale(v), (a, b) ⇒ combine(a, b), () ⇒ reload() and their block-bodied equivalents. A literal that transforms, reorders or adds to its arguments is not reported, and neither is one passed to forEach, which is covered by its own rule.

Rationale

The literal is a second function wrapping the first, so each element pays for an extra indirect call, and the wrapper itself is a closure that has to be allocated - inside a Flutter build method or a map in a hot path, on every call. A tear-off passes the target function directly, with no wrapper to allocate or call. The literal also weakens what the reader and the compiler see: the parameter types come from the surrounding context rather than the target’s signature, so a change to that signature surfaces as an error inside the closure body instead of at the argument, and the forwarding hides whether anything else happens on the way.

The following code illustrates the pattern detected by this rule:

class Sink {
  void write(String line) {}
}

class Pipeline {
  final Sink sink = Sink();

  int scale(int value) => value * 2;
  int combine(int a, int b) => a + b;
  void reload() {}

  Iterable<int> scaled(List<int> values) {
    // FLAGGED: Function literal that only forwards its parameters to one call
    return values.map((v) => scale(v));
  }
}

Remediation

Pass the function as a tear-off - values.map(scale), onRefresh: reload, lines.map(sink.write). Keep the literal only where the body does something more than forward the parameters, such as reordering arguments or supplying an extra one.

Configuration

This detector does not need any configuration.

© 2026 Xygeni. All rights reserved