|
1 | 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, Mutability}; |
| 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 | 7 | use rustc_span::BytePos;
|
8 |
| -// use rustc_span::source_map::{BytePos, Span}; |
9 | 8 |
|
10 | 9 | declare_clippy_lint! {
|
11 | 10 | /// **What it does:** Checks for usage of `*&` and `*&mut` in expressions.
|
@@ -53,22 +52,20 @@ impl EarlyLintPass for DerefAddrOf {
|
53 | 52 | // Remove leading whitespace from the given span
|
54 | 53 | // e.g: ` $visitor` turns into `$visitor`
|
55 | 54 | let trim_leading_whitespaces = |span| {
|
56 |
| - if let Some(start_no_whitespace) = snippet_opt(cx, span).and_then(|snip| { |
| 55 | + snippet_opt(cx, span).and_then(|snip| { |
| 56 | + #[allow(clippy::cast_possible_truncation)] |
57 | 57 | snip.find(|c: char| !c.is_whitespace()).map(|pos| {
|
58 | 58 | span.lo() + BytePos(pos as u32)
|
59 | 59 | })
|
60 |
| - }) { |
61 |
| - e.span.with_lo(start_no_whitespace) |
62 |
| - } else { |
63 |
| - span |
64 |
| - } |
| 60 | + }).map_or(span, |start_no_whitespace| e.span.with_lo(start_no_whitespace)) |
65 | 61 | };
|
66 | 62 |
|
67 | 63 | let rpos = if *mutability == Mutability::Mut {
|
68 | 64 | macro_source.rfind("mut").expect("already checked this is a mutable reference") + "mut".len()
|
69 | 65 | } else {
|
70 |
| - macro_source.rfind("&").expect("already checked this is a reference") + "&".len() |
| 66 | + macro_source.rfind('&').expect("already checked this is a reference") + "&".len() |
71 | 67 | };
|
| 68 | + #[allow(clippy::cast_possible_truncation)] |
72 | 69 | let span_after_ref = e.span.with_lo(BytePos(e.span.lo().0 + rpos as u32));
|
73 | 70 | let span = trim_leading_whitespaces(span_after_ref);
|
74 | 71 | snippet_with_applicability(cx, span, "_", &mut applicability).to_string()
|
|
0 commit comments