1 //! Checks for usage of `&Vec[_]` and `&String`.
4 use rustc::hir::map::NodeItem;
7 use syntax::ast::NodeId;
8 use syntax::codemap::Span;
9 use syntax_pos::MultiSpan;
10 use utils::{match_path, match_type, paths, span_lint, span_lint_and_then};
12 /// **What it does:** This lint checks for function arguments of type `&String`
14 /// the references are mutable.
16 /// **Why is this bad?** Requiring the argument to be of the specific size
17 /// makes the function less
18 /// useful for no benefit; slices in the form of `&[T]` or `&str` usually
19 /// suffice and can be
20 /// obtained from other types, too.
22 /// **Known problems:** None.
26 /// fn foo(&Vec<u32>) { .. }
31 "fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` \
32 instead, respectively"
35 /// **What it does:** This lint checks for equality comparisons with `ptr::null`
37 /// **Why is this bad?** It's easier and more readable to use the inherent
41 /// **Known problems:** None.
45 /// if x == ptr::null { .. }
50 "comparing a pointer to a null pointer, suggesting to use `.is_null()` instead."
53 /// **What it does:** This lint checks for functions that take immutable
54 /// references and return
57 /// **Why is this bad?** This is trivially unsound, as one can create two
58 /// mutable references
59 /// from the same (immutable!) source. This
60 /// [error](https://github.com/rust-lang/rust/issues/39465)
61 /// actually lead to an interim Rust release 1.15.1.
63 /// **Known problems:** To be on the conservative side, if there's at least one
65 /// with the output lifetime, this lint will not trigger. In practice, this
66 /// case is unlikely anyway.
70 /// fn foo(&Foo) -> &mut Bar { .. }
75 "fns that create mutable refs from immutable ref args"
78 #[derive(Copy, Clone)]
79 pub struct PointerPass;
81 impl LintPass for PointerPass {
82 fn get_lints(&self) -> LintArray {
83 lint_array!(PTR_ARG, CMP_NULL, MUT_FROM_REF)
87 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PointerPass {
88 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx Item) {
89 if let ItemFn(ref decl, _, _, _, _, _) = item.node {
90 check_fn(cx, decl, item.id);
94 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx ImplItem) {
95 if let ImplItemKind::Method(ref sig, _) = item.node {
96 if let Some(NodeItem(it)) = cx.tcx.hir.find(cx.tcx.hir.get_parent(item.id)) {
97 if let ItemImpl(_, _, _, _, Some(_), _, _) = it.node {
98 return; // ignore trait impls
101 check_fn(cx, &sig.decl, item.id);
105 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, item: &'tcx TraitItem) {
106 if let TraitItemKind::Method(ref sig, _) = item.node {
107 check_fn(cx, &sig.decl, item.id);
111 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
112 if let ExprBinary(ref op, ref l, ref r) = expr.node {
113 if (op.node == BiEq || op.node == BiNe) && (is_null_path(l) || is_null_path(r)) {
118 "Comparing with null is better expressed by the .is_null() method",
125 fn check_fn(cx: &LateContext, decl: &FnDecl, fn_id: NodeId) {
126 let fn_def_id = cx.tcx.hir.local_def_id(fn_id);
127 let sig = cx.tcx.fn_sig(fn_def_id);
128 let fn_ty = sig.skip_binder();
130 for (arg, ty) in decl.inputs.iter().zip(fn_ty.inputs()) {
137 if match_type(cx, ty, &paths::VEC) {
142 "writing `&Vec<_>` instead of `&[_]` involves one more reference and cannot be used \
143 with non-Vec-based slices. Consider changing the type to `&[...]`",
145 } else if match_type(cx, ty, &paths::STRING) {
150 "writing `&String` instead of `&str` involves a new object where a slice will do. \
151 Consider changing the type to `&str`",
157 if let FunctionRetTy::Return(ref ty) = decl.output {
158 if let Some((out, MutMutable, _)) = get_rptr_lm(ty) {
159 let mut immutables = vec![];
160 for (_, ref mutbl, ref argspan) in
161 decl.inputs.iter().filter_map(|ty| get_rptr_lm(ty)).filter(
162 |&(lt, _, _)| lt.name == out.name,
165 if *mutbl == MutMutable {
168 immutables.push(*argspan);
170 if immutables.is_empty() {
173 span_lint_and_then(cx, MUT_FROM_REF, ty.span, "mutable borrow from immutable input(s)", |db| {
174 let ms = MultiSpan::from_spans(immutables);
175 db.span_note(ms, "immutable borrow here");
181 fn get_rptr_lm(ty: &Ty) -> Option<(&Lifetime, Mutability, Span)> {
182 if let Ty_::TyRptr(ref lt, ref m) = ty.node {
183 Some((lt, m.mutbl, ty.span))
189 fn is_null_path(expr: &Expr) -> bool {
190 if let ExprCall(ref pathexp, ref args) = expr.node {
192 if let ExprPath(ref path) = pathexp.node {
193 return match_path(path, &paths::PTR_NULL) || match_path(path, &paths::PTR_NULL_MUT);