Skip to content

Commit d219888

Browse files
committed
Auto merge of #6891 - Y-Nak:use-iterator-sym, r=Manishearth
Use sym::Iterator instead of paths::ITERATOR Since `sym::Iterator` was added to diagnostic_item, it's time to remove `paths::ITERATOR`. ref: #5393 changelog: Add `is_trait_method` to `clippy_utils` changelog: Remove `paths::ITERATOR`
2 parents 6ed6f1e + 93ee80a commit d219888

30 files changed

+97
-74
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::{
2-
differing_macro_contexts, get_parent_expr, get_trait_def_id, implements_trait, paths,
3-
snippet_block_with_applicability, span_lint, span_lint_and_sugg,
2+
differing_macro_contexts, get_parent_expr, implements_trait, snippet_block_with_applicability, span_lint,
3+
span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
@@ -10,6 +10,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
1010
use rustc_middle::hir::map::Map;
1111
use rustc_middle::lint::in_external_macro;
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
13+
use rustc_span::sym;
1314

1415
declare_clippy_lint! {
1516
/// **What it does:** Checks for `if` conditions that use blocks containing an
@@ -61,7 +62,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
6162
if let Some(parent) = get_parent_expr(self.cx, expr);
6263
if let ExprKind::MethodCall(_, _, args, _) = parent.kind;
6364
let caller = self.cx.typeck_results().expr_ty(&args[0]);
64-
if let Some(iter_id) = get_trait_def_id(self.cx, &paths::ITERATOR);
65+
if let Some(iter_id) = self.cx.tcx.get_diagnostic_item(sym::Iterator);
6566
if implements_trait(self.cx, caller, iter_id, &[]);
6667
then {
6768
return;

clippy_lints/src/copy_iterator.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
use crate::utils::{is_copy, match_path, paths, span_lint_and_note};
1+
use crate::utils::{is_copy, span_lint_and_note};
22
use rustc_hir::{Impl, Item, ItemKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
5+
use rustc_span::sym;
6+
7+
use if_chain::if_chain;
58

69
declare_clippy_lint! {
710
/// **What it does:** Checks for types that implement `Copy` as well as
@@ -33,14 +36,16 @@ declare_lint_pass!(CopyIterator => [COPY_ITERATOR]);
3336

3437
impl<'tcx> LateLintPass<'tcx> for CopyIterator {
3538
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
36-
if let ItemKind::Impl(Impl {
37-
of_trait: Some(ref trait_ref),
38-
..
39-
}) = item.kind
40-
{
39+
if_chain! {
40+
if let ItemKind::Impl(Impl {
41+
of_trait: Some(ref trait_ref),
42+
..
43+
}) = item.kind;
4144
let ty = cx.tcx.type_of(item.def_id);
42-
43-
if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
45+
if is_copy(cx, ty);
46+
if let Some(trait_id) = trait_ref.trait_def_id();
47+
if cx.tcx.is_diagnostic_item(sym::Iterator, trait_id);
48+
then {
4449
span_lint_and_note(
4550
cx,
4651
COPY_ITERATOR,

clippy_lints/src/loops/iter_next_loop.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use super::ITER_NEXT_LOOP;
2-
use crate::utils::{match_trait_method, paths, span_lint};
32
use rustc_hir::Expr;
43
use rustc_lint::LateContext;
4+
use rustc_span::sym;
5+
6+
use crate::utils::{is_trait_method, span_lint};
57

68
pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, expr: &Expr<'_>) -> bool {
7-
if match_trait_method(cx, arg, &paths::ITERATOR) {
9+
if is_trait_method(cx, arg, sym::Iterator) {
810
span_lint(
911
cx,
1012
ITER_NEXT_LOOP,

clippy_lints/src/loops/needless_collect.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::NEEDLESS_COLLECT;
22
use crate::utils::sugg::Sugg;
33
use crate::utils::{
4-
is_type_diagnostic_item, match_trait_method, match_type, path_to_local_id, paths, snippet, span_lint_and_sugg,
4+
is_trait_method, is_type_diagnostic_item, match_type, path_to_local_id, paths, snippet, span_lint_and_sugg,
55
span_lint_and_then,
66
};
77
use if_chain::if_chain;
@@ -23,7 +23,7 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
2323
if_chain! {
2424
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
2525
if let ExprKind::MethodCall(ref chain_method, _, _, _) = args[0].kind;
26-
if chain_method.ident.name == sym!(collect) && match_trait_method(cx, &args[0], &paths::ITERATOR);
26+
if chain_method.ident.name == sym!(collect) && is_trait_method(cx, &args[0], sym::Iterator);
2727
if let Some(ref generic_args) = chain_method.args;
2828
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
2929
then {
@@ -94,7 +94,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
9494
init: Some(ref init_expr), .. }
9595
) = stmt.kind;
9696
if let ExprKind::MethodCall(ref method_name, _, &[ref iter_source], ..) = init_expr.kind;
97-
if method_name.ident.name == sym!(collect) && match_trait_method(cx, &init_expr, &paths::ITERATOR);
97+
if method_name.ident.name == sym!(collect) && is_trait_method(cx, &init_expr, sym::Iterator);
9898
if let Some(ref generic_args) = method_name.args;
9999
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
100100
if let ty = cx.typeck_results().node_type(ty.hir_id);

clippy_lints/src/loops/utils.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::utils::{
2-
get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, is_integer_const, path_to_local,
3-
path_to_local_id, paths, sugg,
2+
get_parent_expr, has_iter_method, implements_trait, is_integer_const, path_to_local, path_to_local_id, sugg,
43
};
54
use if_chain::if_chain;
65
use rustc_data_structures::fx::FxHashMap;
@@ -10,7 +9,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, P
109
use rustc_lint::LateContext;
1110
use rustc_middle::hir::map::Map;
1211
use rustc_span::source_map::Span;
13-
use rustc_span::symbol::Symbol;
12+
use rustc_span::symbol::{sym, Symbol};
1413
use std::iter::Iterator;
1514

1615
#[derive(Debug, PartialEq)]
@@ -316,7 +315,7 @@ pub(super) fn get_span_of_entire_for_loop(expr: &Expr<'_>) -> Span {
316315
/// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
317316
/// actual `Iterator` that the loop uses.
318317
pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic_ref: &mut Applicability) -> String {
319-
let impls_iterator = get_trait_def_id(cx, &paths::ITERATOR).map_or(false, |id| {
318+
let impls_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| {
320319
implements_trait(cx, cx.typeck_results().expr_ty(arg), id, &[])
321320
});
322321
if impls_iterator {

clippy_lints/src/loops/while_let_on_iterator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use super::utils::{LoopNestVisitor, Nesting};
22
use super::WHILE_LET_ON_ITERATOR;
33
use crate::utils::usage::mutated_variables;
44
use crate::utils::{
5-
get_enclosing_block, get_trait_def_id, implements_trait, is_refutable, last_path_segment, match_trait_method,
6-
path_to_local, path_to_local_id, paths, snippet_with_applicability, span_lint_and_sugg,
5+
get_enclosing_block, implements_trait, is_refutable, is_trait_method, last_path_segment, path_to_local,
6+
path_to_local_id, snippet_with_applicability, span_lint_and_sugg,
77
};
88
use if_chain::if_chain;
99
use rustc_errors::Applicability;
@@ -27,7 +27,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
2727
// Don't lint when the iterator is recreated on every iteration
2828
if_chain! {
2929
if let ExprKind::MethodCall(..) | ExprKind::Call(..) = iter_expr.kind;
30-
if let Some(iter_def_id) = get_trait_def_id(cx, &paths::ITERATOR);
30+
if let Some(iter_def_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
3131
if implements_trait(cx, cx.typeck_results().expr_ty(iter_expr), iter_def_id, &[]);
3232
then {
3333
return;
@@ -36,7 +36,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
3636

3737
let lhs_constructor = last_path_segment(qpath);
3838
if method_path.ident.name == sym::next
39-
&& match_trait_method(cx, match_expr, &paths::ITERATOR)
39+
&& is_trait_method(cx, match_expr, sym::Iterator)
4040
&& lhs_constructor.ident.name == sym::Some
4141
&& (pat_args.is_empty()
4242
|| !is_refutable(cx, &pat_args[0])

clippy_lints/src/map_clone.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use crate::utils::paths;
21
use crate::utils::{
3-
is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
2+
is_copy, is_trait_method, is_type_diagnostic_item, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
43
};
54
use if_chain::if_chain;
65
use rustc_errors::Applicability;
@@ -55,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
5554
if args.len() == 2;
5655
if method.ident.name == sym::map;
5756
let ty = cx.typeck_results().expr_ty(&args[0]);
58-
if is_type_diagnostic_item(cx, ty, sym::option_type) || match_trait_method(cx, e, &paths::ITERATOR);
57+
if is_type_diagnostic_item(cx, ty, sym::option_type) || is_trait_method(cx, e, sym::Iterator);
5958
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;
6059
let closure_body = cx.tcx.hir().body(body_id);
6160
let closure_expr = remove_blocks(&closure_body.value);

clippy_lints/src/map_identity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::utils::{
2-
is_adjusted, is_type_diagnostic_item, match_path, match_trait_method, match_var, paths, remove_blocks,
2+
is_adjusted, is_trait_method, is_type_diagnostic_item, match_path, match_var, paths, remove_blocks,
33
span_lint_and_sugg,
44
};
55
use if_chain::if_chain;
@@ -65,7 +65,7 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
6565
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
6666
if args.len() == 2 && method.ident.name == sym::map;
6767
let caller_ty = cx.typeck_results().expr_ty(&args[0]);
68-
if match_trait_method(cx, expr, &paths::ITERATOR)
68+
if is_trait_method(cx, expr, sym::Iterator)
6969
|| is_type_diagnostic_item(cx, caller_ty, sym::result_type)
7070
|| is_type_diagnostic_item(cx, caller_ty, sym::option_type);
7171
then {

clippy_lints/src/matches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ where
16141614

16151615
mod redundant_pattern_match {
16161616
use super::REDUNDANT_PATTERN_MATCHING;
1617-
use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
1617+
use crate::utils::{is_trait_method, match_qpath, paths, snippet, span_lint_and_then};
16181618
use if_chain::if_chain;
16191619
use rustc_ast::ast::LitKind;
16201620
use rustc_errors::Applicability;
@@ -1679,7 +1679,7 @@ mod redundant_pattern_match {
16791679
if keyword == "while";
16801680
if let ExprKind::MethodCall(method_path, _, _, _) = op.kind;
16811681
if method_path.ident.name == sym::next;
1682-
if match_trait_method(cx, op, &paths::ITERATOR);
1682+
if is_trait_method(cx, op, sym::Iterator);
16831683
then {
16841684
return;
16851685
}

clippy_lints/src/methods/filter_flat_map.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::utils::{match_trait_method, paths, span_lint_and_help};
1+
use crate::utils::{is_trait_method, span_lint_and_help};
22
use rustc_hir as hir;
33
use rustc_lint::LateContext;
4+
use rustc_span::sym;
45

56
use super::FILTER_MAP;
67

@@ -12,7 +13,7 @@ pub(super) fn check<'tcx>(
1213
_map_args: &'tcx [hir::Expr<'_>],
1314
) {
1415
// lint if caller of `.filter().flat_map()` is an Iterator
15-
if match_trait_method(cx, expr, &paths::ITERATOR) {
16+
if is_trait_method(cx, expr, sym::Iterator) {
1617
let msg = "called `filter(..).flat_map(..)` on an `Iterator`";
1718
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
1819
and filtering by returning `iter::empty()`";

0 commit comments

Comments
 (0)