Skip to content

Commit 2e0f8b6

Browse files
committed
Auto merge of #5843 - dima74:iter_skip_next.add-suggestion, r=phansch
Add suggestion for `iter_skip_next` lint changelog: Add suggestion for [`iter_skip_next`](https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next) lint
2 parents 73764ab + b375f1d commit 2e0f8b6

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

clippy_lints/src/methods/mod.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
14081408
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
14091409
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
14101410
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
1411-
["next", "skip"] => lint_iter_skip_next(cx, expr),
1411+
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
14121412
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
14131413
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
14141414
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
@@ -2433,17 +2433,21 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
24332433
);
24342434
}
24352435

2436-
fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
2436+
fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
24372437
// lint if caller of skip is an Iterator
24382438
if match_trait_method(cx, expr, &paths::ITERATOR) {
2439-
span_lint_and_help(
2440-
cx,
2441-
ITER_SKIP_NEXT,
2442-
expr.span,
2443-
"called `skip(x).next()` on an iterator",
2444-
None,
2445-
"this is more succinctly expressed by calling `nth(x)`",
2446-
);
2439+
if let [caller, n] = skip_args {
2440+
let hint = format!(".nth({})", snippet(cx, n.span, ".."));
2441+
span_lint_and_sugg(
2442+
cx,
2443+
ITER_SKIP_NEXT,
2444+
expr.span.trim_start(caller.span).unwrap(),
2445+
"called `skip(x).next()` on an iterator",
2446+
"use `nth` instead",
2447+
hint,
2448+
Applicability::MachineApplicable,
2449+
);
2450+
}
24472451
}
24482452
}
24492453

tests/ui/iter_skip_next.fixed

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run-rustfix
2+
// aux-build:option_helpers.rs
3+
4+
#![warn(clippy::iter_skip_next)]
5+
#![allow(clippy::blacklisted_name)]
6+
#![allow(clippy::iter_nth)]
7+
8+
extern crate option_helpers;
9+
10+
use option_helpers::IteratorFalsePositives;
11+
12+
/// Checks implementation of `ITER_SKIP_NEXT` lint
13+
fn main() {
14+
let some_vec = vec![0, 1, 2, 3];
15+
let _ = some_vec.iter().nth(42);
16+
let _ = some_vec.iter().cycle().nth(42);
17+
let _ = (1..10).nth(10);
18+
let _ = &some_vec[..].iter().nth(3);
19+
let foo = IteratorFalsePositives { foo: 0 };
20+
let _ = foo.skip(42).next();
21+
let _ = foo.filter().skip(42).next();
22+
}

tests/ui/iter_skip_next.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
// run-rustfix
12
// aux-build:option_helpers.rs
23

34
#![warn(clippy::iter_skip_next)]
45
#![allow(clippy::blacklisted_name)]
6+
#![allow(clippy::iter_nth)]
57

68
extern crate option_helpers;
79

810
use option_helpers::IteratorFalsePositives;
911

1012
/// Checks implementation of `ITER_SKIP_NEXT` lint
11-
fn iter_skip_next() {
12-
let mut some_vec = vec![0, 1, 2, 3];
13+
fn main() {
14+
let some_vec = vec![0, 1, 2, 3];
1315
let _ = some_vec.iter().skip(42).next();
1416
let _ = some_vec.iter().cycle().skip(42).next();
1517
let _ = (1..10).skip(10).next();
@@ -18,5 +20,3 @@ fn iter_skip_next() {
1820
let _ = foo.skip(42).next();
1921
let _ = foo.filter().skip(42).next();
2022
}
21-
22-
fn main() {}

tests/ui/iter_skip_next.stderr

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,28 @@
11
error: called `skip(x).next()` on an iterator
2-
--> $DIR/iter_skip_next.rs:13:13
2+
--> $DIR/iter_skip_next.rs:15:28
33
|
44
LL | let _ = some_vec.iter().skip(42).next();
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
66
|
77
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
8-
= help: this is more succinctly expressed by calling `nth(x)`
98

109
error: called `skip(x).next()` on an iterator
11-
--> $DIR/iter_skip_next.rs:14:13
10+
--> $DIR/iter_skip_next.rs:16:36
1211
|
1312
LL | let _ = some_vec.iter().cycle().skip(42).next();
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15-
|
16-
= help: this is more succinctly expressed by calling `nth(x)`
13+
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
1714

1815
error: called `skip(x).next()` on an iterator
19-
--> $DIR/iter_skip_next.rs:15:13
16+
--> $DIR/iter_skip_next.rs:17:20
2017
|
2118
LL | let _ = (1..10).skip(10).next();
22-
| ^^^^^^^^^^^^^^^^^^^^^^^
23-
|
24-
= help: this is more succinctly expressed by calling `nth(x)`
19+
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(10)`
2520

2621
error: called `skip(x).next()` on an iterator
27-
--> $DIR/iter_skip_next.rs:16:14
22+
--> $DIR/iter_skip_next.rs:18:33
2823
|
2924
LL | let _ = &some_vec[..].iter().skip(3).next();
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31-
|
32-
= help: this is more succinctly expressed by calling `nth(x)`
25+
| ^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(3)`
3326

3427
error: aborting due to 4 previous errors
3528

0 commit comments

Comments
 (0)