]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/default_trait_access.rs
default_trait_access skips <F as Default>::default()
[rust.git] / clippy_lints / src / default_trait_access.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use rustc::{declare_lint, lint_array};
4 use if_chain::if_chain;
5 use rustc::ty::TyKind;
6
7 use crate::utils::{any_parent_is_automatically_derived, match_def_path, opt_def_id, paths, span_lint_and_sugg};
8
9
10 /// **What it does:** Checks for literal calls to `Default::default()`.
11 ///
12 /// **Why is this bad?** It's more clear to the reader to use the name of the type whose default is
13 /// being gotten than the generic `Default`.
14 ///
15 /// **Known problems:** None.
16 ///
17 /// **Example:**
18 /// ```rust
19 /// // Bad
20 /// let s: String = Default::default();
21 ///
22 /// // Good
23 /// let s = String::default();
24 /// ```
25 declare_clippy_lint! {
26     pub DEFAULT_TRAIT_ACCESS,
27     pedantic,
28     "checks for literal calls to Default::default()"
29 }
30
31 #[derive(Copy, Clone)]
32 pub struct DefaultTraitAccess;
33
34 impl LintPass for DefaultTraitAccess {
35     fn get_lints(&self) -> LintArray {
36         lint_array!(DEFAULT_TRAIT_ACCESS)
37     }
38 }
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for DefaultTraitAccess {
41     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
42         if_chain! {
43             if let ExprKind::Call(ref path, ..) = expr.node;
44             if !any_parent_is_automatically_derived(cx.tcx, expr.id);
45             if let ExprKind::Path(ref qpath) = path.node;
46             if let Some(def_id) = opt_def_id(cx.tables.qpath_def(qpath, path.hir_id));
47             if match_def_path(cx.tcx, def_id, &paths::DEFAULT_TRAIT_METHOD);
48             then {
49                 match qpath {
50                     QPath::Resolved(..) => {
51                         if let ExprKind::Call(ref method, ref _args) = expr.node {
52                             if format!("{:?}", method).contains(" as Default>") {
53                                 return
54                             }
55                         }
56
57
58                         // TODO: Work out a way to put "whatever the imported way of referencing
59                         // this type in this file" rather than a fully-qualified type.
60                         let expr_ty = cx.tables.expr_ty(expr);
61                         if let TyKind::Adt(..) = expr_ty.sty {
62                             let replacement = format!("{}::default()", expr_ty);
63                             span_lint_and_sugg(
64                                 cx,
65                                 DEFAULT_TRAIT_ACCESS,
66                                 expr.span,
67                                 &format!("Calling {} is more clear than this expression", replacement),
68                                 "try",
69                                 replacement);
70                          }
71                     },
72                     QPath::TypeRelative(..) => {},
73                 }
74             }
75          }
76     }
77 }