1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::{is_default_equivalent, peel_blocks};
5 Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind,
7 use rustc_lint::{LateContext, LateLintPass};
8 use rustc_session::{declare_lint_pass, declare_tool_lint};
11 declare_clippy_lint! {
13 /// Detects manual `std::default::Default` implementations that are identical to a derived implementation.
15 /// ### Why is this bad?
16 /// It is less concise.
24 /// impl Default for Foo {
25 /// fn default() -> Self {
35 /// #[derive(Default)]
41 /// ### Known problems
42 /// Derive macros [sometimes use incorrect bounds](https://github.com/rust-lang/rust/issues/26925)
43 /// in generic types and the user defined `impl` maybe is more generalized or
44 /// specialized than what derive will produce. This lint can't detect the manual `impl`
45 /// has exactly equal bounds, and therefore this lint is disabled for types with
46 /// generic parameters.
47 #[clippy::version = "1.57.0"]
50 "manual implementation of the `Default` trait which is equal to a derive"
53 declare_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]);
55 fn is_path_self(e: &Expr<'_>) -> bool {
56 if let ExprKind::Path(QPath::Resolved(_, p)) = e.kind {
57 matches!(p.res, Res::SelfCtor(..) | Res::Def(DefKind::Ctor(..), _))
63 impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
64 fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
66 if let ItemKind::Impl(Impl {
67 of_trait: Some(ref trait_ref),
72 if !cx.tcx.has_attr(item.def_id.to_def_id(), sym::automatically_derived);
73 if !item.span.from_expansion();
74 if let Some(def_id) = trait_ref.trait_def_id();
75 if cx.tcx.is_diagnostic_item(sym::Default, def_id);
76 if let impl_item_hir = child.id.hir_id();
77 if let Some(Node::ImplItem(impl_item)) = cx.tcx.hir().find(impl_item_hir);
78 if let ImplItemKind::Fn(_, b) = &impl_item.kind;
79 if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b);
80 if let Some(adt_def) = cx.tcx.type_of(item.def_id).ty_adt_def();
81 if let attrs = cx.tcx.hir().attrs(item.hir_id());
82 if !attrs.iter().any(|attr| attr.doc_str().is_some());
83 if let child_attrs = cx.tcx.hir().attrs(impl_item_hir);
84 if !child_attrs.iter().any(|attr| attr.doc_str().is_some());
85 if adt_def.is_struct();
87 if let TyKind::Path(QPath::Resolved(_, p)) = self_ty.kind {
88 if let Some(PathSegment { args: Some(a), .. }) = p.segments.last() {
90 if !matches!(arg, GenericArg::Lifetime(_)) {
96 let should_emit = match peel_blocks(func_expr).kind {
97 ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),
98 ExprKind::Call(callee, args)
99 if is_path_self(callee) => args.iter().all(|e| is_default_equivalent(cx, e)),
100 ExprKind::Struct(_, fields, _) => fields.iter().all(|ef| is_default_equivalent(cx, ef.expr)),
104 let path_string = cx.tcx.def_path_str(adt_def.did());
109 "this `impl` can be derived",
111 &format!("try annotating `{}` with `#[derive(Default)]`", path_string),