Skip to content

Commit

Permalink
Fix a wrong suggestion of ref_in_deref
Browse files Browse the repository at this point in the history
  • Loading branch information
giraffate committed Jan 19, 2021
1 parent 91292f1 commit 9663206
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
10 changes: 8 additions & 2 deletions clippy_lints/src/reference.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::sugg::Sugg;
use crate::utils::{in_macro, snippet_opt, snippet_with_applicability, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_ast::ast::{Expr, ExprKind, Mutability, UnOp};
Expand Down Expand Up @@ -124,14 +125,19 @@ impl EarlyLintPass for RefInDeref {
if let ExprKind::Paren(ref parened) = object.kind;
if let ExprKind::AddrOf(_, _, ref inner) = parened.kind;
then {
let mut applicability = Applicability::MachineApplicable;
let applicability = if inner.span.from_expansion() {
Applicability::MaybeIncorrect
} else {
Applicability::MachineApplicable
};
let sugg = Sugg::ast(cx, inner, "_").maybe_par();
span_lint_and_sugg(
cx,
REF_IN_DEREF,
object.span,
"creating a reference that is immediately dereferenced",
"try this",
snippet_with_applicability(cx, inner.span, "_", &mut applicability).to_string(),
sugg.to_string(),
applicability,
);
}
Expand Down
11 changes: 10 additions & 1 deletion tests/ui/unnecessary_ref.fixed
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-rustfix

#![feature(stmt_expr_attributes)]
#![allow(unused_variables)]
#![allow(unused_variables, dead_code)]

struct Outer {
inner: u32,
Expand All @@ -12,3 +12,12 @@ fn main() {
let outer = Outer { inner: 0 };
let inner = outer.inner;
}

struct Apple;
impl Apple {
fn hello(&self) {}
}
struct Package(pub *const Apple);
fn foobar(package: *const Package) {
unsafe { &*(*package).0 }.hello();
}
11 changes: 10 additions & 1 deletion tests/ui/unnecessary_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// run-rustfix

#![feature(stmt_expr_attributes)]
#![allow(unused_variables)]
#![allow(unused_variables, dead_code)]

struct Outer {
inner: u32,
Expand All @@ -12,3 +12,12 @@ fn main() {
let outer = Outer { inner: 0 };
let inner = (&outer).inner;
}

struct Apple;
impl Apple {
fn hello(&self) {}
}
struct Package(pub *const Apple);
fn foobar(package: *const Package) {
unsafe { &*(&*package).0 }.hello();
}
10 changes: 9 additions & 1 deletion tests/ui/unnecessary_ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ note: the lint level is defined here
LL | #[deny(clippy::ref_in_deref)]
| ^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 2 previous errors

0 comments on commit 9663206

Please sign in to comment.