From 61e280863fb68c26bb1f976acfa4a45a26d70545 Mon Sep 17 00:00:00 2001 From: xFrednet Date: Tue, 13 Jul 2021 23:27:19 +0200 Subject: [PATCH] Fixed `branches_sharing_code` FP with block expressions in else And added `branches_sharing_code` PF note to lint doc for `rust-clippy#7452` --- clippy_lints/src/copies.rs | 8 ++++-- .../branches_sharing_code/false_positives.rs | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 tests/ui/branches_sharing_code/false_positives.rs diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index aea1accccc6..9cbcde59768 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -120,7 +120,10 @@ declare_clippy_lint! { /// /// **Why is this bad?** Duplicate code is less maintainable. /// - /// **Known problems:** Hopefully none. + /// **Known problems:** + /// * The lint doesn't check if the moved expressions modify values that are beeing used in + /// the if condition. The suggestion can in that case modify the behavior of the program. + /// See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452) /// /// **Example:** /// ```ignore @@ -358,8 +361,7 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option< expr_eq &= block_expr_eq; } - let has_expr = blocks[0].expr.is_some(); - if has_expr && !expr_eq { + if !expr_eq { end_eq = 0; } diff --git a/tests/ui/branches_sharing_code/false_positives.rs b/tests/ui/branches_sharing_code/false_positives.rs new file mode 100644 index 00000000000..7f42df46341 --- /dev/null +++ b/tests/ui/branches_sharing_code/false_positives.rs @@ -0,0 +1,28 @@ +#![allow(dead_code)] +#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + +// ################################## +// # Issue clippy#7369 +// ################################## +#[derive(Debug)] +pub struct FooBar { + foo: Vec, +} + +impl FooBar { + pub fn bar(&mut self) { + if true { + self.foo.pop(); + } else { + self.baz(); + + self.foo.pop(); + + self.baz() + } + } + + fn baz(&mut self) {} +} + +fn main() {}