diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 1176430dd7fe..49aef26c4a81 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 diff --git a/tests/ui/branches_sharing_code/false_positives.rs b/tests/ui/branches_sharing_code/false_positives.rs index 7f42df463411..1501263324d4 100644 --- a/tests/ui/branches_sharing_code/false_positives.rs +++ b/tests/ui/branches_sharing_code/false_positives.rs @@ -25,4 +25,24 @@ impl FooBar { fn baz(&mut self) {} } +// ######################### +// # Issue 7452 +// ######################### +fn test() { + let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; + let mut counter = 0; + for i in v { + if counter == 0 { + counter += 1; + println!("first"); + } else if counter == 1 { + counter += 1; + println!("second"); + } else { + counter += 1; + println!("other: {}", i); + } + } +} + fn main() {} diff --git a/tests/ui/branches_sharing_code/false_positives.stderr b/tests/ui/branches_sharing_code/false_positives.stderr new file mode 100644 index 000000000000..26006c56e59e --- /dev/null +++ b/tests/ui/branches_sharing_code/false_positives.stderr @@ -0,0 +1,20 @@ +error: all if blocks contain the same code at the start + --> $DIR/false_positives.rs:35:9 + | +LL | / if counter == 0 { +LL | | counter += 1; + | |_________________________^ + | +note: the lint level is defined here + --> $DIR/false_positives.rs:2:36 + | +LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +help: consider moving the start statements out like this + | +LL | counter += 1; +LL | if counter == 0 { + | + +error: aborting due to previous error +