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: unnecesary_duplicate_value #5079

Open
3 of 5 tasks
FMorschel opened this issue Aug 29, 2024 · 1 comment
Open
3 of 5 tasks

proposal: unnecesary_duplicate_value #5079

FMorschel opened this issue Aug 29, 2024 · 1 comment
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

unnecesary_duplicate_value

Description

Remove unnecessary ||/&& values to avoid redundancy.

Details

When the || or && operator is used with identical values, leading to redundant code, removing these unnecessary duplicates improves code clarity and reduces potential confusion. By enforcing this lint, developers are encouraged to write more concise and maintainable switch cases, eliminating redundancy without changing the logic of the program. This lint is particularly useful in ensuring that codebases remain clean and that unnecessary conditions do not obscure the intended logic.

Kind

This lint enforces style advice by ensuring more concise and clear code, eliminating redundant conditions in switch cases.

Bad Examples

return switch (value) {
    3 || 3 => 1,  // Lint should trigger here
    _ => 3,
};
if (value == 5 || value == 5) {  // Lint should trigger here
    doSomething();
}
if (value == 5 && value == 5) {  // Lint should trigger here
    doSomething();
}

Good Examples

return switch (value) {
    3 => 1,
    _ => 3,
};
if (value == 5) {
    doSomething();
}

Discussion

Inspired by #5078 for cases of big or expressions where the values might not be exactly side by side and can be hard to spot. Usually, these cases appear when you are doing code refactoring so I believe this could help mainly in those cases.

This could of course have some false negatives but I believe it is best to have some kind of warning than to have none.

If this is ever implemented, I believe this 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.
@srawlins srawlins added the type-enhancement A request for a change that isn't a bug label Aug 30, 2024
@FMorschel
Copy link
Contributor Author

This would also probably have some relation to unreachable_switch_case and #5078

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