Skip to content

Commit 043cf97

Browse files
committed
Auto merge of #6609 - giraffate:fix_wrong_suggestion_of_ref_in_deref, r=llogiq
Fix a wrong suggestion of `ref_in_deref` Fix #6358. changelog: Fix a wrong suggestion of `ref_in_deref`
2 parents 4d381c3 + 9663206 commit 043cf97

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

clippy_lints/src/reference.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::utils::sugg::Sugg;
12
use crate::utils::{in_macro, snippet_opt, snippet_with_applicability, span_lint_and_sugg};
23
use if_chain::if_chain;
34
use rustc_ast::ast::{Expr, ExprKind, Mutability, UnOp};
@@ -124,14 +125,19 @@ impl EarlyLintPass for RefInDeref {
124125
if let ExprKind::Paren(ref parened) = object.kind;
125126
if let ExprKind::AddrOf(_, _, ref inner) = parened.kind;
126127
then {
127-
let mut applicability = Applicability::MachineApplicable;
128+
let applicability = if inner.span.from_expansion() {
129+
Applicability::MaybeIncorrect
130+
} else {
131+
Applicability::MachineApplicable
132+
};
133+
let sugg = Sugg::ast(cx, inner, "_").maybe_par();
128134
span_lint_and_sugg(
129135
cx,
130136
REF_IN_DEREF,
131137
object.span,
132138
"creating a reference that is immediately dereferenced",
133139
"try this",
134-
snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(),
140+
sugg.to_string(),
135141
applicability,
136142
);
137143
}

tests/ui/unnecessary_ref.fixed

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-rustfix
22

33
#![feature(stmt_expr_attributes)]
4-
#![allow(unused_variables)]
4+
#![allow(unused_variables, dead_code)]
55

66
struct Outer {
77
inner: u32,
@@ -12,3 +12,12 @@ fn main() {
1212
let outer = Outer { inner: 0 };
1313
let inner = outer.inner;
1414
}
15+
16+
struct Apple;
17+
impl Apple {
18+
fn hello(&self) {}
19+
}
20+
struct Package(pub *const Apple);
21+
fn foobar(package: *const Package) {
22+
unsafe { &*(*package).0 }.hello();
23+
}

tests/ui/unnecessary_ref.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// run-rustfix
22

33
#![feature(stmt_expr_attributes)]
4-
#![allow(unused_variables)]
4+
#![allow(unused_variables, dead_code)]
55

66
struct Outer {
77
inner: u32,
@@ -12,3 +12,12 @@ fn main() {
1212
let outer = Outer { inner: 0 };
1313
let inner = (&outer).inner;
1414
}
15+
16+
struct Apple;
17+
impl Apple {
18+
fn hello(&self) {}
19+
}
20+
struct Package(pub *const Apple);
21+
fn foobar(package: *const Package) {
22+
unsafe { &*(&*package).0 }.hello();
23+
}

tests/ui/unnecessary_ref.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,13 @@ note: the lint level is defined here
1010
LL | #[deny(clippy::ref_in_deref)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: creating a reference that is immediately dereferenced
14+
--> $DIR/unnecessary_ref.rs:22:16
15+
|
16+
LL | unsafe { &*(&*package).0 }.hello();
17+
| ^^^^^^^^^^^ help: try this: `(*package)`
18+
|
19+
= note: `-D clippy::ref-in-deref` implied by `-D warnings`
20+
21+
error: aborting due to 2 previous errors
1422

0 commit comments

Comments
 (0)