Skip to content

Commit 4028625

Browse files
committed
have a condition for has_break_or_return in WHILE_IMMUTABLE_CONDITION lint
1 parent 5a3ed18 commit 4028625

File tree

3 files changed

+60
-16
lines changed

3 files changed

+60
-16
lines changed

clippy_lints/src/loops.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -2374,13 +2374,20 @@ fn check_infinite_loop<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, cond: &'tcx Expr, e
23742374
has_break_or_return_visitor.visit_expr(expr);
23752375
let has_break_or_return = has_break_or_return_visitor.has_break_or_return;
23762376

2377-
if no_cond_variable_mutated && !mutable_static_in_cond && !has_break_or_return {
2378-
span_lint(
2377+
if no_cond_variable_mutated && !mutable_static_in_cond {
2378+
span_lint_and_then(
23792379
cx,
23802380
WHILE_IMMUTABLE_CONDITION,
23812381
cond.span,
2382-
"variables in the condition are not mutated in the loop body. \
2383-
This may lead to an infinite or to a never running loop",
2382+
"variables in the condition are not mutated in the loop body",
2383+
|db| {
2384+
db.note("This may lead to an infinite or to a never running loop");
2385+
2386+
if has_break_or_return {
2387+
db.note("this loop contains `return`s or `break`s");
2388+
db.help("rewrite it as `if cond { loop { } }`");
2389+
}
2390+
},
23842391
);
23852392
}
23862393
}

tests/ui/infinite_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ fn while_loop_with_break_and_return() {
183183
if y == 0 {
184184
break;
185185
}
186-
println!("OK - loop contains break");
186+
println!("KO - loop contains break");
187187
}
188188

189189
while y < 10 {
190190
if y == 0 {
191191
return;
192192
}
193-
println!("OK - loop contains return");
193+
println!("KO - loop contains return");
194194
}
195195
}
196196

tests/ui/infinite_loop.stderr

+47-10
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,95 @@
1-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
1+
error: variables in the condition are not mutated in the loop body
22
--> $DIR/infinite_loop.rs:23:11
33
|
44
LL | while y < 10 {
55
| ^^^^^^
66
|
77
= note: `#[deny(clippy::while_immutable_condition)]` on by default
8+
= note: This may lead to an infinite or to a never running loop
89

9-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
10+
error: variables in the condition are not mutated in the loop body
1011
--> $DIR/infinite_loop.rs:28:11
1112
|
1213
LL | while y < 10 && x < 3 {
1314
| ^^^^^^^^^^^^^^^
15+
|
16+
= note: This may lead to an infinite or to a never running loop
1417

15-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
18+
error: variables in the condition are not mutated in the loop body
1619
--> $DIR/infinite_loop.rs:35:11
1720
|
1821
LL | while !cond {
1922
| ^^^^^
23+
|
24+
= note: This may lead to an infinite or to a never running loop
2025

21-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
26+
error: variables in the condition are not mutated in the loop body
2227
--> $DIR/infinite_loop.rs:79:11
2328
|
2429
LL | while i < 3 {
2530
| ^^^^^
31+
|
32+
= note: This may lead to an infinite or to a never running loop
2633

27-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
34+
error: variables in the condition are not mutated in the loop body
2835
--> $DIR/infinite_loop.rs:84:11
2936
|
3037
LL | while i < 3 && j > 0 {
3138
| ^^^^^^^^^^^^^^
39+
|
40+
= note: This may lead to an infinite or to a never running loop
3241

33-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
42+
error: variables in the condition are not mutated in the loop body
3443
--> $DIR/infinite_loop.rs:88:11
3544
|
3645
LL | while i < 3 {
3746
| ^^^^^
47+
|
48+
= note: This may lead to an infinite or to a never running loop
3849

39-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
50+
error: variables in the condition are not mutated in the loop body
4051
--> $DIR/infinite_loop.rs:103:11
4152
|
4253
LL | while i < 3 {
4354
| ^^^^^
55+
|
56+
= note: This may lead to an infinite or to a never running loop
4457

45-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
58+
error: variables in the condition are not mutated in the loop body
4659
--> $DIR/infinite_loop.rs:108:11
4760
|
4861
LL | while i < 3 {
4962
| ^^^^^
63+
|
64+
= note: This may lead to an infinite or to a never running loop
5065

51-
error: variables in the condition are not mutated in the loop body. This may lead to an infinite or to a never running loop
66+
error: variables in the condition are not mutated in the loop body
5267
--> $DIR/infinite_loop.rs:174:15
5368
|
5469
LL | while self.count < n {
5570
| ^^^^^^^^^^^^^^
71+
|
72+
= note: This may lead to an infinite or to a never running loop
73+
74+
error: variables in the condition are not mutated in the loop body
75+
--> $DIR/infinite_loop.rs:182:11
76+
|
77+
LL | while y < 10 {
78+
| ^^^^^^
79+
|
80+
= note: This may lead to an infinite or to a never running loop
81+
= note: this loop contains `return`s or `break`s
82+
= help: rewrite it as `if cond { loop { } }`
83+
84+
error: variables in the condition are not mutated in the loop body
85+
--> $DIR/infinite_loop.rs:189:11
86+
|
87+
LL | while y < 10 {
88+
| ^^^^^^
89+
|
90+
= note: This may lead to an infinite or to a never running loop
91+
= note: this loop contains `return`s or `break`s
92+
= help: rewrite it as `if cond { loop { } }`
5693

57-
error: aborting due to 9 previous errors
94+
error: aborting due to 11 previous errors
5895

0 commit comments

Comments
 (0)