Skip to content

Commit 45481c1

Browse files
committed
span_lint_and_then
1 parent 103f626 commit 45481c1

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

clippy_lints/src/needless_deref.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_help;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet_opt;
33
use rustc_hir::{ExprKind, UnOp};
44
use rustc_lint::{LateContext, LateLintPass};
@@ -63,17 +63,25 @@ impl LateLintPass<'_> for NeedlessDeref {
6363
}
6464
}
6565

66-
span_lint_and_help(
66+
span_lint_and_then(
6767
cx,
6868
NEEDLESS_DEREF,
6969
e.span,
7070
"deref on an immutable borrow",
71-
None,
72-
&format!(
73-
"consider using `{}` if you would like to deref\nconsider using `{}` if you would like to reborrow",
74-
"&**".to_owned() + &snippet_opt(cx, deref_target.span).unwrap(),
75-
&snippet_opt(cx, deref_target.span).unwrap(),
76-
)
71+
|diag| {
72+
diag.help(
73+
&format!(
74+
"consider using `{}` if you would like to deref",
75+
"&**".to_owned() + &snippet_opt(cx, deref_target.span).unwrap(),
76+
)
77+
);
78+
diag.help(
79+
&format!(
80+
"consider using `{}` if you would like to reborrow",
81+
&snippet_opt(cx, deref_target.span).unwrap(),
82+
)
83+
);
84+
}
7785
);
7886
}
7987
}

tests/ui/needless_deref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ mod should_not_lint {
2020
foo(&mut &*x); // should not lint
2121
assert_eq!(*x, 0);
2222

23-
foo(&mut x); // should not lint
23+
foo(&mut x);
2424
assert_eq!(*x, 1);
2525
}
2626
}

tests/ui/needless_deref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | let _b = &*a;
66
|
77
= note: `-D clippy::needless-deref` implied by `-D warnings`
88
= help: consider using `&**a` if you would like to deref
9-
consider using `a` if you would like to reborrow
9+
= help: consider using `a` if you would like to reborrow
1010

1111
error: deref on an immutable borrow
1212
--> $DIR/needless_deref.rs:9:23
@@ -15,7 +15,7 @@ LL | let a: &str = &*s;
1515
| ^^^
1616
|
1717
= help: consider using `&**s` if you would like to deref
18-
consider using `s` if you would like to reborrow
18+
= help: consider using `s` if you would like to reborrow
1919

2020
error: aborting due to 2 previous errors
2121

0 commit comments

Comments
 (0)