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
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
// 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,
) {} void f(int _param) {}void g() { var _local = 1;void h() { final _temp = 2;void i() { for (var _x in <int>[]) { try { } catch (_e) {void k() { final fn = (int _p) => 0;void localFunction() { void _helper() {} class Primary(int _argument, final int field);void renamedField(PatternValue input) { if (input case PatternValue(_value: var _local)) { typedef LocalCallback = void Function(int _value);void functionTypedParameter( void Function(int _value) callback,Valid
// 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.
{
"linter": {
"rules": {
"style": {
"no-leading-underscores-for-local-identifiers": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/style/no-leading-underscores-for-local-identifiers: <reason>More style rules
- no-literal-bool-comparisons
no_literal_bool_comparisonsFlags boolean literals compared or combined with a non-nullable
bool. - no-magic-number
no-magic-numberFlags unnamed numeric literals (magic numbers) used inline in expressions.
- no-object-declaration
no-object-declarationFlags class members declared with the type
Object(orObject?). - no-runtime-type-to-string
no_runtimeType_toStringAvoid calling
toString()onruntimeType. - non-constant-identifier-names
non_constant_identifier_namesrecommendedFlags non-constant identifiers that are not written in lowerCamelCase.
- omit-local-variable-types
omit_local_variable_typesFlags local variable type annotations that repeat the initializer's type.
- only-throw-errors
only_throw_errorsrecommendedOnly throw instances of classes that extend
ExceptionorError. - package-names
package_namesrecommendedcross-fileRequire package names to be valid
lower_case_with_underscoresidentifiers.