Commit d91da40
committed
Auto merge of rust-lang#6463 - xFrednet:5234-shared-code-in-if-blocks, r=phansch
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: rust-lang#5234
changelog: Added a new lint: `branches_sharing_code`
And hello to the one that is writing the changelog for this release :DFile tree
31 files changed
+1785
-240
lines changed- clippy_lints/src
- methods
- clippy_utils/src
- tests/ui
- branches_sharing_code
- checked_unwrap
- needless_bool
31 files changed
+1785
-240
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2129 | 2129 | | |
2130 | 2130 | | |
2131 | 2131 | | |
| 2132 | + | |
2132 | 2133 | | |
2133 | 2134 | | |
2134 | 2135 | | |
| |||
0 commit comments