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

proposal: prefer_merging_statement #5078

Open
2 of 5 tasks
FMorschel opened this issue Aug 29, 2024 · 3 comments
Open
2 of 5 tasks

proposal: prefer_merging_statement #5078

FMorschel opened this issue Aug 29, 2024 · 3 comments
Labels
lint-proposal status-pending type-enhancement A request for a change that isn't a bug

Comments

@FMorschel
Copy link
Contributor

FMorschel commented Aug 29, 2024

prefer_merging_statements

Description

Prefer merging switch statements with identical bodies to improve code readability and reduce redundancy.

Details

Merging switch statements with identical bodies improves code readability and reduces redundancy.

Kind

This lint enforces style advice by promoting code readability and reducing redundancy through the merging of identical switch statement bodies.

Bad Examples

switch (value) {
  case value1:
    return 1;   // Lint here
  case value2:
    return 3;
  case value3:
    return 1;   // Lint here
  default:
    return 2;
}
switch (value) {
  value1 => 1;   // Lint here
  value2 => 3;
  value3 => 1;   // Lint here
  _ => 2;
}

Good Examples

switch (value) {
  case value1:
  case value3:
    return 1;
  case value2:
    return 3;
  default:
    return 2;
}
switch (value) {
  case value1 || value3:
    return 1;
  case value2:
    return 3;
  default:
    return 2;
}
switch (value) {
  value1 || value3 => 1;
  value2 => 3;
  _ => 2;
}

Discussion


Edit

This would complement unreachable_switch_case:

This case is covered by the previous cases.
Try removing the case clause, or restructuring the preceding patterns.

The future quick-fixes could be the two above examples where the case value3 is moved up removing the current duplicated code or for it to merge with the other statement using the || operator.

I believe if this is implemented it should be added to Effective Dart.

Discussion checklist

  • List any existing rules this proposal modifies, complements, overlaps or conflicts with.
  • List any relevant issues (reported here, the [SDK Tracker], or elsewhere).
  • If there's any prior art (e.g., in other linters), please add references here.
  • If this proposal corresponds to [Effective Dart] or [Flutter Style Guide] advice, please call it out. (If there isn't any corresponding advice, should there be?)
  • If this proposal is motivated by real-world examples, please provide as many details as you can. Demonstrating potential impact is especially valuable.
@FMorschel FMorschel changed the title proposal: prefer_merging_cases proposal: prefer_merging_statement Aug 29, 2024
@FMorschel
Copy link
Contributor Author

Inspired by dart-lang/sdk#56597

@FMorschel
Copy link
Contributor Author

This would be pretty useful in contexts where there is a big switch statement and some refactoring was done. So that the user can see the identical body.

I know this could lead to false negatives but I think in this case it is better to have some form or warning than to not have any.

@FMorschel
Copy link
Contributor Author

FMorschel commented Aug 30, 2024

This would be great with dart-lang/sdk#56180 and would probably relate to #5079

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-proposal status-pending type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants