Skip to content

Commit 171c4c1

Browse files
committed
move iter_skip_next to its own module
1 parent 24909fa commit 171c4c1

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use crate::utils::{match_trait_method, paths, snippet, span_lint_and_sugg};
2+
use rustc_errors::Applicability;
3+
use rustc_hir as hir;
4+
use rustc_lint::LateContext;
5+
6+
use super::ITER_SKIP_NEXT;
7+
8+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
9+
// lint if caller of skip is an Iterator
10+
if match_trait_method(cx, expr, &paths::ITERATOR) {
11+
if let [caller, n] = skip_args {
12+
let hint = format!(".nth({})", snippet(cx, n.span, ".."));
13+
span_lint_and_sugg(
14+
cx,
15+
ITER_SKIP_NEXT,
16+
expr.span.trim_start(caller.span).unwrap(),
17+
"called `skip(..).next()` on an iterator",
18+
"use `nth` instead",
19+
hint,
20+
Applicability::MachineApplicable,
21+
);
22+
}
23+
}
24+
}

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ mod iter_count;
2222
mod iter_next_slice;
2323
mod iter_nth;
2424
mod iter_nth_zero;
25+
mod iter_skip_next;
2526
mod iterator_step_by_zero;
2627
mod manual_saturating_arithmetic;
2728
mod map_collect_result_unit;
@@ -1730,7 +1731,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17301731
["nth", "bytes"] => bytes_nth::check(cx, expr, &arg_lists[1]),
17311732
["nth", ..] => iter_nth_zero::check(cx, expr, arg_lists[0]),
17321733
["step_by", ..] => iterator_step_by_zero::check(cx, expr, arg_lists[0]),
1733-
["next", "skip"] => lint_iter_skip_next(cx, expr, arg_lists[1]),
1734+
["next", "skip"] => iter_skip_next::check(cx, expr, arg_lists[1]),
17341735
["collect", "cloned"] => iter_cloned_collect::check(cx, expr, arg_lists[1]),
17351736
["as_ref"] => lint_asref(cx, expr, "as_ref", arg_lists[0]),
17361737
["as_mut"] => lint_asref(cx, expr, "as_mut", arg_lists[0]),
@@ -2510,24 +2511,6 @@ fn lint_unnecessary_fold(cx: &LateContext<'_>, expr: &hir::Expr<'_>, fold_args:
25102511
}
25112512
}
25122513

2513-
fn lint_iter_skip_next(cx: &LateContext<'_>, expr: &hir::Expr<'_>, skip_args: &[hir::Expr<'_>]) {
2514-
// lint if caller of skip is an Iterator
2515-
if match_trait_method(cx, expr, &paths::ITERATOR) {
2516-
if let [caller, n] = skip_args {
2517-
let hint = format!(".nth({})", snippet(cx, n.span, ".."));
2518-
span_lint_and_sugg(
2519-
cx,
2520-
ITER_SKIP_NEXT,
2521-
expr.span.trim_start(caller.span).unwrap(),
2522-
"called `skip(..).next()` on an iterator",
2523-
"use `nth` instead",
2524-
hint,
2525-
Applicability::MachineApplicable,
2526-
);
2527-
}
2528-
}
2529-
}
2530-
25312514
fn derefs_to_slice<'tcx>(
25322515
cx: &LateContext<'tcx>,
25332516
expr: &'tcx hir::Expr<'tcx>,

0 commit comments

Comments
 (0)