Auto merge of #7660 - HKalbasi:derivable-impls, r=camsteffen
Fix derivable impl false positives fix #7654 fix #7655 changelog: none (not released)
This commit is contained in:
commit
e8cd4e5bf0
@ -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 clippy_utils::{in_macro, is_automatically_derived, is_default_equivalent, remove_blocks};
|
||||||
use rustc_hir::{
|
use rustc_hir::{
|
||||||
def::{DefKind, Res},
|
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_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty::TypeFoldable;
|
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::sym;
|
use rustc_span::sym;
|
||||||
|
|
||||||
@ -68,6 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
|
|||||||
if let ItemKind::Impl(Impl {
|
if let ItemKind::Impl(Impl {
|
||||||
of_trait: Some(ref trait_ref),
|
of_trait: Some(ref trait_ref),
|
||||||
items: [child],
|
items: [child],
|
||||||
|
self_ty,
|
||||||
..
|
..
|
||||||
}) = item.kind;
|
}) = item.kind;
|
||||||
if let attrs = cx.tcx.hir().attrs(item.hir_id());
|
if let attrs = cx.tcx.hir().attrs(item.hir_id());
|
||||||
@ -80,10 +80,19 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
|
|||||||
if let ImplItemKind::Fn(_, b) = &impl_item.kind;
|
if let ImplItemKind::Fn(_, b) = &impl_item.kind;
|
||||||
if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b);
|
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 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 {
|
then {
|
||||||
if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
let should_emit = match remove_blocks(func_expr).kind {
|
let should_emit = match remove_blocks(func_expr).kind {
|
||||||
ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),
|
ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),
|
||||||
ExprKind::Call(callee, args)
|
ExprKind::Call(callee, args)
|
||||||
|
@ -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() {}
|
fn main() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user