Skip to content

Commit 20de4f2

Browse files
committed
Added branches_sharing_code fp test and note to lint doc rust-clippy#7452
1 parent 1326b78 commit 20de4f2

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

clippy_lints/src/copies.rs

Lines changed: 4 additions & 1 deletion
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

tests/ui/branches_sharing_code/false_positives.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,24 @@ impl FooBar {
2525
fn baz(&mut self) {}
2626
}
2727

28+
// #########################
29+
// # Issue 7452
30+
// #########################
31+
fn test() {
32+
let v = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
33+
let mut counter = 0;
34+
for i in v {
35+
if counter == 0 {
36+
counter += 1;
37+
println!("first");
38+
} else if counter == 1 {
39+
counter += 1;
40+
println!("second");
41+
} else {
42+
counter += 1;
43+
println!("other: {}", i);
44+
}
45+
}
46+
}
47+
2848
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: all if blocks contain the same code at the start
2+
--> $DIR/false_positives.rs:35:9
3+
|
4+
LL | / if counter == 0 {
5+
LL | | counter += 1;
6+
| |_________________________^
7+
|
8+
note: the lint level is defined here
9+
--> $DIR/false_positives.rs:2:36
10+
|
11+
LL | #![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
help: consider moving the start statements out like this
14+
|
15+
LL | counter += 1;
16+
LL | if counter == 0 {
17+
|
18+
19+
error: aborting due to previous error
20+

0 commit comments

Comments
 (0)