Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use sym::Iterator instead of paths::ITERATOR #6891

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clippy_lints/src/blocks_in_if_conditions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::utils::{
differing_macro_contexts, get_parent_expr, get_trait_def_id, implements_trait, paths,
snippet_block_with_applicability, span_lint, span_lint_and_sugg,
differing_macro_contexts, get_parent_expr, implements_trait, snippet_block_with_applicability, span_lint,
span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand All @@ -10,6 +10,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::hir::map::Map;
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

declare_clippy_lint! {
/// **What it does:** Checks for `if` conditions that use blocks containing an
Expand Down Expand Up @@ -61,7 +62,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ExVisitor<'a, 'tcx> {
if let Some(parent) = get_parent_expr(self.cx, expr);
if let ExprKind::MethodCall(_, _, args, _) = parent.kind;
let caller = self.cx.typeck_results().expr_ty(&args[0]);
if let Some(iter_id) = get_trait_def_id(self.cx, &paths::ITERATOR);
if let Some(iter_id) = self.cx.tcx.get_diagnostic_item(sym::Iterator);
if implements_trait(self.cx, caller, iter_id, &[]);
then {
return;
Expand Down
21 changes: 13 additions & 8 deletions clippy_lints/src/copy_iterator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::utils::{is_copy, match_path, paths, span_lint_and_note};
use crate::utils::{is_copy, span_lint_and_note};
use rustc_hir::{Impl, Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;

use if_chain::if_chain;

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

impl<'tcx> LateLintPass<'tcx> for CopyIterator {
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
if let ItemKind::Impl(Impl {
of_trait: Some(ref trait_ref),
..
}) = item.kind
{
if_chain! {
if let ItemKind::Impl(Impl {
of_trait: Some(ref trait_ref),
..
}) = item.kind;
let ty = cx.tcx.type_of(item.def_id);

if is_copy(cx, ty) && match_path(&trait_ref.path, &paths::ITERATOR) {
if is_copy(cx, ty);
if let Some(trait_id) = trait_ref.trait_def_id();
if cx.tcx.is_diagnostic_item(sym::Iterator, trait_id);
then {
span_lint_and_note(
cx,
COPY_ITERATOR,
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/loops/iter_next_loop.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use super::ITER_NEXT_LOOP;
use crate::utils::{match_trait_method, paths, span_lint};
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_span::sym;

use crate::utils::{is_trait_method, span_lint};

pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, expr: &Expr<'_>) -> bool {
if match_trait_method(cx, arg, &paths::ITERATOR) {
if is_trait_method(cx, arg, sym::Iterator) {
span_lint(
cx,
ITER_NEXT_LOOP,
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/loops/needless_collect.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::NEEDLESS_COLLECT;
use crate::utils::sugg::Sugg;
use crate::utils::{
is_type_diagnostic_item, match_trait_method, match_type, path_to_local_id, paths, snippet, span_lint_and_sugg,
is_trait_method, is_type_diagnostic_item, match_type, path_to_local_id, paths, snippet, span_lint_and_sugg,
span_lint_and_then,
};
use if_chain::if_chain;
Expand All @@ -23,7 +23,7 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
if_chain! {
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
if let ExprKind::MethodCall(ref chain_method, _, _, _) = args[0].kind;
if chain_method.ident.name == sym!(collect) && match_trait_method(cx, &args[0], &paths::ITERATOR);
if chain_method.ident.name == sym!(collect) && is_trait_method(cx, &args[0], sym::Iterator);
if let Some(ref generic_args) = chain_method.args;
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
then {
Expand Down Expand Up @@ -94,7 +94,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
init: Some(ref init_expr), .. }
) = stmt.kind;
if let ExprKind::MethodCall(ref method_name, _, &[ref iter_source], ..) = init_expr.kind;
if method_name.ident.name == sym!(collect) && match_trait_method(cx, &init_expr, &paths::ITERATOR);
if method_name.ident.name == sym!(collect) && is_trait_method(cx, &init_expr, sym::Iterator);
if let Some(ref generic_args) = method_name.args;
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
if let ty = cx.typeck_results().node_type(ty.hir_id);
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/loops/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::utils::{
get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, is_integer_const, path_to_local,
path_to_local_id, paths, sugg,
get_parent_expr, has_iter_method, implements_trait, is_integer_const, path_to_local, path_to_local_id, sugg,
};
use if_chain::if_chain;
use rustc_data_structures::fx::FxHashMap;
Expand All @@ -10,7 +9,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, P
use rustc_lint::LateContext;
use rustc_middle::hir::map::Map;
use rustc_span::source_map::Span;
use rustc_span::symbol::Symbol;
use rustc_span::symbol::{sym, Symbol};
use std::iter::Iterator;

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -316,7 +315,7 @@ pub(super) fn get_span_of_entire_for_loop(expr: &Expr<'_>) -> Span {
/// If `arg` was the argument to a `for` loop, return the "cleanest" way of writing the
/// actual `Iterator` that the loop uses.
pub(super) fn make_iterator_snippet(cx: &LateContext<'_>, arg: &Expr<'_>, applic_ref: &mut Applicability) -> String {
let impls_iterator = get_trait_def_id(cx, &paths::ITERATOR).map_or(false, |id| {
let impls_iterator = cx.tcx.get_diagnostic_item(sym::Iterator).map_or(false, |id| {
implements_trait(cx, cx.typeck_results().expr_ty(arg), id, &[])
});
if impls_iterator {
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/loops/while_let_on_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use super::utils::{LoopNestVisitor, Nesting};
use super::WHILE_LET_ON_ITERATOR;
use crate::utils::usage::mutated_variables;
use crate::utils::{
get_enclosing_block, get_trait_def_id, implements_trait, is_refutable, last_path_segment, match_trait_method,
path_to_local, path_to_local_id, paths, snippet_with_applicability, span_lint_and_sugg,
get_enclosing_block, implements_trait, is_refutable, is_trait_method, last_path_segment, path_to_local,
path_to_local_id, snippet_with_applicability, span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand All @@ -27,7 +27,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
// Don't lint when the iterator is recreated on every iteration
if_chain! {
if let ExprKind::MethodCall(..) | ExprKind::Call(..) = iter_expr.kind;
if let Some(iter_def_id) = get_trait_def_id(cx, &paths::ITERATOR);
if let Some(iter_def_id) = cx.tcx.get_diagnostic_item(sym::Iterator);
if implements_trait(cx, cx.typeck_results().expr_ty(iter_expr), iter_def_id, &[]);
then {
return;
Expand All @@ -36,7 +36,7 @@ pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {

let lhs_constructor = last_path_segment(qpath);
if method_path.ident.name == sym::next
&& match_trait_method(cx, match_expr, &paths::ITERATOR)
&& is_trait_method(cx, match_expr, sym::Iterator)
&& lhs_constructor.ident.name == sym::Some
&& (pat_args.is_empty()
|| !is_refutable(cx, &pat_args[0])
Expand Down
5 changes: 2 additions & 3 deletions clippy_lints/src/map_clone.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::utils::paths;
use crate::utils::{
is_copy, is_type_diagnostic_item, match_trait_method, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
is_copy, is_trait_method, is_type_diagnostic_item, remove_blocks, snippet_with_applicability, span_lint_and_sugg,
};
use if_chain::if_chain;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -55,7 +54,7 @@ impl<'tcx> LateLintPass<'tcx> for MapClone {
if args.len() == 2;
if method.ident.name == sym::map;
let ty = cx.typeck_results().expr_ty(&args[0]);
if is_type_diagnostic_item(cx, ty, sym::option_type) || match_trait_method(cx, e, &paths::ITERATOR);
if is_type_diagnostic_item(cx, ty, sym::option_type) || is_trait_method(cx, e, sym::Iterator);
if let hir::ExprKind::Closure(_, _, body_id, _, _) = args[1].kind;
let closure_body = cx.tcx.hir().body(body_id);
let closure_expr = remove_blocks(&closure_body.value);
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/map_identity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::utils::{
is_adjusted, is_type_diagnostic_item, match_path, match_trait_method, match_var, paths, remove_blocks,
is_adjusted, is_trait_method, is_type_diagnostic_item, match_path, match_var, paths, remove_blocks,
span_lint_and_sugg,
};
use if_chain::if_chain;
Expand Down Expand Up @@ -65,7 +65,7 @@ fn get_map_argument<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a
if let ExprKind::MethodCall(ref method, _, ref args, _) = expr.kind;
if args.len() == 2 && method.ident.name == sym::map;
let caller_ty = cx.typeck_results().expr_ty(&args[0]);
if match_trait_method(cx, expr, &paths::ITERATOR)
if is_trait_method(cx, expr, sym::Iterator)
|| is_type_diagnostic_item(cx, caller_ty, sym::result_type)
|| is_type_diagnostic_item(cx, caller_ty, sym::option_type);
then {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1614,7 +1614,7 @@ where

mod redundant_pattern_match {
use super::REDUNDANT_PATTERN_MATCHING;
use crate::utils::{match_qpath, match_trait_method, paths, snippet, span_lint_and_then};
use crate::utils::{is_trait_method, match_qpath, paths, snippet, span_lint_and_then};
use if_chain::if_chain;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -1679,7 +1679,7 @@ mod redundant_pattern_match {
if keyword == "while";
if let ExprKind::MethodCall(method_path, _, _, _) = op.kind;
if method_path.ident.name == sym::next;
if match_trait_method(cx, op, &paths::ITERATOR);
if is_trait_method(cx, op, sym::Iterator);
then {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filter_flat_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{match_trait_method, paths, span_lint_and_help};
use crate::utils::{is_trait_method, span_lint_and_help};
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::sym;

use super::FILTER_MAP;

Expand All @@ -12,7 +13,7 @@ pub(super) fn check<'tcx>(
_map_args: &'tcx [hir::Expr<'_>],
) {
// lint if caller of `.filter().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let msg = "called `filter(..).flat_map(..)` on an `Iterator`";
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning `iter::empty()`";
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/filter_map.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::utils::{match_trait_method, path_to_local_id, paths, snippet, span_lint_and_sugg, SpanlessEq};
use crate::utils::{is_trait_method, path_to_local_id, snippet, span_lint_and_sugg, SpanlessEq};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
Expand All @@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, is_
if_chain! {
if let ExprKind::MethodCall(_, _, [map_recv, map_arg], map_span) = expr.kind;
if let ExprKind::MethodCall(_, _, [_, filter_arg], filter_span) = map_recv.kind;
if match_trait_method(cx, map_recv, &paths::ITERATOR);
if is_trait_method(cx, map_recv, sym::Iterator);

// filter(|x| ...is_some())...
if let ExprKind::Closure(_, _, filter_body_id, ..) = filter_arg.kind;
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filter_map_flat_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{match_trait_method, paths, span_lint_and_help};
use crate::utils::{is_trait_method, span_lint_and_help};
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::sym;

use super::FILTER_MAP;

Expand All @@ -12,7 +13,7 @@ pub(super) fn check<'tcx>(
_map_args: &'tcx [hir::Expr<'_>],
) {
// lint if caller of `.filter_map().flat_map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let msg = "called `filter_map(..).flat_map(..)` on an `Iterator`";
let hint = "this is more succinctly expressed by calling `.flat_map(..)` \
and filtering by returning `iter::empty()`";
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/filter_map_identity.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::utils::{match_qpath, match_trait_method, path_to_local_id, paths, span_lint_and_sugg};
use crate::utils::{is_trait_method, match_qpath, path_to_local_id, paths, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use rustc_span::{source_map::Span, sym};

use super::FILTER_MAP_IDENTITY;

Expand All @@ -13,7 +13,7 @@ pub(super) fn check(
filter_map_args: &[hir::Expr<'_>],
filter_map_span: Span,
) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let arg_node = &filter_map_args[1].kind;

let apply_lint = |message: &str| {
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filter_map_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::utils::{match_trait_method, paths, span_lint_and_help};
use crate::utils::{is_trait_method, span_lint_and_help};
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::sym;

use super::FILTER_MAP;

Expand All @@ -12,7 +13,7 @@ pub(super) fn check<'tcx>(
_map_args: &'tcx [hir::Expr<'_>],
) {
// lint if caller of `.filter_map().map()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let msg = "called `filter_map(..).map(..)` on an `Iterator`";
let hint = "this is more succinctly expressed by only calling `.filter_map(..)` instead";
span_lint_and_help(cx, FILTER_MAP, expr.span, msg, None, hint);
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filter_map_next.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::utils::{match_trait_method, meets_msrv, paths, snippet, span_lint, span_lint_and_sugg};
use crate::utils::{is_trait_method, meets_msrv, snippet, span_lint, span_lint_and_sugg};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_semver::RustcVersion;
use rustc_span::sym;

use super::FILTER_MAP_NEXT;

Expand All @@ -14,7 +15,7 @@ pub(super) fn check<'tcx>(
filter_args: &'tcx [hir::Expr<'_>],
msrv: Option<&RustcVersion>,
) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
if !meets_msrv(msrv, &FILTER_MAP_NEXT_MSRV) {
return;
}
Expand Down
5 changes: 3 additions & 2 deletions clippy_lints/src/methods/filter_next.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::utils::{match_trait_method, paths, snippet, span_lint, span_lint_and_sugg};
use crate::utils::{is_trait_method, snippet, span_lint, span_lint_and_sugg};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::sym;

use super::FILTER_NEXT;

/// lint use of `filter().next()` for `Iterators`
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, filter_args: &'tcx [hir::Expr<'_>]) {
// lint if caller of `.filter().next()` is an Iterator
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let msg = "called `filter(..).next()` on an `Iterator`. This is more succinctly expressed by calling \
`.find(..)` instead";
let filter_snippet = snippet(cx, filter_args[1].span, "..");
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/flat_map_identity.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::utils::{match_qpath, match_trait_method, paths, span_lint_and_sugg};
use crate::utils::{is_trait_method, match_qpath, paths, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use rustc_span::{source_map::Span, sym};

use super::FLAT_MAP_IDENTITY;

Expand All @@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
flat_map_args: &'tcx [hir::Expr<'_>],
flat_map_span: Span,
) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::Iterator) {
let arg_node = &flat_map_args[1].kind;

let apply_lint = |message: &str| {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/methods/from_iter_instead_of_collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_lint::{LateContext, LintContext};
use rustc_middle::ty::Ty;
use rustc_span::sym;

use super::FROM_ITER_INSTEAD_OF_COLLECT;

Expand All @@ -13,7 +14,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Exp

if_chain! {
if let Some(from_iter_id) = get_trait_def_id(cx, &paths::FROM_ITERATOR);
if let Some(iter_id) = get_trait_def_id(cx, &paths::ITERATOR);
if let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator);

if implements_trait(cx, ty, from_iter_id, &[]) && implements_trait(cx, arg_ty, iter_id, &[]);
then {
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/methods/inspect_for_each.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rustc_hir as hir;
use rustc_lint::LateContext;
use rustc_span::source_map::Span;
use rustc_span::{source_map::Span, sym};

use crate::utils::{match_trait_method, paths, span_lint_and_help};
use crate::utils::{is_trait_method, span_lint_and_help};

use super::INSPECT_FOR_EACH;

/// lint use of `inspect().for_each()` for `Iterators`
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>, inspect_span: Span) {
if match_trait_method(cx, expr, &paths::ITERATOR) {
if is_trait_method(cx, expr, sym::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(
Expand Down
Loading