]> git.lizzy.rs Git - rust.git/commitdiff
Convert DEREF_ADDROF to EarlyLintPass
authorPhil Turnbull <philip.turnbull@gmail.com>
Fri, 25 Nov 2016 14:54:07 +0000 (09:54 -0500)
committerPhil Turnbull <philip.turnbull@gmail.com>
Fri, 25 Nov 2016 15:33:21 +0000 (10:33 -0500)
clippy_lints/src/lib.rs
clippy_lints/src/reference.rs
tests/compile-fail/formatting.rs
tests/compile-fail/reference.rs

index 508ef023b80f6a780e0a6690e218446a990c63ea..537c34b4e2c4174a5e06877db1c9557214567de2 100644 (file)
@@ -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,
index 97ee9b2a577558768f9fa937eceb6db87275b63c..19e8a3c7d146efa196000aae610fc7b837a598d6 100644 (file)
@@ -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 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-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,
index 9b8146dc2293fff49c67264e02e069d6d791c69d..faaae46af71821a94cc15536af1dd13b3966be2a 100644 (file)
@@ -5,6 +5,7 @@
 #![allow(unused_variables)]
 #![allow(unused_assignments)]
 #![allow(if_same_then_else)]
+#![allow(deref_addrof)]
 
 fn foo() -> bool { true }
 
index 178e7387d3fef9f9e00acf9f94728b32fb7a2235..b77afbc127025ea4969dafa202597b030dd674f1 100644 (file)
@@ -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