1 use syntax::ast::{Expr, ExprKind, UnOp};
3 use crate::utils::{snippet, span_lint_and_sugg};
5 /// **What it does:** Checks for usage of `*&` and `*&mut` in expressions.
7 /// **Why is this bad?** Immediately dereferencing a reference is no-op and
8 /// makes the code less clear.
10 /// **Known problems:** Multiple dereference/addrof pairs are not handled so
11 /// the suggested fix for `x = **&&y` is `x = *&y`, which is still incorrect.
15 /// let a = f(*&mut b);
18 declare_clippy_lint! {
21 "use of `*&` or `*&mut` in an expression"
26 impl LintPass for Pass {
27 fn get_lints(&self) -> LintArray {
28 lint_array!(DEREF_ADDROF)
32 fn without_parens(mut e: &Expr) -> &Expr {
33 while let ExprKind::Paren(ref child_e) = e.node {
39 impl EarlyLintPass for Pass {
40 fn check_expr(&mut self, cx: &EarlyContext, e: &Expr) {
42 if let ExprKind::Unary(UnOp::Deref, ref deref_target) = e.node;
43 if let ExprKind::AddrOf(_, ref addrof_target) = without_parens(deref_target).node;
49 "immediately dereferencing a reference",
51 format!("{}", snippet(cx, addrof_target.span, "_")),
58 /// **What it does:** Checks for references in expressions that use
61 /// **Why is this bad?** The reference is a no-op and is automatically
62 /// dereferenced by the compiler and makes the code less clear.
66 /// struct Point(u32, u32);
67 /// let point = Foo(30, 20);
68 /// let x = (&point).x;
70 declare_clippy_lint! {
73 "Use of reference in auto dereference expression."
78 impl LintPass for DerefPass {
79 fn get_lints(&self) -> LintArray {
80 lint_array!(REF_IN_DEREF)
84 impl EarlyLintPass for DerefPass {
85 fn check_expr(&mut self, cx: &EarlyContext, e: &Expr) {
87 if let ExprKind::Field(ref object, ref field_name) = e.node;
88 if let ExprKind::Paren(ref parened) = object.node;
89 if let ExprKind::AddrOf(_, ref inner) = parened.node;
95 "Creating a reference that is immediately dereferenced.",
99 snippet(cx, inner.span, "_"),
100 snippet(cx, field_name.span, "_")