-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform another layer of pattern equality checking before spelling th…
…e lint
- Loading branch information
Showing
2 changed files
with
38 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,46 @@ | ||
use if_chain::if_chain; | ||
use rustc_hir as hir; | ||
use rustc_lint::LateContext; | ||
use rustc_span::source_map::Span; | ||
|
||
use crate::utils::{match_trait_method, paths, span_lint_and_help}; | ||
use crate::utils::{match_trait_method, paths, span_lint_and_help, SpanlessEq}; | ||
|
||
use super::INSPECT_THEN_FOR_EACH; | ||
|
||
/// lint use of `inspect().for_each()` for `Iterators` | ||
pub(super) fn lint<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, inspect_span: Span) { | ||
pub(super) fn lint<'tcx>( | ||
cx: &LateContext<'tcx>, | ||
expr: &'tcx hir::Expr<'_>, | ||
inspect_args: &'tcx [hir::Expr<'_>], | ||
for_each_args: &'tcx [hir::Expr<'_>], | ||
inspect_span: Span, | ||
) { | ||
if match_trait_method(cx, expr, &paths::ITERATOR) { | ||
let msg = "called `inspect(..).for_each(..)` on an `Iterator`"; | ||
let hint = "move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)`"; | ||
span_lint_and_help( | ||
cx, | ||
INSPECT_THEN_FOR_EACH, | ||
inspect_span.with_hi(expr.span.hi()), | ||
msg, | ||
None, | ||
hint, | ||
); | ||
let inspect_closure = &inspect_args[1].kind; | ||
let for_each_closure = &for_each_args[1].kind; | ||
|
||
if_chain! { | ||
if let hir::ExprKind::Closure(_, _, inspect_closure_body_id, _, _) = inspect_closure; | ||
if let hir::ExprKind::Closure(_, _, for_each_closure_body_id, _, _) = for_each_closure; | ||
|
||
let inspect_closure_pat = cx.tcx.hir().body(*inspect_closure_body_id).params[0].pat; | ||
let for_each_closure_pat = cx.tcx.hir().body(*for_each_closure_body_id).params[0].pat; | ||
|
||
if SpanlessEq::new(cx).eq_pat(inspect_closure_pat, for_each_closure_pat); | ||
|
||
then { | ||
let msg = "called `inspect(..).for_each(..)` on an `Iterator`"; | ||
let hint = "move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)`"; | ||
|
||
span_lint_and_help( | ||
cx, | ||
INSPECT_THEN_FOR_EACH, | ||
inspect_span.with_hi(expr.span.hi()), | ||
msg, | ||
None, | ||
hint, | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters