1 use crate::utils::{match_trait_method, paths, snippet, span_lint, span_lint_and_sugg};
2 use rustc_errors::Applicability;
4 use rustc_lint::LateContext;
6 use super::FILTER_NEXT;
8 /// lint use of `filter().next()` for `Iterators`
9 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>]) {
10 // lint if caller of `.filter().next()` is an Iterator
11 if match_trait_method(cx, expr, &paths::ITERATOR) {
12 let msg = "called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling \
14 let filter_snippet = snippet(cx, filter_args[1].span, "..");
15 if filter_snippet.lines().count() <= 1 {
16 let iter_snippet = snippet(cx, filter_args[0].span, "..");
17 // add note if not multi-line
24 format!("{}.find({})", iter_snippet, filter_snippet),
25 Applicability::MachineApplicable,
28 span_lint(cx, FILTER_NEXT, expr.span, msg);