2020-01-26 19:48:30 +01:00
|
|
|
use crate::utils::{get_parent_expr, method_calls, snippet, span_lint_and_sugg};
|
|
|
|
use if_chain::if_chain;
|
2020-01-23 16:28:01 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-26 19:48:30 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::{Expr, ExprKind, QPath, StmtKind};
|
2020-01-23 16:28:01 +01:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-26 19:48:30 +01:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
use rustc_span::source_map::Span;
|
2018-10-03 17:53:39 +01:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2020-01-23 16:28:01 +01: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;
|
|
|
|
/// ```
|
2020-01-26 19:48:30 +01:00
|
|
|
///
|
2020-01-23 16:28:01 +01:00
|
|
|
/// This lint excludes
|
|
|
|
/// ```rust
|
|
|
|
/// let e = d.unwrap().deref();
|
|
|
|
/// ```
|
2018-10-03 17:53:39 +01:00
|
|
|
pub EXPLICIT_DEREF_METHOD,
|
|
|
|
pedantic,
|
|
|
|
"Explicit use of deref or deref_mut method while not in a method chain."
|
|
|
|
}
|
|
|
|
|
2020-01-23 16:28:01 +01:00
|
|
|
declare_lint_pass!(Dereferencing => [
|
|
|
|
EXPLICIT_DEREF_METHOD
|
|
|
|
]);
|
2018-10-03 17:53:39 +01:00
|
|
|
|
2020-01-23 16:28:01 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Dereferencing {
|
2020-01-26 19:48:30 +01:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext<'a, 'tcx>, stmt: &'tcx hir::Stmt<'_>) {
|
|
|
|
if_chain! {
|
|
|
|
if let StmtKind::Local(ref local) = stmt.kind;
|
|
|
|
if let Some(ref init) = local.init;
|
|
|
|
|
|
|
|
then {
|
|
|
|
match init.kind {
|
|
|
|
ExprKind::Call(ref _method, args) => {
|
|
|
|
for arg in args {
|
|
|
|
if_chain! {
|
|
|
|
// Caller must call only one other function (deref or deref_mut)
|
|
|
|
// otherwise it can lead to error prone suggestions (ex: &*a.len())
|
|
|
|
let (method_names, arg_list, _) = method_calls(arg, 2);
|
|
|
|
if method_names.len() == 1;
|
|
|
|
// Caller must be a variable
|
|
|
|
let variables = arg_list[0];
|
|
|
|
if variables.len() == 1;
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, _)) = variables[0].kind;
|
|
|
|
|
|
|
|
then {
|
|
|
|
let name = method_names[0].as_str();
|
|
|
|
lint_deref(cx, &*name, variables[0].span, arg.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ExprKind::MethodCall(ref method_name, _, ref args) => {
|
|
|
|
if init.span.from_expansion() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if_chain! {
|
|
|
|
if args.len() == 1;
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, _)) = args[0].kind;
|
|
|
|
// Caller must call only one other function (deref or deref_mut)
|
|
|
|
// otherwise it can lead to error prone suggestions (ex: &*a.len())
|
|
|
|
let (method_names, arg_list, _) = method_calls(init, 2);
|
|
|
|
if method_names.len() == 1;
|
|
|
|
// Caller must be a variable
|
|
|
|
let variables = arg_list[0];
|
|
|
|
if variables.len() == 1;
|
|
|
|
if let ExprKind::Path(QPath::Resolved(None, _)) = variables[0].kind;
|
|
|
|
|
|
|
|
then {
|
|
|
|
let name = method_name.ident.as_str();
|
|
|
|
lint_deref(cx, &*name, args[0].span, init.span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ()
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 17:53:39 +01:00
|
|
|
}
|
2020-01-26 19:48:30 +01:00
|
|
|
}
|
2018-10-03 17:53:39 +01:00
|
|
|
|
2020-01-26 19:48:30 +01:00
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
|
2018-10-03 17:53:39 +01:00
|
|
|
if_chain! {
|
2020-01-23 16:28:01 +01:00
|
|
|
if let ExprKind::MethodCall(ref method_name, _, ref args) = &expr.kind;
|
2020-01-26 19:48:30 +01:00
|
|
|
if args.len() == 1;
|
|
|
|
if let Some(parent) = get_parent_expr(cx, &expr);
|
|
|
|
|
2018-10-03 17:53:39 +01:00
|
|
|
then {
|
2020-01-26 19:48:30 +01:00
|
|
|
// Call and MethodCall exprs are better reported using statements
|
|
|
|
match parent.kind {
|
|
|
|
ExprKind::Call(_, _) => return,
|
|
|
|
ExprKind::MethodCall(_, _, _) => return,
|
|
|
|
_ => {
|
|
|
|
let name = method_name.ident.as_str();
|
|
|
|
lint_deref(cx, &*name, args[0].span, expr.span);
|
|
|
|
}
|
|
|
|
}
|
2018-10-03 17:53:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 19:48:30 +01:00
|
|
|
|
|
|
|
fn lint_deref(cx: &LateContext<'_, '_>, fn_name: &str, var_span: Span, expr_span: Span) {
|
|
|
|
match fn_name {
|
|
|
|
"deref" => {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EXPLICIT_DEREF_METHOD,
|
|
|
|
expr_span,
|
|
|
|
"explicit deref method call",
|
|
|
|
"try this",
|
|
|
|
format!("&*{}", &snippet(cx, var_span, "..")),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
"deref_mut" => {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
EXPLICIT_DEREF_METHOD,
|
|
|
|
expr_span,
|
|
|
|
"explicit deref_mut method call",
|
|
|
|
"try this",
|
|
|
|
format!("&mut *{}", &snippet(cx, var_span, "..")),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|