Rules / Correctness / avoid-unused-parameters

avoid-unused-parameters

lint/correctness/avoid-unused-parameters
recommended
Same as avoid-unused-parameters · Dart Code Metrics

Disallow declared parameters that are never used.

Flags a function or method parameter whose name never appears in the body. An unused parameter is usually a leftover from a refactor or a sign the implementation drifted from its signature; either way it misleads callers about what the function actually consumes. Remove it, or rename it to _ — an all-underscore name (_, __, …) is the convention for a deliberately ignored parameter and is never flagged. Parameters of @override methods and of noSuchMethod are exempt because their signatures are dictated by the supertype, as are constructor field (this.x) and super (super.x) parameters. The check reaches into functions and the members of classes, mixins, mixin classes, enums, extensions, and extension types.

Invalid

example.dartdart
// Test cases for avoid-unused-parameters rule
// Flags function/method parameters that are declared but never referenced

void logMessage(String message, String unused) {
  print(message);
}

void processData(int value, String ignore, bool flag) {
  print(value);
  if (flag) {
    print("flagged");
  }
}

void simpleUnused(String name, String unused) {
  return;
}

class MyClass {
  void method(int id, String unused) {
    print(id);
  }

  void anotherMethod(int a, int b, int c) {
    print(a);
  }

  static void staticMethod(String key, String ignored) {
    print(key);
  }
}

typedef Callback = void Function(String result, String unused);

void functionWithCallback(Callback cb, String unused) {
  cb("result");
}

Future<void> asyncFunction(int timeout, String unused) {
  return Future.delayed(Duration(milliseconds: timeout));
}

extension StringExt on String {
  void customMethod(String value, String unused) {
    print(value);
  }
}
Parameter 'unused' is declared but not used
3
4void logMessage(String message, String unused) {
Parameter 'ignore' is declared but not used
7
8void processData(int value, String ignore, bool flag) {
Parameter 'name' is declared but not used
14
15void simpleUnused(String name, String unused) {
Parameter 'unused' is declared but not used
14
15void simpleUnused(String name, String unused) {
Parameter 'unused' is declared but not used
19class MyClass {
20 void method(int id, String unused) {
Parameter 'b' is declared but not used
23
24 void anotherMethod(int a, int b, int c) {
Parameter 'c' is declared but not used
23
24 void anotherMethod(int a, int b, int c) {
Parameter 'ignored' is declared but not used
27
28 static void staticMethod(String key, String ignored) {
Parameter 'unused' is declared but not used
34
35void functionWithCallback(Callback cb, String unused) {
Parameter 'unused' is declared but not used
38
39Future<void> asyncFunction(int timeout, String unused) {
Parameter 'unused' is declared but not used
43extension StringExt on String {
44 void customMethod(String value, String unused) {

Valid

example.dartdart
// Good examples for avoid-unused-parameters rule
// Using all declared parameters

void logMessage(String message) {
  print(message);
}

void processData(int value, String name, bool flag) {
  print(value);
  print(name);
  if (flag) {
    print("flagged");
  }
}

void usingAllParameters(String a, String b, String c) {
  print(a);
  print(b);
  print(c);
}

class MyClass {
  void method(int id, String name) {
    print(id);
    print(name);
  }

  void anotherMethod(int a, int b, int c) {
    print(a + b + c);
  }

  static void staticMethod(String key, String value) {
    print("$key: $value");
  }
}

typedef Callback = void Function(String result);

void functionWithCallback(Callback cb) {
  cb("result");
}

Future<void> asyncFunction(int timeout) {
  return Future.delayed(Duration(milliseconds: timeout));
}

extension StringExt on String {
  void customMethod(String value) {
    print(value);
  }

  int compareWith(String other) {
    return length.compareTo(other.length);
  }
}

void functionUsingInNestedScope(String param) {
  void inner() {
    print(param);
  }
  inner();
}

// Parameter used only inside a closure with a BLOCK body (regression: the
// reference collector must descend into block-bodied closures).
List<int> mapInClosure(int factor) {
  return [1, 2, 3].map((x) {
    return x * factor;
  }).toList();
}

// Parameter used only inside a switch-case body.
String describe(int code) {
  switch (code) {
    case 200:
      return 'ok';
    default:
      return 'code $code';
  }
}

// Parameter used only inside a cascade section.
void configure(Object target, String label) {
  target
    ..toString()
    ..hashCode.toString()
    ..runtimeType.toString();
  print(label);
}

class Overrides {
  // @override methods keep the supertype's parameter list; an unused param here
  // is not the author's to remove, so it is exempt.
  @override
  void didUpdateWidget(Object oldWidget) {
    print('updated');
  }
}

// All-underscore parameter names are conventional "unused" markers.
void ignoresArgs(int __, String ___) {
  print('ignored');
}

// Parameter used only inside a map-comprehension iterable
// (`{ for (..) k: v }` lives in `Map.elements`, not `Map.entries`).
Map<String, int> buildLookup(List<String> keys) {
  return {for (final k in keys) k: k.length};
}

How to configure

Set the severity of avoid-unused-parameters in your falcon.json:

falcon.jsonjson
{
  "linter": {
    "rules": {
      "correctness": {
        "avoid-unused-parameters": "error"
      }
    }
  }
}