parent
530083c3b9
commit
a9f5b90f0f
6 changed files with 129 additions and 1 deletions
48
clippy_lints/src/reference.rs
Normal file
48
clippy_lints/src/reference.rs
Normal file
|
@ -0,0 +1,48 @@
|
|||
use rustc::hir::*;
|
||||
use rustc::lint::*;
|
||||
use utils::{span_lint_and_then, snippet};
|
||||
|
||||
/// **What it does:** Checks for usage of `*&` and `*&mut` in expressions.
|
||||
///
|
||||
/// **Why is this bad?** Immediately dereferencing a reference is no-op and
|
||||
/// makes the code less clear.
|
||||
///
|
||||
/// **Known problems:** Multiple dereference/addrof pairs are not handled so
|
||||
/// the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect.
|
||||
///
|
||||
/// **Example:**
|
||||
/// ```rust
|
||||
/// let a = f(*&mut b);
|
||||
/// let c = *&d;
|
||||
/// ```
|
||||
declare_lint! {
|
||||
pub DEREF_ADDROF,
|
||||
Warn,
|
||||
"use of `*&` or `*&mut` in an expression"
|
||||
}
|
||||
|
||||
pub struct Pass;
|
||||
|
||||
impl LintPass for Pass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(DEREF_ADDROF)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
DEREF_ADDROF,
|
||||
e.span,
|
||||
"immediately dereferencing a reference",
|
||||
|db| {
|
||||
db.span_suggestion(e.span, "try this",
|
||||
format!("{}", snippet(cx, addrof_target.span, "_")));
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue