From cc3ab1c0ec8c94920fea7aab6c912297f489ff49 Mon Sep 17 00:00:00 2001 From: Yoshitomo Nakanishi Date: Fri, 12 Feb 2021 14:28:17 +0900 Subject: [PATCH] Move linked_list to its own module --- clippy_lints/src/types/linked_list.rs | 20 ++++++++++++++++++++ clippy_lints/src/types/mod.rs | 23 +++++------------------ clippy_lints/src/types/option_option.rs | 6 ++---- 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 clippy_lints/src/types/linked_list.rs diff --git a/clippy_lints/src/types/linked_list.rs b/clippy_lints/src/types/linked_list.rs new file mode 100644 index 00000000000..74be8959fa4 --- /dev/null +++ b/clippy_lints/src/types/linked_list.rs @@ -0,0 +1,20 @@ +use rustc_hir::{self as hir, def_id::DefId}; +use rustc_lint::LateContext; + +use crate::utils::{match_def_path, paths, span_lint_and_help}; + +pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, def_id: DefId) -> bool { + if match_def_path(cx, def_id, &paths::LINKED_LIST) { + span_lint_and_help( + cx, + super::LINKEDLIST, + hir_ty.span, + "you seem to be using a `LinkedList`! Perhaps you meant some other data structure?", + None, + "a `VecDeque` might work", + ); + true + } else { + false + } +} diff --git a/clippy_lints/src/types/mod.rs b/clippy_lints/src/types/mod.rs index 55f1d86bc56..86a12b8ffa9 100644 --- a/clippy_lints/src/types/mod.rs +++ b/clippy_lints/src/types/mod.rs @@ -1,6 +1,7 @@ #![allow(rustc::default_hash_types)] mod box_vec; +mod linked_list; mod option_option; mod rc_buffer; mod redundant_allocation; @@ -39,10 +40,9 @@ use crate::utils::sugg::Sugg; use crate::utils::{ clip, comparisons, differing_macro_contexts, higher, in_constant, indent_of, int_bits, is_hir_ty_cfg_dependant, - is_ty_param_diagnostic_item, is_type_diagnostic_item, match_def_path, match_path, meets_msrv, method_chain_args, - multispan_sugg, numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt, - snippet_with_applicability, snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, - span_lint_and_then, unsext, + is_type_diagnostic_item, match_path, meets_msrv, method_chain_args, multispan_sugg, + numeric_literal::NumericLiteral, reindent_multiline, sext, snippet, snippet_opt, snippet_with_applicability, + snippet_with_macro_callsite, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, unsext, }; declare_clippy_lint! { @@ -313,7 +313,6 @@ fn check_fn_decl(&mut self, cx: &LateContext<'_>, decl: &FnDecl<'_>) { /// /// The parameter `is_local` distinguishes the context of the type; types from /// local bindings should only be checked for the `BORROWED_BOX` lint. - #[allow(clippy::too_many_lines)] fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: bool) { if hir_ty.span.from_expansion() { return; @@ -329,18 +328,7 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: boo triggered |= rc_buffer::check(cx, hir_ty, qpath, def_id); triggered |= vec_box::check(cx, hir_ty, qpath, def_id, self.vec_box_size_threshold); triggered |= option_option::check(cx, hir_ty, qpath, def_id); - - if match_def_path(cx, def_id, &paths::LINKED_LIST) { - span_lint_and_help( - cx, - LINKEDLIST, - hir_ty.span, - "you seem to be using a `LinkedList`! Perhaps you meant some other data structure?", - None, - "a `VecDeque` might work", - ); - return; // don't recurse into the type - } + triggered |= linked_list::check(cx, hir_ty, def_id); if triggered { return; @@ -389,7 +377,6 @@ fn check_ty(&mut self, cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, is_local: boo } }, TyKind::Rptr(ref lt, ref mut_ty) => self.check_ty_rptr(cx, hir_ty, is_local, lt, mut_ty), - // recurse TyKind::Slice(ref ty) | TyKind::Array(ref ty, _) | TyKind::Ptr(MutTy { ref ty, .. }) => { self.check_ty(cx, ty, is_local) }, diff --git a/clippy_lints/src/types/option_option.rs b/clippy_lints/src/types/option_option.rs index 42aad70438b..d91132aaeb2 100644 --- a/clippy_lints/src/types/option_option.rs +++ b/clippy_lints/src/types/option_option.rs @@ -2,13 +2,11 @@ use rustc_lint::LateContext; use rustc_span::symbol::sym; -use crate::utils::span_lint; - -use super::utils; +use crate::utils::{is_ty_param_diagnostic_item, span_lint}; pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, qpath: &QPath<'_>, def_id: DefId) -> bool { if cx.tcx.is_diagnostic_item(sym::option_type, def_id) { - if utils::is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() { + if is_ty_param_diagnostic_item(cx, qpath, sym::option_type).is_some() { span_lint( cx, super::OPTION_OPTION, -- 2.44.0