]> git.lizzy.rs Git - rust.git/blob - src/ptr_arg.rs
Merge pull request #938 from Manishearth/fix-876
[rust.git] / src / ptr_arg.rs
1 //! Checks for usage of  `&Vec[_]` and `&String`.
2
3 use rustc::hir::*;
4 use rustc::hir::map::NodeItem;
5 use rustc::lint::*;
6 use rustc::ty;
7 use syntax::ast::NodeId;
8 use utils::{match_type, paths, span_lint};
9
10 /// **What it does:** This lint checks for function arguments of type `&String` or `&Vec` unless the references are mutable.
11 ///
12 /// **Why is this bad?** Requiring the argument to be of the specific size makes the function less useful for no benefit; slices in the form of `&[T]` or `&str` usually suffice and can be obtained from other types, too.
13 ///
14 /// **Known problems:** None
15 ///
16 /// **Example:** `fn foo(&Vec<u32>) { .. }`
17 declare_lint! {
18     pub PTR_ARG,
19     Warn,
20     "fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` \
21      instead, respectively"
22 }
23
24 #[derive(Copy,Clone)]
25 pub struct PtrArg;
26
27 impl LintPass for PtrArg {
28     fn get_lints(&self) -> LintArray {
29         lint_array!(PTR_ARG)
30     }
31 }
32
33 impl LateLintPass for PtrArg {
34     fn check_item(&mut self, cx: &LateContext, item: &Item) {
35         if let ItemFn(ref decl, _, _, _, _, _) = item.node {
36             check_fn(cx, decl, item.id);
37         }
38     }
39
40     fn check_impl_item(&mut self, cx: &LateContext, item: &ImplItem) {
41         if let ImplItemKind::Method(ref sig, _) = item.node {
42             if let Some(NodeItem(it)) = cx.tcx.map.find(cx.tcx.map.get_parent(item.id)) {
43                 if let ItemImpl(_, _, _, Some(_), _, _) = it.node {
44                     return; // ignore trait impls
45                 }
46             }
47             check_fn(cx, &sig.decl, item.id);
48         }
49     }
50
51     fn check_trait_item(&mut self, cx: &LateContext, item: &TraitItem) {
52         if let MethodTraitItem(ref sig, _) = item.node {
53             check_fn(cx, &sig.decl, item.id);
54         }
55     }
56 }
57
58 fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) {
59     let fn_ty = cx.tcx.node_id_to_type(fn_id).fn_sig().skip_binder();
60
61     for (arg, ty) in decl.inputs.iter().zip(&fn_ty.inputs) {
62         if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
63             if match_type(cx, ty, &paths::VEC) {
64                 span_lint(cx,
65                           PTR_ARG,
66                           arg.ty.span,
67                           "writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
68                            with non-Vec-based slices. Consider changing the type to `&[...]`");
69             } else if match_type(cx, ty, &paths::STRING) {
70                 span_lint(cx,
71                           PTR_ARG,
72                           arg.ty.span,
73                           "writing `&String` instead of `&str` involves a new object where a slice will do. \
74                            Consider changing the type to `&str`");
75             }
76         }
77     }
78 }