1 use rustc::hir::intravisit::FnKind;
2 use rustc::hir::def_id::DefId;
7 use syntax::codemap::Span;
9 use utils::{get_trait_def_id, implements_trait, in_external_macro, return_ty, same_tys, span_lint_and_then};
10 use utils::sugg::DiagnosticBuilderExt;
12 /// **What it does:** Checks for types with a `fn new() -> Self` method and no
14 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html).
16 /// **Why is this bad?** The user might expect to be able to use
17 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
18 /// type can be constructed without arguments.
20 /// **Known problems:** Hopefully none.
28 /// fn new() -> Self {
39 /// impl Default for Foo {
40 /// fn default() -> Self {
46 /// You can also have `new()` call `Default::default()`.
48 pub NEW_WITHOUT_DEFAULT,
50 "`fn new() -> Self` method without `Default` implementation"
53 /// **What it does:** Checks for types with a `fn new() -> Self` method
54 /// and no implementation of
55 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html),
56 /// where the `Default` can be derived by `#[derive(Default)]`.
58 /// **Why is this bad?** The user might expect to be able to use
59 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html) as the
60 /// type can be constructed without arguments.
62 /// **Known problems:** Hopefully none.
70 /// fn new() -> Self {
76 /// Just prepend `#[derive(Default)]` before the `struct` definition.
78 pub NEW_WITHOUT_DEFAULT_DERIVE,
80 "`fn new() -> Self` without `#[derive]`able `Default` implementation"
84 pub struct NewWithoutDefault;
86 impl LintPass for NewWithoutDefault {
87 fn get_lints(&self) -> LintArray {
88 lint_array!(NEW_WITHOUT_DEFAULT, NEW_WITHOUT_DEFAULT_DERIVE)
92 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NewWithoutDefault {
95 cx: &LateContext<'a, 'tcx>,
97 decl: &'tcx hir::FnDecl,
102 if in_external_macro(cx, span) {
106 if let FnKind::Method(name, sig, _, _) = kind {
107 if sig.constness == hir::Constness::Const {
108 // can't be implemented by default
111 if decl.inputs.is_empty() && &*name.as_str() == "new" && cx.access_levels.is_reachable(id) {
113 .item_type(cx.tcx.hir.local_def_id(cx.tcx.hir.get_parent(id)));
115 self_ty.walk_shallow().next().is_none(), // implements_trait does not work with generics
116 same_tys(cx, self_ty, return_ty(cx, id), id),
117 let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT),
118 !implements_trait(cx, self_ty, default_trait_id, Vec::new())
120 if let Some(sp) = can_derive_default(self_ty, cx, default_trait_id) {
121 span_lint_and_then(cx,
122 NEW_WITHOUT_DEFAULT_DERIVE, span,
123 &format!("you should consider deriving a \
124 `Default` implementation for `{}`",
127 db.suggest_item_with_attr(cx, sp, "try this", "#[derive(Default)]");
130 span_lint_and_then(cx,
131 NEW_WITHOUT_DEFAULT, span,
132 &format!("you should consider adding a \
133 `Default` implementation for `{}`",
136 db.suggest_prepend_item(cx,
140 "impl Default for {} {{
141 fn default() -> Self {{
154 fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> Option<Span> {
156 ty::TyAdt(adt_def, substs) if adt_def.is_struct() => {
157 for field in adt_def.all_fields() {
158 let f_ty = field.ty(cx.tcx, substs);
159 if !implements_trait(cx, f_ty, default_trait_id, Vec::new()) {
163 cx.tcx.hir.span_if_local(adt_def.did)