rust-clippy/clippy_lints/src/dereference.rs

81 lines
2.6 KiB
Rust
Raw Normal View History

2020-01-23 15:28:01 +00:00
use rustc_hir::{Expr, ExprKind, QPath};
use rustc_errors::Applicability;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_tool_lint, declare_lint_pass};
2018-10-03 16:53:39 +00:00
use crate::utils::{in_macro, span_lint_and_sugg};
use if_chain::if_chain;
declare_clippy_lint! {
2020-01-23 15:28:01 +00:00
/// **What it does:** Checks for explicit `deref()` or `deref_mut()` method calls.
///
/// **Why is this bad?** Derefencing by `&*x` or `&mut *x` is clearer and more concise,
/// when not part of a method chain.
///
/// **Example:**
/// ```rust
/// let b = a.deref();
/// let c = a.deref_mut();
/// ```
/// Could be written as:
/// ```rust
/// let b = &*a;
/// let c = &mut *a;
/// ```
///
/// This lint excludes
/// ```rust
/// let e = d.unwrap().deref();
/// ```
2018-10-03 16:53:39 +00:00
pub EXPLICIT_DEREF_METHOD,
pedantic,
"Explicit use of deref or deref_mut method while not in a method chain."
}
2020-01-23 15:28:01 +00:00
declare_lint_pass!(Dereferencing => [
EXPLICIT_DEREF_METHOD
]);
2018-10-03 16:53:39 +00:00
2020-01-23 15:28:01 +00:00
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
2018-10-03 16:53:39 +00:00
if in_macro(expr.span) {
return;
}
if_chain! {
// if this is a method call
2020-01-23 15:28:01 +00:00
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.kind;
2018-10-03 16:53:39 +00:00
// on a Path (i.e. a variable/name, not another method)
2020-01-23 15:28:01 +00:00
if let ExprKind::Path(QPath::Resolved(None, path)) = &args[0].kind;
2018-10-03 16:53:39 +00:00
then {
let name = method_name.ident.as_str();
// alter help slightly to account for _mut
match &*name {
"deref" => {
span_lint_and_sugg(
cx,
EXPLICIT_DEREF_METHOD,
expr.span,
"explicit deref method call",
"try this",
format!("&*{}", path),
2020-01-23 15:28:01 +00:00
Applicability::MachineApplicable
2018-10-03 16:53:39 +00:00
);
},
"deref_mut" => {
span_lint_and_sugg(
cx,
EXPLICIT_DEREF_METHOD,
expr.span,
"explicit deref_mut method call",
"try this",
format!("&mut *{}", path),
2020-01-23 15:28:01 +00:00
Applicability::MachineApplicable
2018-10-03 16:53:39 +00:00
);
},
_ => ()
};
}
}
}
}