1 //! Checks for usage of `&Vec[_]` and `&String`.
4 use rustc::hir::map::NodeItem;
7 use syntax::ast::NodeId;
8 use utils::{match_type, paths, span_lint};
10 /// **What it does:** This lint checks for function arguments of type `&String` or `&Vec` unless the references are mutable.
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.
14 /// **Known problems:** None
16 /// **Example:** `fn foo(&Vec<u32>) { .. }`
20 "fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` \
21 instead, respectively"
27 impl LintPass for PtrArg {
28 fn get_lints(&self) -> LintArray {
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);
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
47 check_fn(cx, &sig.decl, item.id);
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);
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();
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) {
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) {
73 "writing `&String` instead of `&str` involves a new object where a slice will do. \
74 Consider changing the type to `&str`");