Skip to content

Commit

Permalink
Auto merge of rust-lang#5843 - dima74:iter_skip_next.add-suggestion, …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
bors committed Jul 29, 2020
2 parents 73764ab + b375f1d commit 2e0f8b6
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
24 changes: 14 additions & 10 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
["nth", "iter_mut"] => lint_iter_nth(cx, expr, &arg_lists, true),
["nth", ..] => lint_iter_nth_zero(cx, expr, arg_lists[0]),
["step_by", ..] => lint_step_by(cx, expr, arg_lists[0]),
["next", "skip"] => lint_iter_skip_next(cx, expr),
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
["collect", "cloned"] => lint_iter_cloned_collect(cx, expr, arg_lists[1]),
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
Expand Down Expand Up @@ -2433,17 +2433,21 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
);
}

fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
// lint if caller of skip is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
span_lint_and_help(
cx,
ITER_SKIP_NEXT,
expr.span,
"called `skip(x).next()` on an iterator",
None,
"this is more succinctly expressed by calling `nth(x)`",
);
if let [caller, n] = skip_args {
let hint = format!(".nth({})", snippet(cx, n.span, ".."));
span_lint_and_sugg(
cx,
ITER_SKIP_NEXT,
expr.span.trim_start(caller.span).unwrap(),
"called `skip(x).next()` on an iterator",
"use `nth` instead",
hint,
Applicability::MachineApplicable,
);
}
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/ui/iter_skip_next.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// run-rustfix
// aux-build:option_helpers.rs

#![warn(clippy::iter_skip_next)]
#![allow(clippy::blacklisted_name)]
#![allow(clippy::iter_nth)]

extern crate option_helpers;

use option_helpers::IteratorFalsePositives;

/// Checks implementation of `ITER_SKIP_NEXT` lint
fn main() {
let some_vec = vec![0, 1, 2, 3];
let _ = some_vec.iter().nth(42);
let _ = some_vec.iter().cycle().nth(42);
let _ = (1..10).nth(10);
let _ = &some_vec[..].iter().nth(3);
let foo = IteratorFalsePositives { foo: 0 };
let _ = foo.skip(42).next();
let _ = foo.filter().skip(42).next();
}
8 changes: 4 additions & 4 deletions tests/ui/iter_skip_next.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// run-rustfix
// aux-build:option_helpers.rs

#![warn(clippy::iter_skip_next)]
#![allow(clippy::blacklisted_name)]
#![allow(clippy::iter_nth)]

extern crate option_helpers;

use option_helpers::IteratorFalsePositives;

/// Checks implementation of `ITER_SKIP_NEXT` lint
fn iter_skip_next() {
let mut some_vec = vec![0, 1, 2, 3];
fn main() {
let some_vec = vec![0, 1, 2, 3];
let _ = some_vec.iter().skip(42).next();
let _ = some_vec.iter().cycle().skip(42).next();
let _ = (1..10).skip(10).next();
Expand All @@ -18,5 +20,3 @@ fn iter_skip_next() {
let _ = foo.skip(42).next();
let _ = foo.filter().skip(42).next();
}

fn main() {}
23 changes: 8 additions & 15 deletions tests/ui/iter_skip_next.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
error: called `skip(x).next()` on an iterator
--> $DIR/iter_skip_next.rs:13:13
--> $DIR/iter_skip_next.rs:15:28
|
LL | let _ = some_vec.iter().skip(42).next();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^ help: use `nth` instead: `.nth(42)`
|
= note: `-D clippy::iter-skip-next` implied by `-D warnings`
= help: this is more succinctly expressed by calling `nth(x)`

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

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

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

error: aborting due to 4 previous errors

0 comments on commit 2e0f8b6

Please sign in to comment.