prefer-underscore-for-unused-callback-parameters

Same as prefer_underscore_for_unused_callback_parameters in Pyramid Lint

Group
Style
Path
lint/style/prefer-underscore-for-unused-callback-parameters
Default
Off, opt in
Origin
Pyramid Lint
Rule
415 of 512

Flags unused closure parameters that are not named with underscores.

Invalid

example.dartdart
// Bad: unused callback parameter should be named _

void example1() {
  final items = [1, 2, 3];
  items.forEach((item) {
    print('hello');
  });
}

void example2() {
  final map = {'a': 1, 'b': 2};
  map.forEach((key, value) {
    print('hello');
  });
}

void example3() {
  final items = ['x', 'y'];
  items.map((element) => 'mapped').toList();
}

void example4() {
  final numbers = [1, 2, 3];
  numbers.where((n) => true).toList();
}

// Bad: unused parameter in reduce-like operation
void example5() {
  final values = [1, 2, 3, 4];
  values.fold(0, (previous, current) => previous + 1);
}

// Bad: unused index parameter in indexed map
void example6() {
  final items = ['a', 'b', 'c'];
  items.asMap().forEach((index, value) {
    print(value);
  });
}

void shadowedCallback(List<int> items) {
  items.forEach((value) {
    final nested = (value) => value + 1;
    for (final value in items) {
      print(nested(value));
    }
  });
}
Unused callback parameter should be named `_`.
4 final items = [1, 2, 3];
5 items.forEach((item) {
Unused callback parameter should be named `_`.
11 final map = {'a': 1, 'b': 2};
12 map.forEach((key, value) {
Unused callback parameter should be named `_`.
18 final items = ['x', 'y'];
19 items.map((element) => 'mapped').toList();
Unused callback parameter should be named `_`.
23 final numbers = [1, 2, 3];
24 numbers.where((n) => true).toList();
Unused callback parameter should be named `_`.
29 final values = [1, 2, 3, 4];
30 values.fold(0, (previous, current) => previous + 1);
Unused callback parameter should be named `_`.
35 final items = ['a', 'b', 'c'];
36 items.asMap().forEach((index, value) {
Unused callback parameter should be named `_`.
41void shadowedCallback(List<int> items) {
42 items.forEach((value) {

Valid

example.dartdart
// Good: unused callback parameters renamed to _

void example1() {
  final items = [1, 2, 3];
  items.forEach((_) {
    print('hello');
  });
}

void example2() {
  final map = {'a': 1, 'b': 2};
  map.forEach((_, __) {
    print('hello');
  });
}

void example3() {
  final items = ['x', 'y'];
  items.map((_) => 'mapped').toList();
}

void example4() {
  final numbers = [1, 2, 3];
  numbers.where((_) => true).toList();
}

void example5() {
  final items = [1, 2, 3];
  items.forEach((item) {
    print(item);
  });
}

void example6() {
  final map = {'a': 1, 'b': 2};
  map.forEach((key, value) {
    print('$key: $value');
  });
}

// A genuine interpolated read counts as a use of the callback parameter.
void interpolatedRead(List<int> items) {
  items.map((value) => "n=$value").toList();
  items.map((value) => "n=${value + 1}").toList();
}

void sourceOrderCallback(List<int> items) {
  items.forEach((value) {
    print(value);
    {
      final value = 1;
      print(value);
    }
  });
}

void capturedCallback(List<int> items) {
  items.forEach((value) {
    final nested = () => value;
    print(nested());
  });
}

void unbracedCallbackScope(List<int> items) {
  items.forEach((value) {
    if (items.isEmpty) int value = 0;
    print(value);
  });
}

Configure prefer-underscore-for-unused-callback-parameters

Set its severity in falcon.json. See the configuration guide.

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "prefer-underscore-for-unused-callback-parameters": "error"
      }
    }
  }
}

Or silence one occurrence with a suppression comment; the reason is required.

example.dartdart
// falcon-ignore lint/style/prefer-underscore-for-unused-callback-parameters: <reason>
  • prefer-void-callbackflutter

    Prefers Flutter's VoidCallback alias over a nongeneric void Function() type.

  • provide-deprecation-messageprovide_deprecation_messagerecommended

    Flags @deprecated and @Deprecated() annotations that carry no message.

  • public-member-api-docspublic_member_api_docs

    Documents public declarations and members in package libraries.

  • record-fields-ordering

    Flags named record type fields that violate the configured order.

  • require-trailing-commasrequire_trailing_commas

    Flags a multi-line call, declaration, assert, or collection literal that is missing a trailing comma.

  • sized-box-shrink-expandsized_box_shrink_expandrecommendedflutter

    Prefers SizedBox.shrink() / SizedBox.expand() over a SizedBox sized to zero or infinity.

  • slash-for-doc-commentsslash_for_doc_commentsrecommended

    Flags /** ...

  • sort-child-properties-lastsort_child_properties_lastrecommendedflutter

    Sort Widget-valued child properties last, except before closure arguments.