]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/dereference.rs
Stop using in_band_lifetimes
[rust.git] / clippy_lints / src / dereference.rs
1 use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2 use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
3 use clippy_utils::ty::peel_mid_ty_refs;
4 use clippy_utils::{get_parent_expr, get_parent_node, is_lint_allowed, path_to_local};
5 use rustc_ast::util::parser::{PREC_POSTFIX, PREC_PREFIX};
6 use rustc_data_structures::fx::FxIndexMap;
7 use rustc_errors::Applicability;
8 use rustc_hir::{
9     BindingAnnotation, Body, BodyId, BorrowKind, Destination, Expr, ExprKind, HirId, MatchSource, Mutability, Node,
10     Pat, PatKind, UnOp,
11 };
12 use rustc_lint::{LateContext, LateLintPass};
13 use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
14 use rustc_middle::ty::{self, Ty, TyCtxt, TyS, TypeckResults};
15 use rustc_session::{declare_tool_lint, impl_lint_pass};
16 use rustc_span::{symbol::sym, Span};
17 use std::iter;
18
19 declare_clippy_lint! {
20     /// ### What it does
21     /// Checks for explicit `deref()` or `deref_mut()` method calls.
22     ///
23     /// ### Why is this bad?
24     /// Dereferencing by `&*x` or `&mut *x` is clearer and more concise,
25     /// when not part of a method chain.
26     ///
27     /// ### Example
28     /// ```rust
29     /// use std::ops::Deref;
30     /// let a: &mut String = &mut String::from("foo");
31     /// let b: &str = a.deref();
32     /// ```
33     /// Could be written as:
34     /// ```rust
35     /// let a: &mut String = &mut String::from("foo");
36     /// let b = &*a;
37     /// ```
38     ///
39     /// This lint excludes
40     /// ```rust,ignore
41     /// let _ = d.unwrap().deref();
42     /// ```
43     #[clippy::version = "1.44.0"]
44     pub EXPLICIT_DEREF_METHODS,
45     pedantic,
46     "Explicit use of deref or deref_mut method while not in a method chain."
47 }
48
49 declare_clippy_lint! {
50     /// ### What it does
51     /// Checks for address of operations (`&`) that are going to
52     /// be dereferenced immediately by the compiler.
53     ///
54     /// ### Why is this bad?
55     /// Suggests that the receiver of the expression borrows
56     /// the expression.
57     ///
58     /// ### Example
59     /// ```rust
60     /// fn fun(_a: &i32) {}
61     ///
62     /// // Bad
63     /// let x: &i32 = &&&&&&5;
64     /// fun(&x);
65     ///
66     /// // Good
67     /// let x: &i32 = &5;
68     /// fun(x);
69     /// ```
70     #[clippy::version = "pre 1.29.0"]
71     pub NEEDLESS_BORROW,
72     style,
73     "taking a reference that is going to be automatically dereferenced"
74 }
75
76 declare_clippy_lint! {
77     /// ### What it does
78     /// Checks for `ref` bindings which create a reference to a reference.
79     ///
80     /// ### Why is this bad?
81     /// The address-of operator at the use site is clearer about the need for a reference.
82     ///
83     /// ### Example
84     /// ```rust
85     /// // Bad
86     /// let x = Some("");
87     /// if let Some(ref x) = x {
88     ///     // use `x` here
89     /// }
90     ///
91     /// // Good
92     /// let x = Some("");
93     /// if let Some(x) = x {
94     ///     // use `&x` here
95     /// }
96     /// ```
97     #[clippy::version = "1.54.0"]
98     pub REF_BINDING_TO_REFERENCE,
99     pedantic,
100     "`ref` binding to a reference"
101 }
102
103 impl_lint_pass!(Dereferencing => [
104     EXPLICIT_DEREF_METHODS,
105     NEEDLESS_BORROW,
106     REF_BINDING_TO_REFERENCE,
107 ]);
108
109 #[derive(Default)]
110 pub struct Dereferencing {
111     state: Option<(State, StateData)>,
112
113     // While parsing a `deref` method call in ufcs form, the path to the function is itself an
114     // expression. This is to store the id of that expression so it can be skipped when
115     // `check_expr` is called for it.
116     skip_expr: Option<HirId>,
117
118     /// The body the first local was found in. Used to emit lints when the traversal of the body has
119     /// been finished. Note we can't lint at the end of every body as they can be nested within each
120     /// other.
121     current_body: Option<BodyId>,
122     /// The list of locals currently being checked by the lint.
123     /// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
124     /// This is needed for or patterns where one of the branches can be linted, but another can not
125     /// be.
126     ///
127     /// e.g. `m!(x) | Foo::Bar(ref x)`
128     ref_locals: FxIndexMap<HirId, Option<RefPat>>,
129 }
130
131 struct StateData {
132     /// Span of the top level expression
133     span: Span,
134     /// The required mutability
135     target_mut: Mutability,
136 }
137
138 enum State {
139     // Any number of deref method calls.
140     DerefMethod {
141         // The number of calls in a sequence which changed the referenced type
142         ty_changed_count: usize,
143         is_final_ufcs: bool,
144     },
145     DerefedBorrow {
146         count: u32,
147     },
148 }
149
150 // A reference operation considered by this lint pass
151 enum RefOp {
152     Method(Mutability),
153     Deref,
154     AddrOf,
155 }
156
157 struct RefPat {
158     /// Whether every usage of the binding is dereferenced.
159     always_deref: bool,
160     /// The spans of all the ref bindings for this local.
161     spans: Vec<Span>,
162     /// The applicability of this suggestion.
163     app: Applicability,
164     /// All the replacements which need to be made.
165     replacements: Vec<(Span, String)>,
166 }
167
168 impl<'tcx> LateLintPass<'tcx> for Dereferencing {
169     #[allow(clippy::too_many_lines)]
170     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
171         // Skip path expressions from deref calls. e.g. `Deref::deref(e)`
172         if Some(expr.hir_id) == self.skip_expr.take() {
173             return;
174         }
175
176         if let Some(local) = path_to_local(expr) {
177             self.check_local_usage(cx, expr, local);
178         }
179
180         // Stop processing sub expressions when a macro call is seen
181         if expr.span.from_expansion() {
182             if let Some((state, data)) = self.state.take() {
183                 report(cx, expr, state, data);
184             }
185             return;
186         }
187
188         let typeck = cx.typeck_results();
189         let (kind, sub_expr) = if let Some(x) = try_parse_ref_op(cx.tcx, typeck, expr) {
190             x
191         } else {
192             // The whole chain of reference operations has been seen
193             if let Some((state, data)) = self.state.take() {
194                 report(cx, expr, state, data);
195             }
196             return;
197         };
198
199         match (self.state.take(), kind) {
200             (None, kind) => {
201                 let parent = get_parent_node(cx.tcx, expr.hir_id);
202                 let expr_ty = typeck.expr_ty(expr);
203
204                 match kind {
205                     RefOp::Method(target_mut)
206                         if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
207                             && is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
208                     {
209                         self.state = Some((
210                             State::DerefMethod {
211                                 ty_changed_count: if deref_method_same_type(expr_ty, typeck.expr_ty(sub_expr)) {
212                                     0
213                                 } else {
214                                     1
215                                 },
216                                 is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
217                             },
218                             StateData {
219                                 span: expr.span,
220                                 target_mut,
221                             },
222                         ));
223                     },
224                     RefOp::AddrOf => {
225                         // Find the number of times the borrow is auto-derefed.
226                         let mut iter = find_adjustments(cx.tcx, typeck, expr).iter();
227                         if let Some((i, adjust)) = iter.by_ref().enumerate().find_map(|(i, adjust)| {
228                             if !matches!(adjust.kind, Adjust::Deref(_)) {
229                                 Some((i, adjust))
230                             } else if !adjust.target.is_ref() {
231                                 // Add one to the number of references found.
232                                 Some((i + 1, adjust))
233                             } else {
234                                 None
235                             }
236                         }) {
237                             // Found two consecutive derefs. At least one can be removed.
238                             if i > 1 {
239                                 let target_mut = iter::once(adjust)
240                                     .chain(iter)
241                                     .find_map(|adjust| match adjust.kind {
242                                         Adjust::Borrow(AutoBorrow::Ref(_, m)) => Some(m.into()),
243                                         _ => None,
244                                     })
245                                     // This default should never happen. Auto-deref always reborrows.
246                                     .unwrap_or(Mutability::Not);
247                                 self.state = Some((
248                                     // Subtract one for the current borrow expression, and one to cover the last
249                                     // reference which can't be removed (it's either reborrowed, or needed for
250                                     // auto-deref to happen).
251                                     State::DerefedBorrow {
252                                         count:
253                                             // Truncation here would require more than a `u32::MAX` level reference. The compiler
254                                             // does not support this.
255                                             #[allow(clippy::cast_possible_truncation)]
256                                             { i as u32 - 2 }
257                                     },
258                                     StateData {
259                                         span: expr.span,
260                                         target_mut,
261                                     },
262                                 ));
263                             }
264                         }
265                     },
266                     _ => (),
267                 }
268             },
269             (Some((State::DerefMethod { ty_changed_count, .. }, data)), RefOp::Method(_)) => {
270                 self.state = Some((
271                     State::DerefMethod {
272                         ty_changed_count: if deref_method_same_type(typeck.expr_ty(expr), typeck.expr_ty(sub_expr)) {
273                             ty_changed_count
274                         } else {
275                             ty_changed_count + 1
276                         },
277                         is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
278                     },
279                     data,
280                 ));
281             },
282             (Some((State::DerefedBorrow { count }, data)), RefOp::AddrOf) if count != 0 => {
283                 self.state = Some((State::DerefedBorrow { count: count - 1 }, data));
284             },
285
286             (Some((state, data)), _) => report(cx, expr, state, data),
287         }
288     }
289
290     fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
291         if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
292             if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
293                 // This binding id has been seen before. Add this pattern to the list of changes.
294                 if let Some(prev_pat) = opt_prev_pat {
295                     if pat.span.from_expansion() {
296                         // Doesn't match the context of the previous pattern. Can't lint here.
297                         *opt_prev_pat = None;
298                     } else {
299                         prev_pat.spans.push(pat.span);
300                         prev_pat.replacements.push((
301                             pat.span,
302                             snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
303                                 .0
304                                 .into(),
305                         ));
306                     }
307                 }
308                 return;
309             }
310
311             if_chain! {
312                 if !pat.span.from_expansion();
313                 if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
314                 // only lint immutable refs, because borrowed `&mut T` cannot be moved out
315                 if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
316                 then {
317                     let mut app = Applicability::MachineApplicable;
318                     let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
319                     self.current_body = self.current_body.or(cx.enclosing_body);
320                     self.ref_locals.insert(
321                         id,
322                         Some(RefPat {
323                             always_deref: true,
324                             spans: vec![pat.span],
325                             app,
326                             replacements: vec![(pat.span, snip.into())],
327                         }),
328                     );
329                 }
330             }
331         }
332     }
333
334     fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
335         if Some(body.id()) == self.current_body {
336             for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
337                 let replacements = pat.replacements;
338                 let app = pat.app;
339                 span_lint_and_then(
340                     cx,
341                     if pat.always_deref {
342                         NEEDLESS_BORROW
343                     } else {
344                         REF_BINDING_TO_REFERENCE
345                     },
346                     pat.spans,
347                     "this pattern creates a reference to a reference",
348                     |diag| {
349                         diag.multipart_suggestion("try this", replacements, app);
350                     },
351                 );
352             }
353             self.current_body = None;
354         }
355     }
356 }
357
358 fn try_parse_ref_op<'tcx>(
359     tcx: TyCtxt<'tcx>,
360     typeck: &'tcx TypeckResults<'_>,
361     expr: &'tcx Expr<'_>,
362 ) -> Option<(RefOp, &'tcx Expr<'tcx>)> {
363     let (def_id, arg) = match expr.kind {
364         ExprKind::MethodCall(_, _, [arg], _) => (typeck.type_dependent_def_id(expr.hir_id)?, arg),
365         ExprKind::Call(
366             Expr {
367                 kind: ExprKind::Path(path),
368                 hir_id,
369                 ..
370             },
371             [arg],
372         ) => (typeck.qpath_res(path, *hir_id).opt_def_id()?, arg),
373         ExprKind::Unary(UnOp::Deref, sub_expr) if !typeck.expr_ty(sub_expr).is_unsafe_ptr() => {
374             return Some((RefOp::Deref, sub_expr));
375         },
376         ExprKind::AddrOf(BorrowKind::Ref, _, sub_expr) => return Some((RefOp::AddrOf, sub_expr)),
377         _ => return None,
378     };
379     if tcx.is_diagnostic_item(sym::deref_method, def_id) {
380         Some((RefOp::Method(Mutability::Not), arg))
381     } else if tcx.trait_of_item(def_id)? == tcx.lang_items().deref_mut_trait()? {
382         Some((RefOp::Method(Mutability::Mut), arg))
383     } else {
384         None
385     }
386 }
387
388 // Checks whether the type for a deref call actually changed the type, not just the mutability of
389 // the reference.
390 fn deref_method_same_type(result_ty: Ty<'_>, arg_ty: Ty<'_>) -> bool {
391     match (result_ty.kind(), arg_ty.kind()) {
392         (ty::Ref(_, result_ty, _), ty::Ref(_, arg_ty, _)) => TyS::same_type(result_ty, arg_ty),
393
394         // The result type for a deref method is always a reference
395         // Not matching the previous pattern means the argument type is not a reference
396         // This means that the type did change
397         _ => false,
398     }
399 }
400
401 // Checks whether the parent node is a suitable context for switching from a deref method to the
402 // deref operator.
403 fn is_linted_explicit_deref_position(parent: Option<Node<'_>>, child_id: HirId, child_span: Span) -> bool {
404     let parent = match parent {
405         Some(Node::Expr(e)) if e.span.ctxt() == child_span.ctxt() => e,
406         _ => return true,
407     };
408     match parent.kind {
409         // Leave deref calls in the middle of a method chain.
410         // e.g. x.deref().foo()
411         ExprKind::MethodCall(_, _, [self_arg, ..], _) if self_arg.hir_id == child_id => false,
412
413         // Leave deref calls resulting in a called function
414         // e.g. (x.deref())()
415         ExprKind::Call(func_expr, _) if func_expr.hir_id == child_id => false,
416
417         // Makes an ugly suggestion
418         // e.g. *x.deref() => *&*x
419         ExprKind::Unary(UnOp::Deref, _)
420         // Postfix expressions would require parens
421         | ExprKind::Match(_, _, MatchSource::TryDesugar | MatchSource::AwaitDesugar)
422         | ExprKind::Field(..)
423         | ExprKind::Index(..)
424         | ExprKind::Err => false,
425
426         ExprKind::Box(..)
427         | ExprKind::ConstBlock(..)
428         | ExprKind::Array(_)
429         | ExprKind::Call(..)
430         | ExprKind::MethodCall(..)
431         | ExprKind::Tup(..)
432         | ExprKind::Binary(..)
433         | ExprKind::Unary(..)
434         | ExprKind::Lit(..)
435         | ExprKind::Cast(..)
436         | ExprKind::Type(..)
437         | ExprKind::DropTemps(..)
438         | ExprKind::If(..)
439         | ExprKind::Loop(..)
440         | ExprKind::Match(..)
441         | ExprKind::Let(..)
442         | ExprKind::Closure(..)
443         | ExprKind::Block(..)
444         | ExprKind::Assign(..)
445         | ExprKind::AssignOp(..)
446         | ExprKind::Path(..)
447         | ExprKind::AddrOf(..)
448         | ExprKind::Break(..)
449         | ExprKind::Continue(..)
450         | ExprKind::Ret(..)
451         | ExprKind::InlineAsm(..)
452         | ExprKind::LlvmInlineAsm(..)
453         | ExprKind::Struct(..)
454         | ExprKind::Repeat(..)
455         | ExprKind::Yield(..) => true,
456     }
457 }
458
459 /// Adjustments are sometimes made in the parent block rather than the expression itself.
460 fn find_adjustments<'tcx>(
461     tcx: TyCtxt<'tcx>,
462     typeck: &'tcx TypeckResults<'_>,
463     expr: &'tcx Expr<'_>,
464 ) -> &'tcx [Adjustment<'tcx>] {
465     let map = tcx.hir();
466     let mut iter = map.parent_iter(expr.hir_id);
467     let mut prev = expr;
468
469     loop {
470         match typeck.expr_adjustments(prev) {
471             [] => (),
472             a => break a,
473         };
474
475         match iter.next().map(|(_, x)| x) {
476             Some(Node::Block(_)) => {
477                 if let Some((_, Node::Expr(e))) = iter.next() {
478                     prev = e;
479                 } else {
480                     // This shouldn't happen. Blocks are always contained in an expression.
481                     break &[];
482                 }
483             },
484             Some(Node::Expr(&Expr {
485                 kind: ExprKind::Break(Destination { target_id: Ok(id), .. }, _),
486                 ..
487             })) => {
488                 if let Some(Node::Expr(e)) = map.find(id) {
489                     prev = e;
490                     iter = map.parent_iter(id);
491                 } else {
492                     // This shouldn't happen. The destination should exist.
493                     break &[];
494                 }
495             },
496             _ => break &[],
497         }
498     }
499 }
500
501 #[allow(clippy::needless_pass_by_value)]
502 fn report(cx: &LateContext<'_>, expr: &Expr<'_>, state: State, data: StateData) {
503     match state {
504         State::DerefMethod {
505             ty_changed_count,
506             is_final_ufcs,
507         } => {
508             let mut app = Applicability::MachineApplicable;
509             let (expr_str, expr_is_macro_call) = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app);
510             let ty = cx.typeck_results().expr_ty(expr);
511             let (_, ref_count) = peel_mid_ty_refs(ty);
512             let deref_str = if ty_changed_count >= ref_count && ref_count != 0 {
513                 // a deref call changing &T -> &U requires two deref operators the first time
514                 // this occurs. One to remove the reference, a second to call the deref impl.
515                 "*".repeat(ty_changed_count + 1)
516             } else {
517                 "*".repeat(ty_changed_count)
518             };
519             let addr_of_str = if ty_changed_count < ref_count {
520                 // Check if a reborrow from &mut T -> &T is required.
521                 if data.target_mut == Mutability::Not && matches!(ty.kind(), ty::Ref(_, _, Mutability::Mut)) {
522                     "&*"
523                 } else {
524                     ""
525                 }
526             } else if data.target_mut == Mutability::Mut {
527                 "&mut "
528             } else {
529                 "&"
530             };
531
532             let expr_str = if !expr_is_macro_call && is_final_ufcs && expr.precedence().order() < PREC_PREFIX {
533                 format!("({})", expr_str)
534             } else {
535                 expr_str.into_owned()
536             };
537
538             span_lint_and_sugg(
539                 cx,
540                 EXPLICIT_DEREF_METHODS,
541                 data.span,
542                 match data.target_mut {
543                     Mutability::Not => "explicit `deref` method call",
544                     Mutability::Mut => "explicit `deref_mut` method call",
545                 },
546                 "try this",
547                 format!("{}{}{}", addr_of_str, deref_str, expr_str),
548                 app,
549             );
550         },
551         State::DerefedBorrow { .. } => {
552             let mut app = Applicability::MachineApplicable;
553             let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
554             span_lint_and_sugg(
555                 cx,
556                 NEEDLESS_BORROW,
557                 data.span,
558                 &format!(
559                     "this expression borrows a reference (`{}`) that is immediately dereferenced by the compiler",
560                     cx.typeck_results().expr_ty(expr),
561                 ),
562                 "change this to",
563                 snip.into(),
564                 app,
565             );
566         },
567     }
568 }
569
570 impl Dereferencing {
571     fn check_local_usage(&mut self, cx: &LateContext<'_>, e: &Expr<'_>, local: HirId) {
572         if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
573             if let Some(pat) = outer_pat {
574                 // Check for auto-deref
575                 if !matches!(
576                     cx.typeck_results().expr_adjustments(e),
577                     [
578                         Adjustment {
579                             kind: Adjust::Deref(_),
580                             ..
581                         },
582                         Adjustment {
583                             kind: Adjust::Deref(_),
584                             ..
585                         },
586                         ..
587                     ]
588                 ) {
589                     match get_parent_expr(cx, e) {
590                         // Field accesses are the same no matter the number of references.
591                         Some(Expr {
592                             kind: ExprKind::Field(..),
593                             ..
594                         }) => (),
595                         Some(&Expr {
596                             span,
597                             kind: ExprKind::Unary(UnOp::Deref, _),
598                             ..
599                         }) if !span.from_expansion() => {
600                             // Remove explicit deref.
601                             let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
602                             pat.replacements.push((span, snip.into()));
603                         },
604                         Some(parent) if !parent.span.from_expansion() => {
605                             // Double reference might be needed at this point.
606                             if parent.precedence().order() == PREC_POSTFIX {
607                                 // Parentheses would be needed here, don't lint.
608                                 *outer_pat = None;
609                             } else {
610                                 pat.always_deref = false;
611                                 let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
612                                 pat.replacements.push((e.span, format!("&{}", snip)));
613                             }
614                         },
615                         _ if !e.span.from_expansion() => {
616                             // Double reference might be needed at this point.
617                             pat.always_deref = false;
618                             let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
619                             pat.replacements.push((e.span, format!("&{}", snip)));
620                         },
621                         // Edge case for macros. The span of the identifier will usually match the context of the
622                         // binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
623                         // macros
624                         _ => *outer_pat = None,
625                     }
626                 }
627             }
628         }
629     }
630 }