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};
11 /// **What it does:** This lints about type with a `fn new() -> Self` method
12 /// and no implementation of
13 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
15 /// **Why is this bad?** User might expect to be able to use
16 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
17 /// as the type can be
18 /// 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:** This lints about type with a `fn new() -> Self` method
54 /// and no implementation of
55 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
57 /// **Why is this bad?** User might expect to be able to use
58 /// [`Default`](https://doc.rust-lang.org/std/default/trait.Default.html)
59 /// as the type can be
60 /// 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 LateLintPass for NewWithoutDefault {
93 fn check_fn(&mut self, cx: &LateContext, kind: FnKind, decl: &hir::FnDecl, _: &hir::Block, span: Span, id: ast::NodeId) {
94 if in_external_macro(cx, span) {
98 if let FnKind::Method(name, _, _, _) = kind {
99 if decl.inputs.is_empty() && name.as_str() == "new" {
100 let self_ty = cx.tcx.lookup_item_type(cx.tcx.map.local_def_id(
101 cx.tcx.map.get_parent(id))).ty;
103 self_ty.walk_shallow().next().is_none(), // implements_trait does not work with generics
104 let Some(ret_ty) = return_ty(cx, id),
105 same_tys(cx, self_ty, ret_ty, id),
106 let Some(default_trait_id) = get_trait_def_id(cx, &paths::DEFAULT_TRAIT),
107 !implements_trait(cx, self_ty, default_trait_id, Vec::new())
109 if can_derive_default(self_ty, cx, default_trait_id) {
111 NEW_WITHOUT_DEFAULT_DERIVE, span,
112 &format!("you should consider deriving a \
113 `Default` implementation for `{}`",
115 span_suggestion(span,
117 "#[derive(Default)]".into());
120 NEW_WITHOUT_DEFAULT, span,
121 &format!("you should consider adding a \
122 `Default` implementation for `{}`",
124 span_suggestion(span,
126 format!("impl Default for {} {{ fn default() -> \
127 Self {{ {}::new() }} }}", self_ty, self_ty));
135 fn can_derive_default<'t, 'c>(ty: ty::Ty<'t>, cx: &LateContext<'c, 't>, default_trait_id: DefId) -> bool {
137 ty::TyStruct(ref adt_def, ref substs) => {
138 for field in adt_def.all_fields() {
139 let f_ty = field.ty(cx.tcx, substs);
140 if !implements_trait(cx, f_ty, default_trait_id, Vec::new()) {