Skip to content

Commit e8cd4e5

Browse files
committed
Auto merge of #7660 - HKalbasi:derivable-impls, r=camsteffen
Fix derivable impl false positives fix #7654 fix #7655 changelog: none (not released)
2 parents d5595e5 + 5aff720 commit e8cd4e5

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

clippy_lints/src/derivable_impls.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::{in_macro, is_automatically_derived, is_default_equivalent, remove_blocks};
33
use rustc_hir::{
44
def::{DefKind, Res},
5-
Body, Expr, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node, QPath,
5+
Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind,
66
};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::TypeFoldable;
98
use rustc_session::{declare_lint_pass, declare_tool_lint};
109
use rustc_span::sym;
1110

@@ -68,6 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
6867
if let ItemKind::Impl(Impl {
6968
of_trait: Some(ref trait_ref),
7069
items: [child],
70+
self_ty,
7171
..
7272
}) = item.kind;
7373
if let attrs = cx.tcx.hir().attrs(item.hir_id());
@@ -80,9 +80,18 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
8080
if let ImplItemKind::Fn(_, b) = &impl_item.kind;
8181
if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b);
8282
if let Some(adt_def) = cx.tcx.type_of(item.def_id).ty_adt_def();
83+
if !attrs.iter().any(|attr| attr.doc_str().is_some());
84+
if let child_attrs = cx.tcx.hir().attrs(impl_item_hir);
85+
if !child_attrs.iter().any(|attr| attr.doc_str().is_some());
8386
then {
84-
if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) {
85-
return;
87+
if let TyKind::Path(QPath::Resolved(_, p)) = self_ty.kind {
88+
if let Some(PathSegment { args: Some(a), .. }) = p.segments.last() {
89+
for arg in a.args {
90+
if !matches!(arg, GenericArg::Lifetime(_)) {
91+
return;
92+
}
93+
}
94+
}
8695
}
8796
let should_emit = match remove_blocks(func_expr).kind {
8897
ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),

tests/ui/derivable_impls.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,44 @@ impl Default for WithoutSelfParan {
167167
}
168168
}
169169

170+
// https://github.com/rust-lang/rust-clippy/issues/7655
171+
172+
pub struct SpecializedImpl2<T> {
173+
v: Vec<T>,
174+
}
175+
176+
impl Default for SpecializedImpl2<String> {
177+
fn default() -> Self {
178+
Self { v: Vec::new() }
179+
}
180+
}
181+
182+
// https://github.com/rust-lang/rust-clippy/issues/7654
183+
184+
pub struct Color {
185+
pub r: u8,
186+
pub g: u8,
187+
pub b: u8,
188+
}
189+
190+
/// `#000000`
191+
impl Default for Color {
192+
fn default() -> Self {
193+
Color { r: 0, g: 0, b: 0 }
194+
}
195+
}
196+
197+
pub struct Color2 {
198+
pub r: u8,
199+
pub g: u8,
200+
pub b: u8,
201+
}
202+
203+
impl Default for Color2 {
204+
/// `#000000`
205+
fn default() -> Self {
206+
Self { r: 0, g: 0, b: 0 }
207+
}
208+
}
209+
170210
fn main() {}

0 commit comments

Comments
 (0)