]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/vec.rs
Remove import of rustc
[rust.git] / clippy_lints / src / vec.rs
1 use rustc::hir::*;
2 use rustc::lint::*;
3 use rustc::{declare_lint, lint_array};
4 use rustc::ty::{self, Ty};
5 use syntax::codemap::Span;
6 use crate::utils::{higher, is_copy, snippet, span_lint_and_sugg};
7 use crate::consts::constant;
8
9 /// **What it does:** Checks for usage of `&vec![..]` when using `&[..]` would
10 /// be possible.
11 ///
12 /// **Why is this bad?** This is less efficient.
13 ///
14 /// **Known problems:** None.
15 ///
16 /// **Example:**
17 /// ```rust,ignore
18 /// foo(&vec![1, 2])
19 /// ```
20 declare_clippy_lint! {
21     pub USELESS_VEC,
22     perf,
23     "useless `vec!`"
24 }
25
26 #[derive(Copy, Clone, Debug)]
27 pub struct Pass;
28
29 impl LintPass for Pass {
30     fn get_lints(&self) -> LintArray {
31         lint_array!(USELESS_VEC)
32     }
33 }
34
35 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
36     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
37         // search for `&vec![_]` expressions where the adjusted type is `&[_]`
38         if_chain! {
39             if let ty::TyRef(_, ty, _) = cx.tables.expr_ty_adjusted(expr).sty;
40             if let ty::TySlice(..) = ty.sty;
41             if let ExprKind::AddrOf(_, ref addressee) = expr.node;
42             if let Some(vec_args) = higher::vec_macro(cx, addressee);
43             then {
44                 check_vec_macro(cx, &vec_args, expr.span);
45             }
46         }
47
48         // search for `for _ in vec![…]`
49         if_chain! {
50             if let Some((_, arg, _)) = higher::for_loop(expr);
51             if let Some(vec_args) = higher::vec_macro(cx, arg);
52             if is_copy(cx, vec_type(cx.tables.expr_ty_adjusted(arg)));
53             then {
54                 // report the error around the `vec!` not inside `<std macros>:`
55                 let span = arg.span
56                     .ctxt()
57                     .outer()
58                     .expn_info()
59                     .map(|info| info.call_site)
60                     .expect("unable to get call_site");
61                 check_vec_macro(cx, &vec_args, span);
62             }
63         }
64     }
65 }
66
67 fn check_vec_macro<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
68     let snippet = match *vec_args {
69         higher::VecArgs::Repeat(elem, len) => {
70             if constant(cx, cx.tables, len).is_some() {
71                 format!("&[{}; {}]", snippet(cx, elem.span, "elem"), snippet(cx, len.span, "len"))
72             } else {
73                 return;
74             }
75         },
76         higher::VecArgs::Vec(args) => if let Some(last) = args.iter().last() {
77             let span = args[0].span.to(last.span);
78
79             format!("&[{}]", snippet(cx, span, ".."))
80         } else {
81             "&[]".into()
82         },
83     };
84
85     span_lint_and_sugg(
86         cx,
87         USELESS_VEC,
88         span,
89         "useless use of `vec!`",
90         "you can use a slice directly",
91         snippet,
92     );
93 }
94
95 /// Return the item type of the vector (ie. the `T` in `Vec<T>`).
96 fn vec_type(ty: Ty) -> Ty {
97     if let ty::TyAdt(_, substs) = ty.sty {
98         substs.type_at(0)
99     } else {
100         panic!("The type of `vec!` is a not a struct?");
101     }
102 }