1 //! Checks for usage of `&Vec[_]` and `&String`.
4 use rustc::hir::map::NodeItem;
7 use syntax::ast::NodeId;
8 use utils::{match_path, match_type, paths, span_lint};
10 /// **What it does:** This lint checks for function arguments of type `&String` or `&Vec` unless
11 /// the references are mutable.
13 /// **Why is this bad?** Requiring the argument to be of the specific size makes the function less
14 /// useful for no benefit; slices in the form of `&[T]` or `&str` usually suffice and can be
15 /// obtained from other types, too.
17 /// **Known problems:** None.
21 /// fn foo(&Vec<u32>) { .. }
26 "fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` \
27 instead, respectively"
30 /// **What it does:** This lint checks for equality comparisons with `ptr::null`
32 /// **Why is this bad?** It's easier and more readable to use the inherent `.is_null()`
35 /// **Known problems:** None.
39 /// if x == ptr::null { .. }
44 "comparing a pointer to a null pointer, suggesting to use `.is_null()` instead."
47 /// **What it does:** This lint checks for functions that take immutable references and return
50 /// **Why is this bad?** This is trivially unsound, as one can create two mutable references
51 /// from the same (immutable!) source. This [error](https://github.com/rust-lang/rust/issues/39465)
52 /// actually lead to an interim Rust release 1.15.1.
54 /// **Known problems:** To be on the conservative side, if there's at least one mutable reference
55 /// with the output lifetime, this lint will not trigger. In practice, this case is unlikely anyway.
59 /// fn foo(&Foo) -> &mut Bar { .. }
64 "fns that create mutable refs from immutable ref args"
68 pub struct PointerPass;
70 impl LintPass for PointerPass {
71 fn get_lints(&self) -> LintArray {
72 lint_array!(PTR_ARG, CMP_NULL, MUT_FROM_REF)
76 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PointerPass {
77 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
78 if let ItemFn(ref decl, _, _, _, _, _) = item.node {
79 check_fn(cx, decl, item.id);
83 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
84 if let ImplItemKind::Method(ref sig, _) = item.node {
85 if let Some(NodeItem(it)) = cx.tcx.hir.find(cx.tcx.hir.get_parent(item.id)) {
86 if let ItemImpl(_, _, _, Some(_), _, _) = it.node {
87 return; // ignore trait impls
90 check_fn(cx, &sig.decl, item.id);
94 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
95 if let TraitItemKind::Method(ref sig, _) = item.node {
96 check_fn(cx, &sig.decl, item.id);
100 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
101 if let ExprBinary(ref op, ref l, ref r) = expr.node {
102 if (op.node == BiEq || op.node == BiNe) && (is_null_path(l) || is_null_path(r)) {
106 "Comparing with null is better expressed by the .is_null() method");
112 fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) {
113 let fn_def_id = cx.tcx.hir.local_def_id(fn_id);
114 let fn_ty = cx.tcx.item_type(fn_def_id).fn_sig().skip_binder();
116 for (arg, ty) in decl.inputs.iter().zip(fn_ty.inputs()) {
117 if let ty::TyRef(_, ty::TypeAndMut { ty, mutbl: MutImmutable }) = ty.sty {
118 if match_type(cx, ty, &paths::VEC) {
122 "writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
123 with non-Vec-based slices. Consider changing the type to `&[...]`");
124 } else if match_type(cx, ty, &paths::STRING) {
128 "writing `&String` instead of `&str` involves a new object where a slice will do. \
129 Consider changing the type to `&str`");
134 if let FunctionRetTy::Return(ref ty) = decl.output {
135 if let Some((out, MutMutable)) = get_rptr_lm(ty) {
136 if let Some(MutImmutable) =
139 .filter_map(|ty| get_rptr_lm(ty))
140 .filter(|&(lt, _)| lt.name == out.name)
141 .fold(None, |x, (_, m)| match (x, m) {
142 (Some(MutMutable), _) |
143 (_, MutMutable) => Some(MutMutable),
149 "this function takes an immutable ref to return a mutable one");
155 fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability)> {
156 if let Ty_::TyRptr(ref lt, ref m) = ty.node {
163 fn is_null_path(expr: &Expr) -> bool {
164 if let ExprCall(ref pathexp, ref args) = expr.node {
166 if let ExprPath(ref path) = pathexp.node {
167 return match_path(path, &paths::PTR_NULL) || match_path(path, &paths::PTR_NULL_MUT);