-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add the derivable_impls
lint
#7570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
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, | ||
}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_middle::ty::TypeFoldable; | ||
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 | ||
HKalbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// ```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. | ||
/// | ||
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], | ||
.. | ||
}) = item.kind; | ||
if let attrs = cx.tcx.hir().attrs(item.hir_id()); | ||
if !is_automatically_derived(attrs); | ||
if !in_macro(item.span); | ||
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(); | ||
then { | ||
if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) { | ||
return; | ||
} | ||
HKalbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let should_emit = match remove_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)), | ||
camsteffen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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), | ||
); | ||
} | ||
} | ||
HKalbasi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.