|
1 |
| -use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg}; |
| 1 | +use crate::utils::{in_macro, snippet_opt, snippet_with_applicability, span_lint_and_sugg}; |
2 | 2 | use if_chain::if_chain;
|
3 |
| -use rustc_ast::ast::{Expr, ExprKind, UnOp}; |
| 3 | +use rustc_ast::ast::{Expr, ExprKind, Mutability, UnOp}; |
4 | 4 | use rustc_errors::Applicability;
|
5 | 5 | use rustc_lint::{EarlyContext, EarlyLintPass};
|
6 | 6 | use rustc_session::{declare_lint_pass, declare_tool_lint};
|
| 7 | +use rustc_span::BytePos; |
7 | 8 |
|
8 | 9 | declare_clippy_lint! {
|
9 | 10 | /// **What it does:** Checks for usage of `*&` and `*&mut` in expressions.
|
@@ -42,17 +43,45 @@ impl EarlyLintPass for DerefAddrOf {
|
42 | 43 | fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
|
43 | 44 | if_chain! {
|
44 | 45 | if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.kind;
|
45 |
| - if let ExprKind::AddrOf(_, _, ref addrof_target) = without_parens(deref_target).kind; |
| 46 | + if let ExprKind::AddrOf(_, ref mutability, ref addrof_target) = without_parens(deref_target).kind; |
46 | 47 | if !in_macro(addrof_target.span);
|
47 | 48 | then {
|
48 | 49 | let mut applicability = Applicability::MachineApplicable;
|
| 50 | + let sugg = if e.span.from_expansion() { |
| 51 | + if let Ok(macro_source) = cx.sess.source_map().span_to_snippet(e.span) { |
| 52 | + // Remove leading whitespace from the given span |
| 53 | + // e.g: ` $visitor` turns into `$visitor` |
| 54 | + let trim_leading_whitespaces = |span| { |
| 55 | + snippet_opt(cx, span).and_then(|snip| { |
| 56 | + #[allow(clippy::cast_possible_truncation)] |
| 57 | + snip.find(|c: char| !c.is_whitespace()).map(|pos| { |
| 58 | + span.lo() + BytePos(pos as u32) |
| 59 | + }) |
| 60 | + }).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace)) |
| 61 | + }; |
| 62 | + |
| 63 | + let rpos = if *mutability == Mutability::Mut { |
| 64 | + macro_source.rfind("mut").expect("already checked this is a mutable reference") + "mut".len() |
| 65 | + } else { |
| 66 | + macro_source.rfind('&').expect("already checked this is a reference") + "&".len() |
| 67 | + }; |
| 68 | + #[allow(clippy::cast_possible_truncation)] |
| 69 | + let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32)); |
| 70 | + let span = trim_leading_whitespaces(span_after_ref); |
| 71 | + snippet_with_applicability(cx, span, "_", &mut applicability) |
| 72 | + } else { |
| 73 | + snippet_with_applicability(cx, e.span, "_", &mut applicability) |
| 74 | + } |
| 75 | + } else { |
| 76 | + snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability) |
| 77 | + }.to_string(); |
49 | 78 | span_lint_and_sugg(
|
50 | 79 | cx,
|
51 | 80 | DEREF_ADDROF,
|
52 | 81 | e.span,
|
53 | 82 | "immediately dereferencing a reference",
|
54 | 83 | "try this",
|
55 |
| - format!("{}", snippet_with_applicability(cx, addrof_target.span, "_", &mut applicability)), |
| 84 | + sugg, |
56 | 85 | applicability,
|
57 | 86 | );
|
58 | 87 | }
|
|
0 commit comments