From 483bac2dc0a47df87141a98df1d470b25cd2eda3 Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Wed, 3 Mar 2021 00:31:15 +0900 Subject: [PATCH] move skip_while_next to its own module --- clippy_lints/src/methods/mod.rs | 22 ++------------------- clippy_lints/src/methods/skip_while_next.rs | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 clippy_lints/src/methods/skip_while_next.rs diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index 71080e9be..34def45a8 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -11,6 +11,7 @@ mod iter_count; mod manual_saturating_arithmetic; mod ok_expect; mod option_map_unwrap_or; +mod skip_while_next; mod unnecessary_filter_map; mod unnecessary_lazy_eval; mod unwrap_used; @@ -1679,7 +1680,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods { } }, ["next", "filter"] => filter_next::check(cx, expr, arg_lists[1]), - ["next", "skip_while"] => lint_skip_while_next(cx, expr, arg_lists[1]), + ["next", "skip_while"] => skip_while_next::check(cx, expr, arg_lists[1]), ["next", "iter"] => lint_iter_next(cx, expr, arg_lists[1]), ["map", "filter"] => lint_filter_map(cx, expr, false), ["map", "filter_map"] => lint_filter_map_map(cx, expr, arg_lists[1], arg_lists[0]), @@ -2965,25 +2966,6 @@ fn lint_map_or_none<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, map ); } -/// lint use of `skip_while().next()` for `Iterators` -fn lint_skip_while_next<'tcx>( - cx: &LateContext<'tcx>, - expr: &'tcx hir::Expr<'_>, - _skip_while_args: &'tcx [hir::Expr<'_>], -) { - // lint if caller of `.skip_while().next()` is an Iterator - if match_trait_method(cx, expr, &paths::ITERATOR) { - span_lint_and_help( - cx, - SKIP_WHILE_NEXT, - expr.span, - "called `skip_while(

).next()` on an `Iterator`", - None, - "this is more succinctly expressed by calling `.find(!

)` instead", - ); - } -} - /// lint use of `filter().map()` or `find().map()` for `Iterators` fn lint_filter_map<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, is_find: bool) { if_chain! { diff --git a/clippy_lints/src/methods/skip_while_next.rs b/clippy_lints/src/methods/skip_while_next.rs new file mode 100644 index 000000000..8ba6ae952 --- /dev/null +++ b/clippy_lints/src/methods/skip_while_next.rs @@ -0,0 +1,20 @@ +use crate::utils::{match_trait_method, paths, span_lint_and_help}; +use rustc_hir as hir; +use rustc_lint::LateContext; + +use super::SKIP_WHILE_NEXT; + +/// lint use of `skip_while().next()` for `Iterators` +pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, _skip_while_args: &'tcx [hir::Expr<'_>]) { + // lint if caller of `.skip_while().next()` is an Iterator + if match_trait_method(cx, expr, &paths::ITERATOR) { + span_lint_and_help( + cx, + SKIP_WHILE_NEXT, + expr.span, + "called `skip_while(

).next()` on an `Iterator`", + None, + "this is more succinctly expressed by calling `.find(!

)` instead", + ); + } +}