Skip to content

Commit 5e58c04

Browse files
committed
span_suggestion
1 parent 31b1cce commit 5e58c04

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

clippy_lints/src/borrow_deref_ref.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::snippet_opt;
44
use clippy_utils::ty::implements_trait;
55
use clippy_utils::{get_parent_expr, is_lint_allowed};
6+
use rustc_errors::Applicability;
67
use rustc_hir::{ExprKind, UnOp};
78
use rustc_lint::{LateContext, LateLintPass};
89
use rustc_middle::mir::Mutability;
@@ -82,11 +83,11 @@ impl LateLintPass<'_> for BorrowDerefRef {
8283
e.span,
8384
"deref on an immutable reference",
8485
|diag| {
85-
diag.help(
86-
&format!(
87-
"consider using `{}` if you would like to reborrow",
88-
&snippet_opt(cx, deref_target.span).unwrap(),
89-
)
86+
diag.span_suggestion(
87+
e.span,
88+
"if you would like to reborrow, try removing `&*`",
89+
snippet_opt(cx, deref_target.span).unwrap(),
90+
Applicability::MachineApplicable
9091
);
9192

9293
// has deref trait -> give 2 help
@@ -97,11 +98,14 @@ impl LateLintPass<'_> for BorrowDerefRef {
9798
}
9899
}
99100

100-
diag.help(
101-
&format!(
102-
"consider using `&**{}` if you would like to deref",
101+
diag.span_suggestion(
102+
e.span,
103+
"if you would like to deref, try using `&**`",
104+
format!(
105+
"&**{}",
103106
&snippet_opt(cx, deref_target.span).unwrap(),
104-
)
107+
),
108+
Applicability::MachineApplicable
105109
);
106110

107111
}

tests/ui/borrow_deref_ref.stderr

+11-10
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,36 @@ error: deref on an immutable reference
22
--> $DIR/borrow_deref_ref.rs:6:17
33
|
44
LL | let b = &*a;
5-
| ^^^
5+
| ^^^ help: if you would like to reborrow, try removing `&*`: `a`
66
|
77
= note: `-D clippy::borrow-deref-ref` implied by `-D warnings`
8-
= help: consider using `a` if you would like to reborrow
98

109
error: deref on an immutable reference
1110
--> $DIR/borrow_deref_ref.rs:9:23
1211
|
1312
LL | let x: &str = &*s;
1413
| ^^^
1514
|
16-
= help: consider using `s` if you would like to reborrow
17-
= help: consider using `&**s` if you would like to deref
15+
help: if you would like to reborrow, try removing `&*`
16+
|
17+
LL | let x: &str = s;
18+
| ~
19+
help: if you would like to deref, try using `&**`
20+
|
21+
LL | let x: &str = &**s;
22+
| ~~~~
1823

1924
error: deref on an immutable reference
2025
--> $DIR/borrow_deref_ref.rs:11:22
2126
|
2227
LL | let b = &mut &*bar(&12);
23-
| ^^^^^^^^^^
24-
|
25-
= help: consider using `bar(&12)` if you would like to reborrow
28+
| ^^^^^^^^^^ help: if you would like to reborrow, try removing `&*`: `bar(&12)`
2629

2730
error: deref on an immutable reference
2831
--> $DIR/borrow_deref_ref.rs:54:23
2932
|
3033
LL | let addr_y = &&*x as *const _ as usize; // assert ok
31-
| ^^^
32-
|
33-
= help: consider using `x` if you would like to reborrow
34+
| ^^^ help: if you would like to reborrow, try removing `&*`: `x`
3435

3536
error: aborting due to 4 previous errors
3637

0 commit comments

Comments
 (0)