Skip to content

Commit ea549fd

Browse files
committed
Add tests for 'Also apply warn(for_loops_over_fallibles) to &T and &mut T, not just T = Result/Option.'
1 parent 77f288c commit ea549fd

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

tests/ui/lint/for_loop_over_fallibles.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,25 @@ fn _returns_result() -> Result<(), ()> {
4141

4242
Ok(())
4343
}
44+
45+
fn _by_ref() {
46+
// Shared refs
47+
for _ in &Some(1) {}
48+
//~^ WARN for loop over an `&Option`. This is more readably written as an `if let` statement
49+
//~| HELP to check pattern in a loop use `while let`
50+
//~| HELP consider using `if let` to clear intent
51+
for _ in &Ok::<_, ()>(1) {}
52+
//~^ WARN for loop over a `&Result`. This is more readably written as an `if let` statement
53+
//~| HELP to check pattern in a loop use `while let`
54+
//~| HELP consider using `if let` to clear intent
55+
56+
// Mutable refs
57+
for _ in &mut Some(1) {}
58+
//~^ WARN for loop over an `&mut Option`. This is more readably written as an `if let` statement
59+
//~| HELP to check pattern in a loop use `while let`
60+
//~| HELP consider using `if let` to clear intent
61+
for _ in &mut Ok::<_, ()>(1) {}
62+
//~^ WARN for loop over a `&mut Result`. This is more readably written as an `if let` statement
63+
//~| HELP to check pattern in a loop use `while let`
64+
//~| HELP consider using `if let` to clear intent
65+
}

tests/ui/lint/for_loop_over_fallibles.stderr

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,65 @@ help: consider using `if let` to clear intent
9797
LL | if let Ok(_) = Ok::<_, ()>([0; 0]) {}
9898
| ~~~~~~~~~~ ~~~
9999

100-
warning: 6 warnings emitted
100+
warning: for loop over an `&Option`. This is more readably written as an `if let` statement
101+
--> $DIR/for_loop_over_fallibles.rs:47:14
102+
|
103+
LL | for _ in &Some(1) {}
104+
| ^^^^^^^^
105+
|
106+
help: to check pattern in a loop use `while let`
107+
|
108+
LL | while let Some(_) = &Some(1) {}
109+
| ~~~~~~~~~~~~~~~ ~~~
110+
help: consider using `if let` to clear intent
111+
|
112+
LL | if let Some(_) = &Some(1) {}
113+
| ~~~~~~~~~~~~ ~~~
114+
115+
warning: for loop over a `&Result`. This is more readably written as an `if let` statement
116+
--> $DIR/for_loop_over_fallibles.rs:51:14
117+
|
118+
LL | for _ in &Ok::<_, ()>(1) {}
119+
| ^^^^^^^^^^^^^^^
120+
|
121+
help: to check pattern in a loop use `while let`
122+
|
123+
LL | while let Ok(_) = &Ok::<_, ()>(1) {}
124+
| ~~~~~~~~~~~~~ ~~~
125+
help: consider using `if let` to clear intent
126+
|
127+
LL | if let Ok(_) = &Ok::<_, ()>(1) {}
128+
| ~~~~~~~~~~ ~~~
129+
130+
warning: for loop over an `&mut Option`. This is more readably written as an `if let` statement
131+
--> $DIR/for_loop_over_fallibles.rs:57:14
132+
|
133+
LL | for _ in &mut Some(1) {}
134+
| ^^^^^^^^^^^^
135+
|
136+
help: to check pattern in a loop use `while let`
137+
|
138+
LL | while let Some(_) = &mut Some(1) {}
139+
| ~~~~~~~~~~~~~~~ ~~~
140+
help: consider using `if let` to clear intent
141+
|
142+
LL | if let Some(_) = &mut Some(1) {}
143+
| ~~~~~~~~~~~~ ~~~
144+
145+
warning: for loop over a `&mut Result`. This is more readably written as an `if let` statement
146+
--> $DIR/for_loop_over_fallibles.rs:61:14
147+
|
148+
LL | for _ in &mut Ok::<_, ()>(1) {}
149+
| ^^^^^^^^^^^^^^^^^^^
150+
|
151+
help: to check pattern in a loop use `while let`
152+
|
153+
LL | while let Ok(_) = &mut Ok::<_, ()>(1) {}
154+
| ~~~~~~~~~~~~~ ~~~
155+
help: consider using `if let` to clear intent
156+
|
157+
LL | if let Ok(_) = &mut Ok::<_, ()>(1) {}
158+
| ~~~~~~~~~~ ~~~
159+
160+
warning: 10 warnings emitted
101161

0 commit comments

Comments
 (0)