Skip to content

Commit 0ee6128

Browse files
Convert DEREF_ADDROF to EarlyLintPass
1 parent 8d04038 commit 0ee6128

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
267267
reg.register_late_lint_pass(box ok_if_let::Pass);
268268
reg.register_late_lint_pass(box if_let_redundant_pattern_matching::Pass);
269269
reg.register_late_lint_pass(box partialeq_ne_impl::Pass);
270-
reg.register_late_lint_pass(box reference::Pass);
270+
reg.register_early_lint_pass(box reference::Pass);
271271

272272
reg.register_lint_group("clippy_restrictions", vec![
273273
arithmetic::FLOAT_ARITHMETIC,

clippy_lints/src/reference.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::hir::*;
1+
use syntax::ast::{Expr,ExprKind,UnOp};
22
use rustc::lint::*;
33
use utils::{span_lint_and_then, snippet};
44

@@ -29,10 +29,17 @@ impl LintPass for Pass {
2929
}
3030
}
3131

32-
impl LateLintPass for Pass {
33-
fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
34-
if let ExprUnary(UnDeref, ref deref_target) = e.node {
35-
if let ExprAddrOf(_, ref addrof_target) = deref_target.node {
32+
fn without_parens(mut e: &Expr) -> &Expr {
33+
while let ExprKind::Paren(ref child_e) = e.node {
34+
e = child_e;
35+
}
36+
e
37+
}
38+
39+
impl EarlyLintPass for Pass {
40+
fn check_expr(&mut self, cx: &EarlyContext, e: &Expr) {
41+
if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.node {
42+
if let ExprKind::AddrOf(_, ref addrof_target) = without_parens(deref_target).node {
3643
span_lint_and_then(
3744
cx,
3845
DEREF_ADDROF,

tests/compile-fail/formatting.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![allow(unused_variables)]
66
#![allow(unused_assignments)]
77
#![allow(if_same_then_else)]
8+
#![allow(deref_addrof)]
89

910
fn foo() -> bool { true }
1011

tests/compile-fail/reference.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,23 @@ fn main() {
3434
//~|HELP try this
3535
//~|SUGGESTION let b = bytes[1..2][0];
3636

37+
//This produces a suggestion of 'let b = (a);' which
38+
//will trigger the 'unused_parens' lint
39+
let b = *&(a);
40+
//~^ERROR immediately dereferencing a reference
41+
//~|HELP try this
42+
//~|SUGGESTION let b = (a)
43+
3744
let b = *(&a);
3845
//~^ERROR immediately dereferencing a reference
3946
//~|HELP try this
4047
//~|SUGGESTION let b = a;
4148

49+
let b = *((&a));
50+
//~^ERROR immediately dereferencing a reference
51+
//~|HELP try this
52+
//~|SUGGESTION let b = a
53+
4254
let b = *&&a;
4355
//~^ERROR immediately dereferencing a reference
4456
//~|HELP try this

0 commit comments

Comments
 (0)