Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude switch expression cases from prefer_final_locals lint #59710

Open
jodinathan opened this issue Dec 12, 2024 · 2 comments
Open

Exclude switch expression cases from prefer_final_locals lint #59710

jodinathan opened this issue Dec 12, 2024 · 2 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-false-positive P4 triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug

Comments

@jodinathan
Copy link

The prefer_final_locals lint currently triggers warnings for variables declared within switch expression cases. This behavior can lead to unnecessary boilerplate or awkward refactoring, as the cases often represent one-off usage patterns where the enforcement of final adds no clear benefit.

Example

return switch (val) {
  Enum en => () {
    assert(Queryable.words.containsKey(en.name));
    return Queryable.words[en.name];
  }(),
  bool b => b ? 1 : 0,
  _ => val,
};

In this example, the lint suggests making en and b final. However, these variables are implicit, context-specific, and short-lived, making the suggestion less meaningful. Forcing developers to ignore the lint in these scenarios is counterproductive.

Proposed Solution
Update the prefer_final_locals lint to exclude variables implicitly declared within switch expression cases. This would align the lint's behavior with practical usage patterns and avoid unnecessary warnings.

Benefits
Improves developer experience by avoiding irrelevant lint warnings.
Maintains code clarity and reduces the need for lint overrides.
Encourages adoption of switch expressions without friction.

Additional Context
This behavior is similar to how final enforcement is not applied to loop variables in for or forEach constructs, where implicit, short-lived variables are common.

@jodinathan
Copy link
Author

it also happens in pattern matching: if (foo case Bar daz) // says [daz] should be final

@munificent munificent transferred this issue from dart-lang/dart_style Dec 12, 2024
@dart-github-bot
Copy link
Collaborator

Summary: The prefer_final_locals lint flags variables in switch expression cases as non-final, despite their short-lived nature offering no practical benefit. The proposed solution is to exclude these cases from the lint.

@dart-github-bot dart-github-bot added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug labels Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-false-positive P4 triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

3 participants