Convert DEREF_ADDROF to EarlyLintPass

This commit is contained in:
Phil Turnbull 2016-11-25 09:54:07 -05:00
parent 8d04038c56
commit 0ee6128e27
4 changed files with 26 additions and 6 deletions

View 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,

View 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 @@ 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,

View file

@ -5,6 +5,7 @@
#![allow(unused_variables)]
#![allow(unused_assignments)]
#![allow(if_same_then_else)]
#![allow(deref_addrof)]
fn foo() -> bool { true }

View 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