about summary refs log tree commit diff
diff options
context:
space:
mode:
authorhamidreza kalbasi <hamidrezakalbasi@protonmail.com>2021-09-10 18:10:55 +0430
committerhamidreza kalbasi <hamidrezakalbasi@protonmail.com>2021-09-10 19:49:02 +0430
commit5aff720e1adb8efc1903c25c20cdee701c9e4f3a (patch)
tree84825e188b26f77b0c3dccd26493f0c5afc1d0f2
parentd5595e55a240c370761319062ef32872604100e0 (diff)
downloadrust-5aff720e1adb8efc1903c25c20cdee701c9e4f3a.tar.gz
rust-5aff720e1adb8efc1903c25c20cdee701c9e4f3a.zip
fix derivable impl false positives
-rw-r--r--clippy_lints/src/derivable_impls.rs17
-rw-r--r--tests/ui/derivable_impls.rs40
2 files changed, 53 insertions, 4 deletions
diff --git a/clippy_lints/src/derivable_impls.rs b/clippy_lints/src/derivable_impls.rs
index b4c4ca016aa..15252ef96cd 100644
--- a/clippy_lints/src/derivable_impls.rs
+++ b/clippy_lints/src/derivable_impls.rs
@@ -2,10 +2,9 @@ use clippy_utils::diagnostics::span_lint_and_help;
 use clippy_utils::{in_macro, is_automatically_derived, is_default_equivalent, remove_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;
 
@@ -68,6 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
             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());
@@ -80,9 +80,18 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
             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 !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());
             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 {
                     ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),
diff --git a/tests/ui/derivable_impls.rs b/tests/ui/derivable_impls.rs
index 336a743de72..ebbc0c77e32 100644
--- a/tests/ui/derivable_impls.rs
+++ b/tests/ui/derivable_impls.rs
@@ -167,4 +167,44 @@ impl Default for WithoutSelfParan {
     }
 }
 
+// https://github.com/rust-lang/rust-clippy/issues/7655
+
+pub struct SpecializedImpl2<T> {
+    v: Vec<T>,
+}
+
+impl Default for SpecializedImpl2<String> {
+    fn default() -> Self {
+        Self { v: Vec::new() }
+    }
+}
+
+// https://github.com/rust-lang/rust-clippy/issues/7654
+
+pub struct Color {
+    pub r: u8,
+    pub g: u8,
+    pub b: u8,
+}
+
+/// `#000000`
+impl Default for Color {
+    fn default() -> Self {
+        Color { r: 0, g: 0, b: 0 }
+    }
+}
+
+pub struct Color2 {
+    pub r: u8,
+    pub g: u8,
+    pub b: u8,
+}
+
+impl Default for Color2 {
+    /// `#000000`
+    fn default() -> Self {
+        Self { r: 0, g: 0, b: 0 }
+    }
+}
+
 fn main() {}