Skip to content

Lint when using variable named _ #51221

Closed
@munificent

Description

@munificent

Inside a pattern, an identifier named _ is a non-binding wildcard. This means you can use it more than once without a collision error:

var [a, _, b, _] = [1, 2, 3, 4]; // OK.

With patterns, we are not changing the behavior of declarations named _ outside of patterns. So this still works:

var _ = 1;
print(_);

But we would eventually like to change the language so that (at least) parameters and local variables named _ become non-binding.

This change is breaking because currently those identifiers are binding and can be used. It's rare for users to refer to parameters and variables named _, but I do see some examples of it in the wild.

To ease that transition, we could add a lint that fires whenever you use a parameter or variable named _ (or any sequence of only underscores, __, ___, etc.) The lint would suggest that they pick another name for the variable if they're going to refer to it. The lint wouldn't warn if you declare a variable or parameter named _. That's fine. It should only be a violation to have an identifier expression or assignment target that uses one of these variables.

That should make it less breaking to make _ non-binding in a later Dart release.

cc @dart-lang/language-team

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work ondart-model-language-patternsIssues with analyzer's support for the patterns language featuredevexp-linterIssues with the analyzer's support for the linter packagelegacy-area-analyzerUse area-devexp instead.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions