2022-09-21 12:02:37 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2023-01-12 12:48:13 -06:00
|
|
|
use clippy_utils::msrvs::{self, Msrv};
|
|
|
|
use clippy_utils::source::indent_of;
|
2022-04-26 07:04:23 -05:00
|
|
|
use clippy_utils::{is_default_equivalent, peel_blocks};
|
2022-09-21 12:02:37 -05:00
|
|
|
use rustc_errors::Applicability;
|
2021-09-08 09:31:47 -05:00
|
|
|
use rustc_hir::{
|
2023-01-12 12:48:13 -06:00
|
|
|
def::{CtorKind, CtorOf, DefKind, Res},
|
|
|
|
Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, Ty, TyKind,
|
2021-09-08 09:31:47 -05:00
|
|
|
};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-02-22 09:51:17 -06:00
|
|
|
use rustc_middle::ty::AdtDef;
|
2023-01-12 12:48:13 -06:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-09-08 09:31:47 -05:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Detects manual `std::default::Default` implementations that are identical to a derived implementation.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// It is less concise.
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```rust
|
|
|
|
/// struct Foo {
|
|
|
|
/// bar: bool
|
|
|
|
/// }
|
|
|
|
///
|
2022-06-04 06:34:07 -05:00
|
|
|
/// impl Default for Foo {
|
2021-09-08 09:31:47 -05:00
|
|
|
/// fn default() -> Self {
|
|
|
|
/// Self {
|
|
|
|
/// bar: false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2022-06-16 10:39:06 -05:00
|
|
|
/// Use instead:
|
2021-09-08 09:31:47 -05:00
|
|
|
/// ```rust
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct Foo {
|
|
|
|
/// bar: bool
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Known problems
|
|
|
|
/// Derive macros [sometimes use incorrect bounds](https://github.com/rust-lang/rust/issues/26925)
|
2022-09-09 06:36:26 -05:00
|
|
|
/// in generic types and the user defined `impl` may be more generalized or
|
2021-09-08 09:31:47 -05:00
|
|
|
/// specialized than what derive will produce. This lint can't detect the manual `impl`
|
|
|
|
/// has exactly equal bounds, and therefore this lint is disabled for types with
|
|
|
|
/// generic parameters.
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-09-08 09:31:47 -05:00
|
|
|
pub DERIVABLE_IMPLS,
|
|
|
|
complexity,
|
|
|
|
"manual implementation of the `Default` trait which is equal to a derive"
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:48:13 -06:00
|
|
|
pub struct DerivableImpls {
|
|
|
|
msrv: Msrv,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerivableImpls {
|
|
|
|
#[must_use]
|
|
|
|
pub fn new(msrv: Msrv) -> Self {
|
|
|
|
DerivableImpls { msrv }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]);
|
2021-09-08 09:31:47 -05:00
|
|
|
|
|
|
|
fn is_path_self(e: &Expr<'_>) -> bool {
|
|
|
|
if let ExprKind::Path(QPath::Resolved(_, p)) = e.kind {
|
|
|
|
matches!(p.res, Res::SelfCtor(..) | Res::Def(DefKind::Ctor(..), _))
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-12 12:48:13 -06:00
|
|
|
fn check_struct<'tcx>(
|
|
|
|
cx: &LateContext<'tcx>,
|
|
|
|
item: &'tcx Item<'_>,
|
|
|
|
self_ty: &Ty<'_>,
|
|
|
|
func_expr: &Expr<'_>,
|
|
|
|
adt_def: AdtDef<'_>,
|
|
|
|
) {
|
|
|
|
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 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)),
|
|
|
|
ExprKind::Struct(_, fields, _) => fields.iter().all(|ef| is_default_equivalent(cx, ef.expr)),
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
|
|
|
|
if should_emit {
|
|
|
|
let struct_span = cx.tcx.def_span(adt_def.did());
|
|
|
|
span_lint_and_then(cx, DERIVABLE_IMPLS, item.span, "this `impl` can be derived", |diag| {
|
|
|
|
diag.span_suggestion_hidden(
|
|
|
|
item.span,
|
|
|
|
"remove the manual implementation...",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
diag.span_suggestion(
|
|
|
|
struct_span.shrink_to_lo(),
|
|
|
|
"...and instead derive it",
|
|
|
|
"#[derive(Default)]\n".to_string(),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_enum<'tcx>(cx: &LateContext<'tcx>, item: &'tcx Item<'_>, func_expr: &Expr<'_>, adt_def: AdtDef<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, p)) = &peel_blocks(func_expr).kind;
|
|
|
|
if let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Const), id) = p.res;
|
|
|
|
if let variant_id = cx.tcx.parent(id);
|
|
|
|
if let Some(variant_def) = adt_def.variants().iter().find(|v| v.def_id == variant_id);
|
|
|
|
if variant_def.fields.is_empty();
|
|
|
|
if !variant_def.is_field_list_non_exhaustive();
|
|
|
|
|
|
|
|
then {
|
|
|
|
let enum_span = cx.tcx.def_span(adt_def.did());
|
|
|
|
let indent_enum = indent_of(cx, enum_span).unwrap_or(0);
|
|
|
|
let variant_span = cx.tcx.def_span(variant_def.def_id);
|
|
|
|
let indent_variant = indent_of(cx, variant_span).unwrap_or(0);
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
DERIVABLE_IMPLS,
|
|
|
|
item.span,
|
|
|
|
"this `impl` can be derived",
|
|
|
|
|diag| {
|
|
|
|
diag.span_suggestion_hidden(
|
|
|
|
item.span,
|
|
|
|
"remove the manual implementation...",
|
|
|
|
String::new(),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
diag.span_suggestion(
|
|
|
|
enum_span.shrink_to_lo(),
|
|
|
|
"...and instead derive it...",
|
|
|
|
format!(
|
|
|
|
"#[derive(Default)]\n{indent}",
|
|
|
|
indent = " ".repeat(indent_enum),
|
|
|
|
),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
diag.span_suggestion(
|
|
|
|
variant_span.shrink_to_lo(),
|
|
|
|
"...and mark the default variant",
|
|
|
|
format!(
|
|
|
|
"#[default]\n{indent}",
|
|
|
|
indent = " ".repeat(indent_variant),
|
|
|
|
),
|
|
|
|
Applicability::MachineApplicable
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:31:47 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
|
|
|
|
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let ItemKind::Impl(Impl {
|
|
|
|
of_trait: Some(ref trait_ref),
|
|
|
|
items: [child],
|
2021-09-28 12:03:12 -05:00
|
|
|
self_ty,
|
2021-09-08 09:31:47 -05:00
|
|
|
..
|
|
|
|
}) = item.kind;
|
2022-10-26 22:02:18 -05:00
|
|
|
if !cx.tcx.has_attr(item.owner_id.to_def_id(), sym::automatically_derived);
|
2021-12-06 05:33:31 -06:00
|
|
|
if !item.span.from_expansion();
|
2021-09-08 09:31:47 -05:00
|
|
|
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();
|
|
|
|
if let Some(Node::ImplItem(impl_item)) = cx.tcx.hir().find(impl_item_hir);
|
|
|
|
if let ImplItemKind::Fn(_, b) = &impl_item.kind;
|
|
|
|
if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b);
|
2023-02-07 02:29:48 -06:00
|
|
|
if let Some(adt_def) = cx.tcx.type_of(item.owner_id).subst_identity().ty_adt_def();
|
2022-04-26 07:04:23 -05:00
|
|
|
if let attrs = cx.tcx.hir().attrs(item.hir_id());
|
2021-09-28 12:03:12 -05:00
|
|
|
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());
|
2022-09-21 12:02:37 -05:00
|
|
|
|
2023-01-12 12:48:13 -06:00
|
|
|
then {
|
|
|
|
if adt_def.is_struct() {
|
|
|
|
check_struct(cx, item, self_ty, func_expr, adt_def);
|
|
|
|
} else if adt_def.is_enum() && self.msrv.meets(msrvs::DEFAULT_ENUM_ATTRIBUTE) {
|
|
|
|
check_enum(cx, item, func_expr, adt_def);
|
2021-09-08 09:31:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 12:48:13 -06:00
|
|
|
|
|
|
|
extract_msrv_attr!(LateContext);
|
2021-09-08 09:31:47 -05:00
|
|
|
}
|