3 use rustc::ty::{self, Ty};
4 use rustc::ty::subst::Substs;
5 use rustc_const_eval::ConstContext;
6 use syntax::codemap::Span;
7 use utils::{higher, is_copy, snippet, span_lint_and_sugg};
9 /// **What it does:** Checks for usage of `&vec![..]` when using `&[..]` would
12 /// **Why is this bad?** This is less efficient.
14 /// **Known problems:** None.
26 #[derive(Copy, Clone, Debug)]
29 impl LintPass for Pass {
30 fn get_lints(&self) -> LintArray {
31 lint_array!(USELESS_VEC)
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 `&[_]`
39 if let ty::TyRef(_, ref ty) = cx.tables.expr_ty_adjusted(expr).sty;
40 if let ty::TySlice(..) = ty.ty.sty;
41 if let ExprAddrOf(_, ref addressee) = expr.node;
42 if let Some(vec_args) = higher::vec_macro(cx, addressee);
44 check_vec_macro(cx, &vec_args, expr.span);
48 // search for `for _ in vec![…]`
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)));
54 // report the error around the `vec!` not inside `<std macros>:`
55 let span = arg.span.ctxt().outer().expn_info().map(|info| info.call_site).expect("unable to get call_site");
56 check_vec_macro(cx, &vec_args, span);
62 fn check_vec_macro<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
63 let snippet = match *vec_args {
64 higher::VecArgs::Repeat(elem, len) => {
65 let parent_item = cx.tcx.hir.get_parent(len.id);
66 let parent_def_id = cx.tcx.hir.local_def_id(parent_item);
67 let substs = Substs::identity_for_item(cx.tcx, parent_def_id);
68 if ConstContext::new(cx.tcx, cx.param_env.and(substs), cx.tables)
72 format!("&[{}; {}]", snippet(cx, elem.span, "elem"), snippet(cx, len.span, "len"))
77 higher::VecArgs::Vec(args) => if let Some(last) = args.iter().last() {
78 let span = args[0].span.to(last.span);
80 format!("&[{}]", snippet(cx, span, ".."))
90 "useless use of `vec!`",
91 "you can use a slice directly",
96 /// Return the item type of the vector (ie. the `T` in `Vec<T>`).
97 fn vec_type(ty: Ty) -> Ty {
98 if let ty::TyAdt(_, substs) = ty.sty {
101 panic!("The type of `vec!` is a not a struct?");