-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Shared code in if and else #5234
Labels
A-lint
Area: New lints
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
L-complexity
Lint: Belongs in the complexity lint group
Comments
flip1995
added
L-complexity
Lint: Belongs in the complexity lint group
E-hard
Call for participation: This a hard problem and requires more experience or effort to work on
A-lint
Area: New lints
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
and removed
E-hard
Call for participation: This a hard problem and requires more experience or effort to work on
labels
Mar 2, 2020
I would like to work on this 🙃. @rustbot claim |
bors
added a commit
that referenced
this issue
Apr 5, 2021
New Lint: `branches_sharing_code` This lint checks if all `if`-blocks contain some statements that are the same and can be moved out of the blocks to prevent code duplication. Here is an example: ```rust let _ = if ... { println!("Start"); // <-- Lint for code duplication let _a = 99; println!("End"); // <-- Lint for code duplication false } else { println!("Start"); let _b = 17; println!("End"); false }; ``` This could be written as: ```rust println!("Start"); let _ = if ... { let _a = 99; false } else { let _b = 17; false }; println!("End"); ``` --- This lint will get masked by the `IF_SAME_THEN_ELSE` lint. I think it makes more sense to only emit one lint per if block. This means that the folloing example: ```rust if ... { let _a = 17; } else { let _a = 17; } ``` Will only trigger the `IF_SAME_THEN_ELSE` lint and not the `SHARED_CODE_IN_IF_BLOCKS` lint. --- closes: #5234 changelog: Added a new lint: `branches_sharing_code` And hello to the one that is writing the changelog for this release :D
bors
added a commit
that referenced
this issue
Apr 5, 2021
New Lint: `branches_sharing_code` This lint checks if all `if`-blocks contain some statements that are the same and can be moved out of the blocks to prevent code duplication. Here is an example: ```rust let _ = if ... { println!("Start"); // <-- Lint for code duplication let _a = 99; println!("End"); // <-- Lint for code duplication false } else { println!("Start"); let _b = 17; println!("End"); false }; ``` This could be written as: ```rust println!("Start"); let _ = if ... { let _a = 99; false } else { let _b = 17; false }; println!("End"); ``` --- This lint will get masked by the `IF_SAME_THEN_ELSE` lint. I think it makes more sense to only emit one lint per if block. This means that the folloing example: ```rust if ... { let _a = 17; } else { let _a = 17; } ``` Will only trigger the `IF_SAME_THEN_ELSE` lint and not the `SHARED_CODE_IN_IF_BLOCKS` lint. --- closes: #5234 changelog: Added a new lint: `branches_sharing_code` And hello to the one that is writing the changelog for this release :D
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-lint
Area: New lints
E-medium
Call for participation: Medium difficulty level problem and requires some initial experience.
L-complexity
Lint: Belongs in the complexity lint group
The lint would trigger if the
if
,else if
andelse
clasues have the same code in all branches.could be optimized to
playground
The text was updated successfully, but these errors were encountered: