1 use rustc::hir::def_id::DefId;
4 use rustc::ty::{self, Ty};
5 use syntax::codemap::Span;
6 use crate::utils::paths;
7 use crate::utils::{get_trait_def_id, implements_trait, in_external_macro, return_ty, same_tys, span_lint_and_then};
8 use crate::utils::sugg::DiagnosticBuilderExt;
10 /// **What it does:** Checks for types with a `fn new() -> Self` method and no
12 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
14 /// **Why is this bad?** The user might expect to be able to use
15 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
16 /// type can be constructed without arguments.
18 /// **Known problems:** Hopefully none.
26 /// fn new() -> Self {
37 /// impl Default for Foo {
38 /// fn default() -> Self {
44 /// You can also have `new()` call `Default::default()`.
45 declare_clippy_lint! {
46 pub NEW_WITHOUT_DEFAULT,
48 "`fn new() -> Self` method without `Default` implementation"
51 /// **What it does:** Checks for types with a `fn new() -> Self` method
52 /// and no implementation of
53 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html),
54 /// where the `Default` can be derived by `#[derive(Default)]`.
56 /// **Why is this bad?** The user might expect to be able to use
57 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
58 /// type can be constructed without arguments.
60 /// **Known problems:** Hopefully none.
68 /// fn new() -> Self {
74 /// Just prepend `#[derive(Default)]` before the `struct` definition.
75 declare_clippy_lint! {
76 pub NEW_WITHOUT_DEFAULT_DERIVE,
78 "`fn new() -> Self` without `#[derive]`able `Default` implementation"
81 #[derive(Copy, Clone)]
82 pub struct NewWithoutDefault;
84 impl LintPass for NewWithoutDefault {
85 fn get_lints(&self) -> LintArray {
86 lint_array!(NEW_WITHOUT_DEFAULT, NEW_WITHOUT_DEFAULT_DERIVE)
90 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
91 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx hir::Item) {
92 if let hir::ItemImpl(_, _, _, _, None, _, ref items) = item.node {
93 for assoc_item in items {
94 if let hir::AssociatedItemKind::Method { has_self: false } = assoc_item.kind {
95 let impl_item = cx.tcx.hir.impl_item(assoc_item.id);
96 if in_external_macro(cx, impl_item.span) {
99 if let hir::ImplItemKind::Method(ref sig, _) = impl_item.node {
100 let name = impl_item.ident.name;
101 let id = impl_item.id;
102 if sig.header.constness == hir::Constness::Const {
103 // can't be implemented by default
106 if impl_item.generics.params.iter().any(|gen| match gen.kind {
107 hir::GenericParamKind::Type { .. } => true,
110 // when the result of `new()` depends on a type parameter we should not require
115 if sig.decl.inputs.is_empty() && name == "new" && cx.access_levels.is_reachable(id) {
117 .type_of(cx.tcx.hir.local_def_id(cx.tcx.hir.get_parent(id)));
119 if same_tys(cx, self_ty, return_ty(cx, id));
120 if let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT);
121 if !implements_trait(cx, self_ty, default_trait_id, &[]);
123 if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) {
126 NEW_WITHOUT_DEFAULT_DERIVE,
128 &format!("you should consider deriving a `Default` implementation for `{}`", self_ty),
130 db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
137 &format!("you should consider adding a `Default` implementation for `{}`", self_ty),
139 db.suggest_prepend_item(
143 &create_new_without_default_suggest_msg(self_ty),
158 fn create_new_without_default_suggest_msg(ty: Ty) -> String {
159 #[cfg_attr(rustfmt, rustfmt_skip)]
161 "impl Default for {} {{
162 fn default() -> Self {{
168 fn can_derive_default<'t, 'c>(ty: Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> Option<Span> {
170 ty::TyAdt(adt_def, substs) if adt_def.is_struct() => {
171 for field in adt_def.all_fields() {
172 let f_ty = field.ty(cx.tcx, substs);
173 if !implements_trait(cx, f_ty, default_trait_id, &[]) {
177 Some(cx.tcx.def_span(adt_def.did))