Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d45f6db
Move some tests to more reasonable directories
c410-f3r Sep 20, 2022
8b9a1f1
Remove tuple candidate, nothing special about it
compiler-errors Oct 7, 2022
293f662
Make tests capture the error printed by a Result return
dtolnay Oct 7, 2022
fa380a8
Start uplifting `clippy::for_loops_over_fallibles`
WaffleLapkin Jul 17, 2022
d030ba5
Use structured suggestions for `for_loop_over_fallibles` lint
WaffleLapkin Jul 18, 2022
21ec99b
`for_loop_over_fallibles`: Suggest removing `.next()`
WaffleLapkin Jul 24, 2022
5dcfdbf
`for_loop_over_fallibles`: suggest `while let` loop
WaffleLapkin Jul 24, 2022
b2975ee
`for_loop_over_fallibles`: suggest using `?` in some cases
WaffleLapkin Jul 24, 2022
dd842ff
`for_loop_over_fallibles`: remove duplication from the message
WaffleLapkin Jul 24, 2022
7308564
Add a test for the `for_loop_over_fallibles` lint
WaffleLapkin Jul 24, 2022
23a7674
`for_loop_over_fallibles`: fix suggestion for "remove `.next()`" case
WaffleLapkin Jul 24, 2022
8ca57b5
`for_loop_over_fallibles`: don't use `MachineApplicable`
WaffleLapkin Jul 24, 2022
0250f02
allow or avoid for loops over option in compiler and tests
WaffleLapkin Jul 26, 2022
75ae20a
allow `for_loop_over_fallibles` in a `core` test
WaffleLapkin Jul 26, 2022
b9b2059
Edit documentation for `for_loop_over_fallibles` lint
WaffleLapkin Aug 14, 2022
6766113
remove an infinite loop
WaffleLapkin Aug 15, 2022
98e0c4d
fix `for_loop_over_fallibles` lint docs
WaffleLapkin Aug 18, 2022
9c64bb1
Fix clippy tests that trigger `for_loop_over_fallibles` lint
WaffleLapkin Aug 25, 2022
7434b9f
fixup lint name
WaffleLapkin Oct 7, 2022
40f36fa
adopt to new rustc lint api
WaffleLapkin Oct 7, 2022
5347c81
deprecate `clippy::for_loops_over_fallibles`
WaffleLapkin Oct 7, 2022
9d4edff
adopt to building infcx
WaffleLapkin Oct 7, 2022
e828ce5
Skip chained OpaqueCast when building captures.
cjgillot Oct 9, 2022
d3bd6be
Rename AssocItemKind::TyAlias to AssocItemKind::Type
compiler-errors Oct 10, 2022
7e16f9f
Rollup merge of #99696 - WaffleLapkin:uplift, r=fee1-dead
Dylan-DPC Oct 10, 2022
0c17324
Rollup merge of #102055 - c410-f3r:moar-errors, r=petrochenkov
Dylan-DPC Oct 10, 2022
58d533d
Rollup merge of #102786 - compiler-errors:no-tuple-candidate, r=lcnr
Dylan-DPC Oct 10, 2022
302bf31
Rollup merge of #102794 - dtolnay:termination, r=thomcc
Dylan-DPC Oct 10, 2022
5a09b72
Rollup merge of #102853 - cjgillot:skip-opaque-cast, r=jackh726
Dylan-DPC Oct 10, 2022
81b9d0b
Rollup merge of #102868 - compiler-errors:rename-assoc-tyalias-to-ty,…
Dylan-DPC Oct 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a test for the for_loop_over_fallibles lint
  • Loading branch information
WaffleLapkin committed Oct 9, 2022
commit 730856442387c9804581eae5927a5667171022ca
43 changes: 43 additions & 0 deletions src/test/ui/lint/for_loop_over_fallibles.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// check-pass

fn main() {
// Common
for _ in Some(1) {}
//~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent
for _ in Ok::<_, ()>(1) {}
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent

// `Iterator::next` specific
for _ in [0; 0].iter().next() {}
//~^ WARN for loop over an `Option`. This is more readably written as an `if let` statement
//~| HELP to iterate over `[0; 0].iter()` remove the call to `next`
//~| HELP consider using `if let` to clear intent

// `Result<impl Iterator, _>`, but function doesn't return `Result`
for _ in Ok::<_, ()>([0; 0].iter()) {}
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider using `if let` to clear intent
}

fn _returns_result() -> Result<(), ()> {
// `Result<impl Iterator, _>`
for _ in Ok::<_, ()>([0; 0].iter()) {}
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
//~| HELP consider using `if let` to clear intent

// `Result<impl IntoIterator>`
for _ in Ok::<_, ()>([0; 0]) {}
//~^ WARN for loop over a `Result`. This is more readably written as an `if let` statement
//~| HELP to check pattern in a loop use `while let`
//~| HELP consider unwrapping the `Result` with `?` to iterate over its contents
//~| HELP consider using `if let` to clear intent

Ok(())
}
102 changes: 102 additions & 0 deletions src/test/ui/lint/for_loop_over_fallibles.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
warning: for loop over an `Option`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:5:14
|
LL | for _ in Some(1) {}
| ^^^^^^^
|
= note: `#[warn(for_loop_over_fallibles)]` on by default
help: to check pattern in a loop use `while let`
|
LL | while let Some(_) = Some(1) {}
| ~~~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Some(_) = Some(1) {}
| ~~~~~~~~~~~~ ~~~

warning: for loop over a `Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:9:14
|
LL | for _ in Ok::<_, ()>(1) {}
| ^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = Ok::<_, ()>(1) {}
| ~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = Ok::<_, ()>(1) {}
| ~~~~~~~~~~ ~~~

warning: for loop over an `Option`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:15:14
|
LL | for _ in [0; 0].iter().next() {}
| ^^^^^^^^^^^^^^^^^^^^
|
help: to iterate over `[0; 0].iter()` remove the call to `next`
|
LL - for _ in [0; 0].iter().next() {}
LL + for _ in [0; 0].iter() {}
|
help: consider using `if let` to clear intent
|
LL | if let Some(_) = [0; 0].iter().next() {}
| ~~~~~~~~~~~~ ~~~

warning: for loop over a `Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:21:14
|
LL | for _ in Ok::<_, ()>([0; 0].iter()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
| ~~~~~~~~~~~~~ ~~~
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
| ~~~~~~~~~~ ~~~

warning: for loop over a `Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:29:14
|
LL | for _ in Ok::<_, ()>([0; 0].iter()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
| ~~~~~~~~~~~~~ ~~~
help: consider unwrapping the `Result` with `?` to iterate over its contents
|
LL | for _ in Ok::<_, ()>([0; 0].iter())? {}
| +
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = Ok::<_, ()>([0; 0].iter()) {}
| ~~~~~~~~~~ ~~~

warning: for loop over a `Result`. This is more readably written as an `if let` statement
--> $DIR/for_loop_over_fallibles.rs:36:14
|
LL | for _ in Ok::<_, ()>([0; 0]) {}
| ^^^^^^^^^^^^^^^^^^^
|
help: to check pattern in a loop use `while let`
|
LL | while let Ok(_) = Ok::<_, ()>([0; 0]) {}
| ~~~~~~~~~~~~~ ~~~
help: consider unwrapping the `Result` with `?` to iterate over its contents
|
LL | for _ in Ok::<_, ()>([0; 0])? {}
| +
help: consider using `if let` to clear intent
|
LL | if let Ok(_) = Ok::<_, ()>([0; 0]) {}
| ~~~~~~~~~~ ~~~

warning: 6 warnings emitted