Skip to content

Commit adbdf75

Browse files
committed
Merge for_loop_over_option and for_loop_over_result lints into for_loop_over_fallible lint
1 parent 0e8be59 commit adbdf75

File tree

6 files changed

+46
-70
lines changed

6 files changed

+46
-70
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,8 +1361,7 @@ Released 2018-09-13
13611361
[`fn_to_numeric_cast`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast
13621362
[`fn_to_numeric_cast_with_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#fn_to_numeric_cast_with_truncation
13631363
[`for_kv_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_kv_map
1364-
[`for_loop_over_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loop_over_option
1365-
[`for_loop_over_result`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loop_over_result
1364+
[`for_loop_over_fallible`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loop_over_fallible
13661365
[`forget_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_copy
13671366
[`forget_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_ref
13681367
[`future_not_send`]: https://rust-lang.github.io/rust-clippy/master/index.html#future_not_send

clippy_lints/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
615615
&loops::EXPLICIT_INTO_ITER_LOOP,
616616
&loops::EXPLICIT_ITER_LOOP,
617617
&loops::FOR_KV_MAP,
618-
&loops::FOR_LOOP_OVER_OPTION,
619-
&loops::FOR_LOOP_OVER_RESULT,
618+
&loops::FOR_LOOP_OVER_FALLIBLE,
620619
&loops::ITER_NEXT_LOOP,
621620
&loops::MANUAL_MEMCPY,
622621
&loops::MUT_RANGE_BOUND,
@@ -1265,8 +1264,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
12651264
LintId::of(&loops::EMPTY_LOOP),
12661265
LintId::of(&loops::EXPLICIT_COUNTER_LOOP),
12671266
LintId::of(&loops::FOR_KV_MAP),
1268-
LintId::of(&loops::FOR_LOOP_OVER_OPTION),
1269-
LintId::of(&loops::FOR_LOOP_OVER_RESULT),
1267+
LintId::of(&loops::FOR_LOOP_OVER_FALLIBLE),
12701268
LintId::of(&loops::ITER_NEXT_LOOP),
12711269
LintId::of(&loops::MANUAL_MEMCPY),
12721270
LintId::of(&loops::MUT_RANGE_BOUND),
@@ -1641,8 +1639,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16411639
LintId::of(&inline_fn_without_body::INLINE_FN_WITHOUT_BODY),
16421640
LintId::of(&let_underscore::LET_UNDERSCORE_LOCK),
16431641
LintId::of(&literal_representation::MISTYPED_LITERAL_SUFFIXES),
1644-
LintId::of(&loops::FOR_LOOP_OVER_OPTION),
1645-
LintId::of(&loops::FOR_LOOP_OVER_RESULT),
1642+
LintId::of(&loops::FOR_LOOP_OVER_FALLIBLE),
16461643
LintId::of(&loops::ITER_NEXT_LOOP),
16471644
LintId::of(&loops::NEVER_LOOP),
16481645
LintId::of(&loops::WHILE_IMMUTABLE_CONDITION),

clippy_lints/src/loops.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -168,55 +168,46 @@ declare_clippy_lint! {
168168
}
169169

170170
declare_clippy_lint! {
171-
/// **What it does:** Checks for `for` loops over `Option` values.
171+
/// **What it does:** Checks for `for` loops over `Option` or `Result` values.
172172
///
173173
/// **Why is this bad?** Readability. This is more clearly expressed as an `if
174174
/// let`.
175175
///
176176
/// **Known problems:** None.
177177
///
178178
/// **Example:**
179-
/// ```ignore
180-
/// for x in option {
181-
/// ..
179+
/// ```rust
180+
/// # let opt = Some(1);
181+
///
182+
/// // Bad
183+
/// for x in opt {
184+
/// // ..
182185
/// }
183-
/// ```
184186
///
185-
/// This should be
186-
/// ```ignore
187-
/// if let Some(x) = option {
188-
/// ..
187+
/// // Good
188+
/// if let Some(x) = opt {
189+
/// // ..
189190
/// }
190191
/// ```
191-
pub FOR_LOOP_OVER_OPTION,
192-
correctness,
193-
"for-looping over an `Option`, which is more clearly expressed as an `if let`"
194-
}
195-
196-
declare_clippy_lint! {
197-
/// **What it does:** Checks for `for` loops over `Result` values.
198192
///
199-
/// **Why is this bad?** Readability. This is more clearly expressed as an `if
200-
/// let`.
193+
/// // or
201194
///
202-
/// **Known problems:** None.
195+
/// ```rust
196+
/// # let res: Result<i32, std::io::Error> = Ok(1);
203197
///
204-
/// **Example:**
205-
/// ```ignore
206-
/// for x in result {
207-
/// ..
198+
/// // Bad
199+
/// for x in &res {
200+
/// // ..
208201
/// }
209-
/// ```
210202
///
211-
/// This should be
212-
/// ```ignore
213-
/// if let Ok(x) = result {
214-
/// ..
203+
/// // Good
204+
/// if let Ok(x) = res {
205+
/// // ..
215206
/// }
216207
/// ```
217-
pub FOR_LOOP_OVER_RESULT,
208+
pub FOR_LOOP_OVER_FALLIBLE,
218209
correctness,
219-
"for-looping over a `Result`, which is more clearly expressed as an `if let`"
210+
"for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`"
220211
}
221212

222213
declare_clippy_lint! {
@@ -435,8 +426,7 @@ declare_lint_pass!(Loops => [
435426
EXPLICIT_ITER_LOOP,
436427
EXPLICIT_INTO_ITER_LOOP,
437428
ITER_NEXT_LOOP,
438-
FOR_LOOP_OVER_RESULT,
439-
FOR_LOOP_OVER_OPTION,
429+
FOR_LOOP_OVER_FALLIBLE,
440430
WHILE_LET_LOOP,
441431
NEEDLESS_COLLECT,
442432
EXPLICIT_COUNTER_LOOP,
@@ -1283,7 +1273,7 @@ fn check_for_loop_arg(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>, e
12831273
ITER_NEXT_LOOP,
12841274
expr.span,
12851275
"you are iterating over `Iterator::next()` which is an Option; this will compile but is \
1286-
probably not what you want",
1276+
probably not what you want",
12871277
);
12881278
next_loop_linted = true;
12891279
}
@@ -1300,11 +1290,11 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
13001290
if is_type_diagnostic_item(cx, ty, sym!(option_type)) {
13011291
span_lint_and_help(
13021292
cx,
1303-
FOR_LOOP_OVER_OPTION,
1293+
FOR_LOOP_OVER_FALLIBLE,
13041294
arg.span,
13051295
&format!(
13061296
"for loop over `{0}`, which is an `Option`. This is more readably written as an \
1307-
`if let` statement.",
1297+
`if let` statement.",
13081298
snippet(cx, arg.span, "_")
13091299
),
13101300
None,
@@ -1317,11 +1307,11 @@ fn check_arg_type(cx: &LateContext<'_, '_>, pat: &Pat<'_>, arg: &Expr<'_>) {
13171307
} else if is_type_diagnostic_item(cx, ty, sym!(result_type)) {
13181308
span_lint_and_help(
13191309
cx,
1320-
FOR_LOOP_OVER_RESULT,
1310+
FOR_LOOP_OVER_FALLIBLE,
13211311
arg.span,
13221312
&format!(
13231313
"for loop over `{0}`, which is a `Result`. This is more readably written as an \
1324-
`if let` statement.",
1314+
`if let` statement.",
13251315
snippet(cx, arg.span, "_")
13261316
),
13271317
None,

src/lintlist/mod.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -676,16 +676,9 @@ pub static ref ALL_LINTS: Vec<Lint> = vec![
676676
module: "loops",
677677
},
678678
Lint {
679-
name: "for_loop_over_option",
679+
name: "for_loop_over_fallible",
680680
group: "correctness",
681-
desc: "for-looping over an `Option`, which is more clearly expressed as an `if let`",
682-
deprecation: None,
683-
module: "loops",
684-
},
685-
Lint {
686-
name: "for_loop_over_result",
687-
group: "correctness",
688-
desc: "for-looping over a `Result`, which is more clearly expressed as an `if let`",
681+
desc: "for-looping over an `Option` or a `Result`, which is more clearly expressed as an `if let`",
689682
deprecation: None,
690683
module: "loops",
691684
},

tests/ui/for_loop_over_option_result.rs renamed to tests/ui/for_loop_over_fallible.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
#![warn(clippy::for_loop_over_option, clippy::for_loop_over_result)]
1+
#![warn(clippy::for_loop_over_fallible)]
22

3-
/// Tests for_loop_over_result and for_loop_over_option
4-
5-
fn for_loop_over_option_and_result() {
3+
fn for_loop_over_fallible() {
64
let option = Some(1);
75
let result = option.ok_or("x not found");
86
let v = vec![0, 1, 2];
97

10-
// check FOR_LOOP_OVER_OPTION lint
8+
// check over an `Option`
119
for x in option {
1210
println!("{}", x);
1311
}
1412

15-
// check FOR_LOOP_OVER_RESULT lint
13+
// check over a `Result`
1614
for x in result {
1715
println!("{}", x);
1816
}

tests/ui/for_loop_over_option_result.stderr renamed to tests/ui/for_loop_over_fallible.stderr

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,54 @@
11
error: for loop over `option`, which is an `Option`. This is more readably written as an `if let` statement.
2-
--> $DIR/for_loop_over_option_result.rs:11:14
2+
--> $DIR/for_loop_over_fallible.rs:9:14
33
|
44
LL | for x in option {
55
| ^^^^^^
66
|
7-
= note: `-D clippy::for-loop-over-option` implied by `-D warnings`
7+
= note: `-D clippy::for-loop-over-fallible` implied by `-D warnings`
88
= help: consider replacing `for x in option` with `if let Some(x) = option`
99

1010
error: for loop over `result`, which is a `Result`. This is more readably written as an `if let` statement.
11-
--> $DIR/for_loop_over_option_result.rs:16:14
11+
--> $DIR/for_loop_over_fallible.rs:14:14
1212
|
1313
LL | for x in result {
1414
| ^^^^^^
1515
|
16-
= note: `-D clippy::for-loop-over-result` implied by `-D warnings`
1716
= help: consider replacing `for x in result` with `if let Ok(x) = result`
1817

1918
error: for loop over `option.ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
20-
--> $DIR/for_loop_over_option_result.rs:20:14
19+
--> $DIR/for_loop_over_fallible.rs:18:14
2120
|
2221
LL | for x in option.ok_or("x not found") {
2322
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
2423
|
2524
= help: consider replacing `for x in option.ok_or("x not found")` with `if let Ok(x) = option.ok_or("x not found")`
2625

2726
error: you are iterating over `Iterator::next()` which is an Option; this will compile but is probably not what you want
28-
--> $DIR/for_loop_over_option_result.rs:26:14
27+
--> $DIR/for_loop_over_fallible.rs:24:14
2928
|
3029
LL | for x in v.iter().next() {
3130
| ^^^^^^^^^^^^^^^
3231
|
3332
= note: `#[deny(clippy::iter_next_loop)]` on by default
3433

3534
error: for loop over `v.iter().next().and(Some(0))`, which is an `Option`. This is more readably written as an `if let` statement.
36-
--> $DIR/for_loop_over_option_result.rs:31:14
35+
--> $DIR/for_loop_over_fallible.rs:29:14
3736
|
3837
LL | for x in v.iter().next().and(Some(0)) {
3938
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4039
|
4140
= help: consider replacing `for x in v.iter().next().and(Some(0))` with `if let Some(x) = v.iter().next().and(Some(0))`
4241

4342
error: for loop over `v.iter().next().ok_or("x not found")`, which is a `Result`. This is more readably written as an `if let` statement.
44-
--> $DIR/for_loop_over_option_result.rs:35:14
43+
--> $DIR/for_loop_over_fallible.rs:33:14
4544
|
4645
LL | for x in v.iter().next().ok_or("x not found") {
4746
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4847
|
4948
= help: consider replacing `for x in v.iter().next().ok_or("x not found")` with `if let Ok(x) = v.iter().next().ok_or("x not found")`
5049

5150
error: this loop never actually loops
52-
--> $DIR/for_loop_over_option_result.rs:47:5
51+
--> $DIR/for_loop_over_fallible.rs:45:5
5352
|
5453
LL | / while let Some(x) = option {
5554
LL | | println!("{}", x);
@@ -60,7 +59,7 @@ LL | | }
6059
= note: `#[deny(clippy::never_loop)]` on by default
6160

6261
error: this loop never actually loops
63-
--> $DIR/for_loop_over_option_result.rs:53:5
62+
--> $DIR/for_loop_over_fallible.rs:51:5
6463
|
6564
LL | / while let Ok(x) = result {
6665
LL | | println!("{}", x);

0 commit comments

Comments
 (0)