Skip to content

Commit

Permalink
Added branches_sharing_code fp test and note to lint doc `rust-clip…
Browse files Browse the repository at this point in the history
…py#7452`
  • Loading branch information
xFrednet committed Jul 13, 2021
1 parent 1326b78 commit 20de4f2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
5 changes: 4 additions & 1 deletion clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/branches_sharing_code/false_positives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
20 changes: 20 additions & 0 deletions tests/ui/branches_sharing_code/false_positives.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 20de4f2

Please sign in to comment.