From 54e539121d7911933157519a80e8dd30219dc9e0 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Thu, 29 Jul 2021 12:10:18 +0200 Subject: Rename two lints to comply with our lint naming convention self_named_constructor -> self_named_constructors append_instead_of_extend -> extend_with_drain --- clippy_lints/src/lib.rs | 16 ++-- .../src/methods/append_instead_of_extend.rs | 41 ---------- clippy_lints/src/methods/extend_with_drain.rs | 41 ++++++++++ clippy_lints/src/methods/mod.rs | 8 +- clippy_lints/src/self_named_constructor.rs | 90 ---------------------- clippy_lints/src/self_named_constructors.rs | 90 ++++++++++++++++++++++ 6 files changed, 143 insertions(+), 143 deletions(-) delete mode 100644 clippy_lints/src/methods/append_instead_of_extend.rs create mode 100644 clippy_lints/src/methods/extend_with_drain.rs delete mode 100644 clippy_lints/src/self_named_constructor.rs create mode 100644 clippy_lints/src/self_named_constructors.rs (limited to 'clippy_lints') diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 3cffb507f70..f49b382c5ea 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -329,7 +329,7 @@ mod regex; mod repeat_once; mod returns; mod self_assignment; -mod self_named_constructor; +mod self_named_constructors; mod semicolon_if_nothing_returned; mod serde_api; mod shadow; @@ -740,7 +740,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: mem_replace::MEM_REPLACE_OPTION_WITH_NONE, mem_replace::MEM_REPLACE_WITH_DEFAULT, mem_replace::MEM_REPLACE_WITH_UNINIT, - methods::APPEND_INSTEAD_OF_EXTEND, methods::BIND_INSTEAD_OF_MAP, methods::BYTES_NTH, methods::CHARS_LAST_CMP, @@ -751,6 +750,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: methods::CLONE_ON_REF_PTR, methods::EXPECT_FUN_CALL, methods::EXPECT_USED, + methods::EXTEND_WITH_DRAIN, methods::FILETYPE_IS_FILE, methods::FILTER_MAP_IDENTITY, methods::FILTER_MAP_NEXT, @@ -900,7 +900,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: returns::LET_AND_RETURN, returns::NEEDLESS_RETURN, self_assignment::SELF_ASSIGNMENT, - self_named_constructor::SELF_NAMED_CONSTRUCTOR, + self_named_constructors::SELF_NAMED_CONSTRUCTORS, semicolon_if_nothing_returned::SEMICOLON_IF_NOTHING_RETURNED, serde_api::SERDE_API_MISUSE, shadow::SHADOW_REUSE, @@ -1296,7 +1296,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(mem_replace::MEM_REPLACE_OPTION_WITH_NONE), LintId::of(mem_replace::MEM_REPLACE_WITH_DEFAULT), LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT), - LintId::of(methods::APPEND_INSTEAD_OF_EXTEND), LintId::of(methods::BIND_INSTEAD_OF_MAP), LintId::of(methods::BYTES_NTH), LintId::of(methods::CHARS_LAST_CMP), @@ -1304,6 +1303,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(methods::CLONE_DOUBLE_REF), LintId::of(methods::CLONE_ON_COPY), LintId::of(methods::EXPECT_FUN_CALL), + LintId::of(methods::EXTEND_WITH_DRAIN), LintId::of(methods::FILTER_MAP_IDENTITY), LintId::of(methods::FILTER_NEXT), LintId::of(methods::FLAT_MAP_IDENTITY), @@ -1407,7 +1407,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(returns::LET_AND_RETURN), LintId::of(returns::NEEDLESS_RETURN), LintId::of(self_assignment::SELF_ASSIGNMENT), - LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR), + LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS), LintId::of(serde_api::SERDE_API_MISUSE), LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), LintId::of(size_of_in_element_count::SIZE_OF_IN_ELEMENT_COUNT), @@ -1561,7 +1561,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES), LintId::of(returns::LET_AND_RETURN), LintId::of(returns::NEEDLESS_RETURN), - LintId::of(self_named_constructor::SELF_NAMED_CONSTRUCTOR), + LintId::of(self_named_constructors::SELF_NAMED_CONSTRUCTORS), LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS), LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS), LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME), @@ -1762,8 +1762,8 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: LintId::of(large_enum_variant::LARGE_ENUM_VARIANT), LintId::of(loops::MANUAL_MEMCPY), LintId::of(loops::NEEDLESS_COLLECT), - LintId::of(methods::APPEND_INSTEAD_OF_EXTEND), LintId::of(methods::EXPECT_FUN_CALL), + LintId::of(methods::EXTEND_WITH_DRAIN), LintId::of(methods::ITER_NTH), LintId::of(methods::MANUAL_STR_REPEAT), LintId::of(methods::OR_FUN_CALL), @@ -2104,7 +2104,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf: let scripts = conf.allowed_scripts.clone(); store.register_early_pass(move || box disallowed_script_idents::DisallowedScriptIdents::new(&scripts)); store.register_late_pass(|| box strlen_on_c_strings::StrlenOnCStrings); - store.register_late_pass(move || box self_named_constructor::SelfNamedConstructor); + store.register_late_pass(move || box self_named_constructors::SelfNamedConstructors); } #[rustfmt::skip] diff --git a/clippy_lints/src/methods/append_instead_of_extend.rs b/clippy_lints/src/methods/append_instead_of_extend.rs deleted file mode 100644 index e39a5a1efd1..00000000000 --- a/clippy_lints/src/methods/append_instead_of_extend.rs +++ /dev/null @@ -1,41 +0,0 @@ -use clippy_utils::diagnostics::span_lint_and_sugg; -use clippy_utils::source::snippet_with_applicability; -use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; -use if_chain::if_chain; -use rustc_errors::Applicability; -use rustc_hir::{Expr, ExprKind, LangItem}; -use rustc_lint::LateContext; -use rustc_span::symbol::sym; - -use super::APPEND_INSTEAD_OF_EXTEND; - -pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) { - let ty = cx.typeck_results().expr_ty(recv).peel_refs(); - if_chain! { - if is_type_diagnostic_item(cx, ty, sym::vec_type); - //check source object - if let ExprKind::MethodCall(src_method, _, [drain_vec, drain_arg], _) = &arg.kind; - if src_method.ident.as_str() == "drain"; - if let src_ty = cx.typeck_results().expr_ty(drain_vec).peel_refs(); - if is_type_diagnostic_item(cx, src_ty, sym::vec_type); - //check drain range - if let src_ty_range = cx.typeck_results().expr_ty(drain_arg).peel_refs(); - if is_type_lang_item(cx, src_ty_range, LangItem::RangeFull); - then { - let mut applicability = Applicability::MachineApplicable; - span_lint_and_sugg( - cx, - APPEND_INSTEAD_OF_EXTEND, - expr.span, - "use of `extend` instead of `append` for adding the full range of a second vector", - "try this", - format!( - "{}.append(&mut {})", - snippet_with_applicability(cx, recv.span, "..", &mut applicability), - snippet_with_applicability(cx, drain_vec.span, "..", &mut applicability) - ), - applicability, - ); - } - } -} diff --git a/clippy_lints/src/methods/extend_with_drain.rs b/clippy_lints/src/methods/extend_with_drain.rs new file mode 100644 index 00000000000..57e10ce42f8 --- /dev/null +++ b/clippy_lints/src/methods/extend_with_drain.rs @@ -0,0 +1,41 @@ +use clippy_utils::diagnostics::span_lint_and_sugg; +use clippy_utils::source::snippet_with_applicability; +use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item}; +use if_chain::if_chain; +use rustc_errors::Applicability; +use rustc_hir::{Expr, ExprKind, LangItem}; +use rustc_lint::LateContext; +use rustc_span::symbol::sym; + +use super::EXTEND_WITH_DRAIN; + +pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg: &Expr<'_>) { + let ty = cx.typeck_results().expr_ty(recv).peel_refs(); + if_chain! { + if is_type_diagnostic_item(cx, ty, sym::vec_type); + //check source object + if let ExprKind::MethodCall(src_method, _, [drain_vec, drain_arg], _) = &arg.kind; + if src_method.ident.as_str() == "drain"; + if let src_ty = cx.typeck_results().expr_ty(drain_vec).peel_refs(); + if is_type_diagnostic_item(cx, src_ty, sym::vec_type); + //check drain range + if let src_ty_range = cx.typeck_results().expr_ty(drain_arg).peel_refs(); + if is_type_lang_item(cx, src_ty_range, LangItem::RangeFull); + then { + let mut applicability = Applicability::MachineApplicable; + span_lint_and_sugg( + cx, + EXTEND_WITH_DRAIN, + expr.span, + "use of `extend` instead of `append` for adding the full range of a second vector", + "try this", + format!( + "{}.append(&mut {})", + snippet_with_applicability(cx, recv.span, "..", &mut applicability), + snippet_with_applicability(cx, drain_vec.span, "..", &mut applicability) + ), + applicability, + ); + } + } +} diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d3e12023814..5aa29424349 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1,4 +1,3 @@ -mod append_instead_of_extend; mod bind_instead_of_map; mod bytes_nth; mod chars_cmp; @@ -12,6 +11,7 @@ mod clone_on_ref_ptr; mod cloned_instead_of_copied; mod expect_fun_call; mod expect_used; +mod extend_with_drain; mod filetype_is_file; mod filter_map; mod filter_map_identity; @@ -1068,7 +1068,7 @@ declare_clippy_lint! { /// // Good /// a.append(&mut b); /// ``` - pub APPEND_INSTEAD_OF_EXTEND, + pub EXTEND_WITH_DRAIN, perf, "using vec.append(&mut vec) to move the full range of a vecor to another" } @@ -1821,7 +1821,7 @@ impl_lint_pass!(Methods => [ IMPLICIT_CLONE, SUSPICIOUS_SPLITN, MANUAL_STR_REPEAT, - APPEND_INSTEAD_OF_EXTEND + EXTEND_WITH_DRAIN ]); /// Extracts a method call name, args, and `Span` of the method name. @@ -2085,7 +2085,7 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio }, ("extend", [arg]) => { string_extend_chars::check(cx, expr, recv, arg); - append_instead_of_extend::check(cx, expr, recv, arg); + extend_with_drain::check(cx, expr, recv, arg); }, ("filter_map", [arg]) => { unnecessary_filter_map::check(cx, expr, arg); diff --git a/clippy_lints/src/self_named_constructor.rs b/clippy_lints/src/self_named_constructor.rs deleted file mode 100644 index 2123a14cc1b..00000000000 --- a/clippy_lints/src/self_named_constructor.rs +++ /dev/null @@ -1,90 +0,0 @@ -use clippy_utils::diagnostics::span_lint; -use clippy_utils::return_ty; -use clippy_utils::ty::{contains_adt_constructor, contains_ty}; -use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node}; -use rustc_lint::{LateContext, LateLintPass}; -use rustc_session::{declare_lint_pass, declare_tool_lint}; - -declare_clippy_lint! { - /// ### What it does - /// Warns when constructors have the same name as their types. - /// - /// ### Why is this bad? - /// Repeating the name of the type is redundant. - /// - /// ### Example - /// ```rust,ignore - /// struct Foo {} - /// - /// impl Foo { - /// pub fn foo() -> Foo { - /// Foo {} - /// } - /// } - /// ``` - /// Use instead: - /// ```rust,ignore - /// struct Foo {} - /// - /// impl Foo { - /// pub fn new() -> Foo { - /// Foo {} - /// } - /// } - /// ``` - pub SELF_NAMED_CONSTRUCTOR, - style, - "method should not have the same name as the type it is implemented for" -} - -declare_lint_pass!(SelfNamedConstructor => [SELF_NAMED_CONSTRUCTOR]); - -impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructor { - fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { - match impl_item.kind { - ImplItemKind::Fn(ref sig, _) => { - if sig.decl.implicit_self.has_implicit_self() { - return; - } - }, - _ => return, - } - - let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); - let item = cx.tcx.hir().expect_item(parent); - let self_ty = cx.tcx.type_of(item.def_id); - let ret_ty = return_ty(cx, impl_item.hir_id()); - - // Do not check trait impls - if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) { - return; - } - - // Ensure method is constructor-like - if let Some(self_adt) = self_ty.ty_adt_def() { - if !contains_adt_constructor(ret_ty, self_adt) { - return; - } - } else if !contains_ty(ret_ty, self_ty) { - return; - } - - if_chain! { - if let Some(self_def) = self_ty.ty_adt_def(); - if let Some(self_local_did) = self_def.did.as_local(); - let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did); - if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id); - let type_name = x.ident.name.as_str().to_lowercase(); - if impl_item.ident.name.as_str() == type_name || impl_item.ident.name.as_str().replace("_", "") == type_name; - - then { - span_lint( - cx, - SELF_NAMED_CONSTRUCTOR, - impl_item.span, - &format!("constructor `{}` has the same name as the type", impl_item.ident.name), - ); - } - } - } -} diff --git a/clippy_lints/src/self_named_constructors.rs b/clippy_lints/src/self_named_constructors.rs new file mode 100644 index 00000000000..4472edecbed --- /dev/null +++ b/clippy_lints/src/self_named_constructors.rs @@ -0,0 +1,90 @@ +use clippy_utils::diagnostics::span_lint; +use clippy_utils::return_ty; +use clippy_utils::ty::{contains_adt_constructor, contains_ty}; +use rustc_hir::{Impl, ImplItem, ImplItemKind, ItemKind, Node}; +use rustc_lint::{LateContext, LateLintPass}; +use rustc_session::{declare_lint_pass, declare_tool_lint}; + +declare_clippy_lint! { + /// ### What it does + /// Warns when constructors have the same name as their types. + /// + /// ### Why is this bad? + /// Repeating the name of the type is redundant. + /// + /// ### Example + /// ```rust,ignore + /// struct Foo {} + /// + /// impl Foo { + /// pub fn foo() -> Foo { + /// Foo {} + /// } + /// } + /// ``` + /// Use instead: + /// ```rust,ignore + /// struct Foo {} + /// + /// impl Foo { + /// pub fn new() -> Foo { + /// Foo {} + /// } + /// } + /// ``` + pub SELF_NAMED_CONSTRUCTORS, + style, + "method should not have the same name as the type it is implemented for" +} + +declare_lint_pass!(SelfNamedConstructors => [SELF_NAMED_CONSTRUCTORS]); + +impl<'tcx> LateLintPass<'tcx> for SelfNamedConstructors { + fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) { + match impl_item.kind { + ImplItemKind::Fn(ref sig, _) => { + if sig.decl.implicit_self.has_implicit_self() { + return; + } + }, + _ => return, + } + + let parent = cx.tcx.hir().get_parent_item(impl_item.hir_id()); + let item = cx.tcx.hir().expect_item(parent); + let self_ty = cx.tcx.type_of(item.def_id); + let ret_ty = return_ty(cx, impl_item.hir_id()); + + // Do not check trait impls + if matches!(item.kind, ItemKind::Impl(Impl { of_trait: Some(_), .. })) { + return; + } + + // Ensure method is constructor-like + if let Some(self_adt) = self_ty.ty_adt_def() { + if !contains_adt_constructor(ret_ty, self_adt) { + return; + } + } else if !contains_ty(ret_ty, self_ty) { + return; + } + + if_chain! { + if let Some(self_def) = self_ty.ty_adt_def(); + if let Some(self_local_did) = self_def.did.as_local(); + let self_id = cx.tcx.hir().local_def_id_to_hir_id(self_local_did); + if let Some(Node::Item(x)) = cx.tcx.hir().find(self_id); + let type_name = x.ident.name.as_str().to_lowercase(); + if impl_item.ident.name.as_str() == type_name || impl_item.ident.name.as_str().replace("_", "") == type_name; + + then { + span_lint( + cx, + SELF_NAMED_CONSTRUCTORS, + impl_item.span, + &format!("constructor `{}` has the same name as the type", impl_item.ident.name), + ); + } + } + } +} -- cgit 1.4.1-3-g733a5