X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Fderivable_impls.rs;h=e98691fd5bb0ac0988185898c174c4f6372af61c;hb=b1a3e7e9c898831dfedf1846664d651a62906505;hp=b4c4ca016aace000f6e8d9ed438e83f5325b7b51;hpb=df7e63b381ef57585d2a449130d42af38918cb3a;p=rust.git diff --git a/clippy_lints/src/derivable_impls.rs b/clippy_lints/src/derivable_impls.rs index b4c4ca016aa..e98691fd5bb 100644 --- a/clippy_lints/src/derivable_impls.rs +++ b/clippy_lints/src/derivable_impls.rs @@ -1,11 +1,10 @@ use clippy_utils::diagnostics::span_lint_and_help; -use clippy_utils::{in_macro, is_automatically_derived, is_default_equivalent, remove_blocks}; +use clippy_utils::{is_default_equivalent, peel_blocks}; use rustc_hir::{ def::{DefKind, Res}, - Body, Expr, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node, QPath, + Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind, }; use rustc_lint::{LateContext, LateLintPass}; -use rustc_middle::ty::TypeFoldable; use rustc_session::{declare_lint_pass, declare_tool_lint}; use rustc_span::sym; @@ -22,7 +21,7 @@ /// bar: bool /// } /// - /// impl std::default::Default for Foo { + /// impl Default for Foo { /// fn default() -> Self { /// Self { /// bar: false @@ -47,6 +46,7 @@ /// has exactly equal bounds, and therefore this lint is disabled for types with /// generic parameters. /// + #[clippy::version = "1.57.0"] pub DERIVABLE_IMPLS, complexity, "manual implementation of the `Default` trait which is equal to a derive" @@ -68,11 +68,11 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { if let ItemKind::Impl(Impl { of_trait: Some(ref trait_ref), items: [child], + self_ty, .. }) = item.kind; - if let attrs = cx.tcx.hir().attrs(item.hir_id()); - if !is_automatically_derived(attrs); - if !in_macro(item.span); + if !cx.tcx.has_attr(item.def_id.to_def_id(), sym::automatically_derived); + if !item.span.from_expansion(); if let Some(def_id) = trait_ref.trait_def_id(); if cx.tcx.is_diagnostic_item(sym::Default, def_id); if let impl_item_hir = child.id.hir_id(); @@ -80,11 +80,22 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { if let ImplItemKind::Fn(_, b) = &impl_item.kind; if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b); if let Some(adt_def) = cx.tcx.type_of(item.def_id).ty_adt_def(); + if let attrs = cx.tcx.hir().attrs(item.hir_id()); + if !attrs.iter().any(|attr| attr.doc_str().is_some()); + if let child_attrs = cx.tcx.hir().attrs(impl_item_hir); + if !child_attrs.iter().any(|attr| attr.doc_str().is_some()); + if adt_def.is_struct(); then { - if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) { - return; + if let TyKind::Path(QPath::Resolved(_, p)) = self_ty.kind { + if let Some(PathSegment { args: Some(a), .. }) = p.segments.last() { + for arg in a.args { + if !matches!(arg, GenericArg::Lifetime(_)) { + return; + } + } + } } - let should_emit = match remove_blocks(func_expr).kind { + let should_emit = match peel_blocks(func_expr).kind { ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)), ExprKind::Call(callee, args) if is_path_self(callee) => args.iter().all(|e| is_default_equivalent(cx, e)), @@ -92,7 +103,7 @@ fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) { _ => false, }; if should_emit { - let path_string = cx.tcx.def_path_str(adt_def.did); + let path_string = cx.tcx.def_path_str(adt_def.did()); span_lint_and_help( cx, DERIVABLE_IMPLS,