From 0ee6128e27496c063bb45fb958c124db133e8baf Mon Sep 17 00:00:00 2001 From: Phil Turnbull Date: Fri, 25 Nov 2016 09:54:07 -0500 Subject: [PATCH] Convert DEREF_ADDROF to EarlyLintPass --- clippy_lints/src/lib.rs | 2 +- clippy_lints/src/reference.rs | 17 ++++++++++++----- tests/compile-fail/formatting.rs | 1 + tests/compile-fail/reference.rs | 12 ++++++++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 508ef023b80f..537c34b4e2c4 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -267,7 +267,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) { reg.register_late_lint_pass(box ok_if_let::Pass); reg.register_late_lint_pass(box if_let_redundant_pattern_matching::Pass); reg.register_late_lint_pass(box partialeq_ne_impl::Pass); - reg.register_late_lint_pass(box reference::Pass); + reg.register_early_lint_pass(box reference::Pass); reg.register_lint_group("clippy_restrictions", vec![ arithmetic::FLOAT_ARITHMETIC, diff --git a/clippy_lints/src/reference.rs b/clippy_lints/src/reference.rs index 97ee9b2a5775..19e8a3c7d146 100644 --- a/clippy_lints/src/reference.rs +++ b/clippy_lints/src/reference.rs @@ -1,4 +1,4 @@ -use rustc::hir::*; +use syntax::ast::{Expr,ExprKind,UnOp}; use rustc::lint::*; use utils::{span_lint_and_then, snippet}; @@ -29,10 +29,17 @@ impl LintPass for Pass { } } -impl LateLintPass for Pass { - fn check_expr(&mut self, cx: &LateContext, e: &Expr) { - if let ExprUnary(UnDeref, ref deref_target) = e.node { - if let ExprAddrOf(_, ref addrof_target) = deref_target.node { +fn without_parens(mut e: &Expr) -> &Expr { + while let ExprKind::Paren(ref child_e) = e.node { + e = child_e; + } + e +} + +impl EarlyLintPass for Pass { + fn check_expr(&mut self, cx: &EarlyContext, e: &Expr) { + if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.node { + if let ExprKind::AddrOf(_, ref addrof_target) = without_parens(deref_target).node { span_lint_and_then( cx, DEREF_ADDROF, diff --git a/tests/compile-fail/formatting.rs b/tests/compile-fail/formatting.rs index 9b8146dc2293..faaae46af718 100644 --- a/tests/compile-fail/formatting.rs +++ b/tests/compile-fail/formatting.rs @@ -5,6 +5,7 @@ #![allow(unused_variables)] #![allow(unused_assignments)] #![allow(if_same_then_else)] +#![allow(deref_addrof)] fn foo() -> bool { true } diff --git a/tests/compile-fail/reference.rs b/tests/compile-fail/reference.rs index 178e7387d3fe..b77afbc12702 100644 --- a/tests/compile-fail/reference.rs +++ b/tests/compile-fail/reference.rs @@ -34,11 +34,23 @@ fn main() { //~|HELP try this //~|SUGGESTION let b = bytes[1..2][0]; + //This produces a suggestion of 'let b = (a);' which + //will trigger the 'unused_parens' lint + let b = *&(a); + //~^ERROR immediately dereferencing a reference + //~|HELP try this + //~|SUGGESTION let b = (a) + let b = *(&a); //~^ERROR immediately dereferencing a reference //~|HELP try this //~|SUGGESTION let b = a; + let b = *((&a)); + //~^ERROR immediately dereferencing a reference + //~|HELP try this + //~|SUGGESTION let b = a + let b = *&&a; //~^ERROR immediately dereferencing a reference //~|HELP try this