2021-08-16 10:04:41 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2021-04-01 10:31:11 -05:00
|
|
|
use clippy_utils::{is_automatically_derived, is_default_equivalent, peel_blocks};
|
2021-08-16 10:04:41 -05:00
|
|
|
use rustc_hir::{
|
|
|
|
def::{DefKind, Res},
|
2021-09-10 08:40:55 -05:00
|
|
|
Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind,
|
2021-08-16 10:04:41 -05:00
|
|
|
};
|
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
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
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl std::default::Default for Foo {
|
|
|
|
/// fn default() -> Self {
|
|
|
|
/// Self {
|
|
|
|
/// bar: false
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Could be written as:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// #[derive(Default)]
|
|
|
|
/// struct Foo {
|
|
|
|
/// bar: bool
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Known problems
|
|
|
|
/// Derive macros [sometimes use incorrect bounds](https://github.com/rust-lang/rust/issues/26925)
|
|
|
|
/// in generic types and the user defined `impl` maybe is more generalized or
|
|
|
|
/// 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.
|
|
|
|
///
|
Added `clippy::version` attribute to all normal lints
So, some context for this, well, more a story. I'm not used to scripting, I've never really scripted anything, even if it's a valuable skill. I just never really needed it. Now, `@flip1995` correctly suggested using a script for this in `rust-clippy#7813`...
And I decided to write a script using nushell because why not? This was a mistake... I spend way more time on this than I would like to admit. It has definitely been more than 4 hours. It shouldn't take that long, but me being new to scripting and nushell just wasn't a good mixture... Anyway, here is the script that creates another script which adds the versions. Fun...
Just execute this on the `gh-pages` branch and the resulting `replacer.sh` in `clippy_lints` and it should all work.
```nu
mv v0.0.212 rust-1.00.0;
mv beta rust-1.57.0;
mv master rust-1.58.0;
let paths = (open ./rust-1.58.0/lints.json | select id id_span | flatten | select id path);
let versions = (
ls | where name =~ "rust-" | select name | format {name}/lints.json |
each { open $it | select id | insert version $it | str substring "5,11" version} |
group-by id | rotate counter-clockwise id version |
update version {get version | first 1} | flatten | select id version);
$paths | each { |row|
let version = ($versions | where id == ($row.id) | format {version})
let idu = ($row.id | str upcase)
$"sed -i '0,/($idu),/{s/pub ($idu),/#[clippy::version = "($version)"]\n pub ($idu),/}' ($row.path)"
} | str collect ";" | str find-replace --all '1.00.0' 'pre 1.29.0' | save "replacer.sh";
```
And this still has some problems, but at this point I just want to be done -.-
2021-10-21 14:06:26 -05:00
|
|
|
#[clippy::version = "1.57.0"]
|
2021-08-16 10:04:41 -05:00
|
|
|
pub DERIVABLE_IMPLS,
|
|
|
|
complexity,
|
|
|
|
"manual implementation of the `Default` trait which is equal to a derive"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(DerivableImpls => [DERIVABLE_IMPLS]);
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-10 08:40:55 -05:00
|
|
|
self_ty,
|
2021-08-16 10:04:41 -05:00
|
|
|
..
|
|
|
|
}) = item.kind;
|
|
|
|
if let attrs = cx.tcx.hir().attrs(item.hir_id());
|
|
|
|
if !is_automatically_derived(attrs);
|
2021-10-29 11:14:22 -05:00
|
|
|
if !item.span.from_expansion();
|
2021-08-16 10:04:41 -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);
|
|
|
|
if let Some(adt_def) = cx.tcx.type_of(item.def_id).ty_adt_def();
|
2021-09-10 08:40:55 -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());
|
2021-10-03 04:31:57 -05:00
|
|
|
if adt_def.is_struct();
|
2021-08-16 10:04:41 -05:00
|
|
|
then {
|
2021-09-10 08:40:55 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-16 10:04:41 -05:00
|
|
|
}
|
2021-04-01 10:31:11 -05:00
|
|
|
let should_emit = match peel_blocks(func_expr).kind {
|
2021-08-16 10:04:41 -05:00
|
|
|
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 path_string = cx.tcx.def_path_str(adt_def.did);
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
DERIVABLE_IMPLS,
|
|
|
|
item.span,
|
|
|
|
"this `impl` can be derived",
|
|
|
|
None,
|
|
|
|
&format!("try annotating `{}` with `#[derive(Default)]`", path_string),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|