Skip to content

Commit 2b193e2

Browse files
committed
Auto merge of rust-lang#7462 - xFrednet:7369-branches-sharing-code-else-expr-fp, r=camsteffen
FP fix and documentation for `branches_sharing_code` lint Closes rust-lang/rust-clippy#7369 Related rust-lang/rust-clippy#7452 I'm still thinking about the best way to fix this. I could simply add another visitor to ensure that the moved expressions don't modify values being used in the condition, but I'm not totally happy with this due to the complexity. I therefore only documented it for now changelog: [`branches_sharing_code`] fixed false positive where block expressions would sometimes be ignored.
2 parents 4acbff9 + 61e2808 commit 2b193e2

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

clippy_lints/src/copies.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ declare_clippy_lint! {
120120
///
121121
/// **Why is this bad?** Duplicate code is less maintainable.
122122
///
123-
/// **Known problems:** Hopefully none.
123+
/// **Known problems:**
124+
/// * The lint doesn't check if the moved expressions modify values that are beeing used in
125+
/// the if condition. The suggestion can in that case modify the behavior of the program.
126+
/// See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452)
124127
///
125128
/// **Example:**
126129
/// ```ignore
@@ -358,8 +361,7 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option<
358361
expr_eq &= block_expr_eq;
359362
}
360363

361-
let has_expr = blocks[0].expr.is_some();
362-
if has_expr && !expr_eq {
364+
if !expr_eq {
363365
end_eq = 0;
364366
}
365367

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![allow(dead_code)]
2+
#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
3+
4+
// ##################################
5+
// # Issue clippy#7369
6+
// ##################################
7+
#[derive(Debug)]
8+
pub struct FooBar {
9+
foo: Vec<u32>,
10+
}
11+
12+
impl FooBar {
13+
pub fn bar(&mut self) {
14+
if true {
15+
self.foo.pop();
16+
} else {
17+
self.baz();
18+
19+
self.foo.pop();
20+
21+
self.baz()
22+
}
23+
}
24+
25+
fn baz(&mut self) {}
26+
}
27+
28+
fn main() {}

0 commit comments

Comments
 (0)