no-leading-underscores-for-local-identifiers

Same as no_leading_underscores_for_local_identifiers in Dart lints

Group
Style
Path
lint/style/no-leading-underscores-for-local-identifiers
Default
On (recommended)
Origin
Dart lints
Rule
289 of 512
recommended

Flags local variables and parameters whose names begin with an underscore.

A leading underscore marks a declaration as library-private, but that privacy only applies to top-level and class members — locals, parameters, closure parameters, for-in variables, and catch clause bindings are already confined to their scope. Prefixing them with an underscore implies a privacy that does not exist and misleads the reader, so drop it. Wildcard names made solely of underscores (e.g. _, __) are exempt, being the idiomatic way to name a deliberately unused binding.

Invalid

example.dartdart
// Local variables and parameters must not start with an underscore.

void f(int _param) {}

void g() {
  var _local = 1;
  print(_local);
}

void h() {
  final _temp = 2;
  print(_temp);
}

void i() {
  for (var _x in <int>[]) {
    print(_x);
  }
}

void j() {
  try {
  } catch (_e) {
    print(_e);
  }
}

void k() {
  final fn = (int _p) => 0;
  print(fn(1));
}

class Mixed {
  final String _token;
  Mixed(this._token, int _other);
}

void localFunction() {
  void _helper() {}
  _helper();
}

class Primary(int _argument, final int field);

class PatternValue {
  final int _value;
  PatternValue(this._value);
}

void renamedField(PatternValue input) {
  if (input case PatternValue(_value: var _local)) {
    print(_local);
  }
}

typedef LocalCallback = void Function(int _value);

void functionTypedParameter(
  void Function(int _value) callback,
) {}
Avoid leading underscores for local identifiers.
2
3void f(int _param) {}
Avoid leading underscores for local identifiers.
5void g() {
6 var _local = 1;
Avoid leading underscores for local identifiers.
10void h() {
11 final _temp = 2;
Avoid leading underscores for local identifiers.
15void i() {
16 for (var _x in <int>[]) {
Avoid leading underscores for local identifiers.
22 try {
23 } catch (_e) {
Avoid leading underscores for local identifiers.
28void k() {
29 final fn = (int _p) => 0;
Avoid leading underscores for local identifiers.
38void localFunction() {
39 void _helper() {}
Avoid leading underscores for local identifiers.
42
43class Primary(int _argument, final int field);
Avoid leading underscores for local identifiers.
50void renamedField(PatternValue input) {
51 if (input case PatternValue(_value: var _local)) {
Avoid leading underscores for local identifiers.
55
56typedef LocalCallback = void Function(int _value);
Avoid leading underscores for local identifiers.
58void functionTypedParameter(
59 void Function(int _value) callback,

Valid

example.dartdart
// Locals without leading underscores; wildcards and members are exempt.

int _private = 0;

class C {
  int _field = 0;
}

void f(int param) {
  print(param);
}

void g() {
  var local = 1;
  print(local);
}

void h(int _) {}

void i() {
  var __ = 1;
  print(__);
}

// Initializing formals and super formals are not local identifiers: the
// underscore denotes the field's privacy and the spelling is forced.
class Provider {
  final String _token;
  Provider(this._token);
}

class NamedProvider {
  final String _token;
  NamedProvider({required this._token});
}

class SubProvider extends Provider {
  SubProvider(super._token);
}

// Declaring primary parameters and extension representations name private fields.
class Primary(final int _field);
class MutablePrimary(var int _field);
extension type Representation(int _value) {}

class ObjectValue {
  final int _value;
  ObjectValue(this._value);
}

void fieldShortcut(ObjectValue input) {
  if (input case ObjectValue(:var _value)) {
    print(_value);
  }
}

void localWildcardFunction() {
  void __() {}
  __();
}

typedef LocalCallback = void Function(int value);

void functionTypedParameter(void Function(int value) callback) {}

Configure no-leading-underscores-for-local-identifiers

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

falcon.jsonjson
{
  "linter": {
    "rules": {
      "style": {
        "no-leading-underscores-for-local-identifiers": "error"
      }
    }
  }
}

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

example.dartdart
// falcon-ignore lint/style/no-leading-underscores-for-local-identifiers: <reason>
  • no-literal-bool-comparisonsno_literal_bool_comparisons

    Flags boolean literals compared or combined with a non-nullable bool.

  • no-magic-numberno-magic-number

    Flags unnamed numeric literals (magic numbers) used inline in expressions.

  • no-object-declarationno-object-declaration

    Flags class members declared with the type Object (or Object?).

  • no-runtime-type-to-stringno_runtimeType_toString

    Avoid calling toString() on runtimeType.

  • non-constant-identifier-namesnon_constant_identifier_namesrecommended

    Flags non-constant identifiers that are not written in lowerCamelCase.

  • omit-local-variable-typesomit_local_variable_types

    Flags local variable type annotations that repeat the initializer's type.

  • only-throw-errorsonly_throw_errorsrecommended

    Only throw instances of classes that extend Exception or Error.

  • package-namespackage_namesrecommendedcross-file

    Require package names to be valid lower_case_with_underscores identifiers.