1 use clippy_utils::consts::{constant, Constant};
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::higher;
4 use clippy_utils::source::snippet_with_applicability;
5 use clippy_utils::ty::is_copy;
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::{BorrowKind, Expr, ExprKind, Mutability};
9 use rustc_lint::{LateContext, LateLintPass};
10 use rustc_middle::ty::layout::LayoutOf;
11 use rustc_middle::ty::{self, Ty};
12 use rustc_session::{declare_tool_lint, impl_lint_pass};
13 use rustc_span::source_map::Span;
15 #[allow(clippy::module_name_repetitions)]
16 #[derive(Copy, Clone)]
17 pub struct UselessVec {
18 pub too_large_for_stack: u64,
21 declare_clippy_lint! {
23 /// Checks for usage of `&vec![..]` when using `&[..]` would
26 /// ### Why is this bad?
27 /// This is less efficient.
31 /// # fn foo(my_vec: &[u8]) {}
39 #[clippy::version = "pre 1.29.0"]
45 impl_lint_pass!(UselessVec => [USELESS_VEC]);
47 impl<'tcx> LateLintPass<'tcx> for UselessVec {
48 fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
49 // search for `&vec![_]` expressions where the adjusted type is `&[_]`
51 if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(expr).kind();
52 if let ty::Slice(..) = ty.kind();
53 if let ExprKind::AddrOf(BorrowKind::Ref, mutability, addressee) = expr.kind;
54 if let Some(vec_args) = higher::VecArgs::hir(cx, addressee);
56 self.check_vec_macro(cx, &vec_args, mutability, expr.span);
60 // search for `for _ in vec![…]`
62 if let Some(higher::ForLoop { arg, .. }) = higher::ForLoop::hir(expr);
63 if let Some(vec_args) = higher::VecArgs::hir(cx, arg);
64 if is_copy(cx, vec_type(cx.typeck_results().expr_ty_adjusted(arg)));
66 // report the error around the `vec!` not inside `<std macros>:`
67 let span = arg.span.ctxt().outer_expn_data().call_site;
68 self.check_vec_macro(cx, &vec_args, Mutability::Not, span);
75 fn check_vec_macro<'tcx>(
77 cx: &LateContext<'tcx>,
78 vec_args: &higher::VecArgs<'tcx>,
79 mutability: Mutability,
82 let mut applicability = Applicability::MachineApplicable;
83 let snippet = match *vec_args {
84 higher::VecArgs::Repeat(elem, len) => {
85 if let Some((Constant::Int(len_constant), _)) = constant(cx, cx.typeck_results(), len) {
86 #[allow(clippy::cast_possible_truncation)]
87 if len_constant as u64 * size_of(cx, elem) > self.too_large_for_stack {
95 snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
96 snippet_with_applicability(cx, len.span, "len", &mut applicability)
102 snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
103 snippet_with_applicability(cx, len.span, "len", &mut applicability)
111 higher::VecArgs::Vec(args) => {
112 if let Some(last) = args.iter().last() {
113 #[allow(clippy::cast_possible_truncation)]
114 if args.len() as u64 * size_of(cx, last) > self.too_large_for_stack {
117 let span = args[0].span.to(last.span);
123 snippet_with_applicability(cx, span, "..", &mut applicability)
127 format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
132 Mutability::Mut => "&mut []".into(),
133 Mutability::Not => "&[]".into(),
143 "useless use of `vec!`",
144 "you can use a slice directly",
151 fn size_of(cx: &LateContext<'_>, expr: &Expr<'_>) -> u64 {
152 let ty = cx.typeck_results().expr_ty_adjusted(expr);
153 cx.layout_of(ty).map_or(0, |l| l.size.bytes())
156 /// Returns the item type of the vector (i.e., the `T` in `Vec<T>`).
157 fn vec_type(ty: Ty<'_>) -> Ty<'_> {
158 if let ty::Adt(_, substs) = ty.kind() {
161 panic!("The type of `vec!` is a not a struct?");