2020-03-07 14:33:27 +00:00
|
|
|
use crate::utils::{get_parent_expr, implements_trait, snippet, span_lint_and_sugg};
|
2020-01-26 18:48:30 +00:00
|
|
|
use if_chain::if_chain;
|
2020-03-21 18:34:56 +00:00
|
|
|
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX, PREC_PREFIX};
|
2020-01-23 15:28:01 +00:00
|
|
|
use rustc_errors::Applicability;
|
2020-03-07 14:33:27 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-01-23 15:28:01 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-01-26 18:48:30 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
|
|
use rustc_span::source_map::Span;
|
2018-10-03 16:53:39 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2020-01-23 15:28:01 +00:00
|
|
|
/// **What it does:** Checks for explicit `deref()` or `deref_mut()` method calls.
|
|
|
|
///
|
2020-07-26 19:07:07 +00:00
|
|
|
/// **Why is this bad?** Dereferencing by `&*x` or `&mut *x` is clearer and more concise,
|
2020-01-23 15:28:01 +00:00
|
|
|
/// when not part of a method chain.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2020-02-25 22:06:24 +00:00
|
|
|
/// use std::ops::Deref;
|
|
|
|
/// let a: &mut String = &mut String::from("foo");
|
|
|
|
/// let b: &str = a.deref();
|
2020-01-23 15:28:01 +00:00
|
|
|
/// ```
|
|
|
|
/// Could be written as:
|
|
|
|
/// ```rust
|
2020-02-25 22:06:24 +00:00
|
|
|
/// let a: &mut String = &mut String::from("foo");
|
2020-01-23 15:28:01 +00:00
|
|
|
/// let b = &*a;
|
|
|
|
/// ```
|
2020-01-26 18:48:30 +00:00
|
|
|
///
|
2020-01-23 15:28:01 +00:00
|
|
|
/// This lint excludes
|
2020-02-25 22:06:24 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// let _ = d.unwrap().deref();
|
2020-01-23 15:28:01 +00:00
|
|
|
/// ```
|
2020-03-07 14:33:27 +00:00
|
|
|
pub EXPLICIT_DEREF_METHODS,
|
2018-10-03 16:53:39 +00:00
|
|
|
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 => [
|
2020-03-07 14:33:27 +00:00
|
|
|
EXPLICIT_DEREF_METHODS
|
2020-01-23 15:28:01 +00:00
|
|
|
]);
|
2018-10-03 16:53:39 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for Dereferencing {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2018-10-03 16:53:39 +00:00
|
|
|
if_chain! {
|
2020-03-07 14:33:27 +00:00
|
|
|
if !expr.span.from_expansion();
|
2020-06-09 21:44:04 +00:00
|
|
|
if let ExprKind::MethodCall(ref method_name, _, ref args, _) = &expr.kind;
|
2020-01-26 18:48:30 +00:00
|
|
|
if args.len() == 1;
|
|
|
|
|
2018-10-03 16:53:39 +00:00
|
|
|
then {
|
2020-03-07 14:33:27 +00:00
|
|
|
if let Some(parent_expr) = get_parent_expr(cx, expr) {
|
|
|
|
// Check if we have the whole call chain here
|
|
|
|
if let ExprKind::MethodCall(..) = parent_expr.kind {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-21 18:34:56 +00:00
|
|
|
// Check for Expr that we don't want to be linted
|
|
|
|
let precedence = parent_expr.precedence();
|
|
|
|
match precedence {
|
|
|
|
// Lint a Call is ok though
|
|
|
|
ExprPrecedence::Call | ExprPrecedence::AddrOf => (),
|
|
|
|
_ => {
|
|
|
|
if precedence.order() >= PREC_PREFIX && precedence.order() <= PREC_POSTFIX {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 18:48:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:33:27 +00:00
|
|
|
let name = method_name.ident.as_str();
|
|
|
|
lint_deref(cx, &*name, &args[0], args[0].span, expr.span);
|
2018-10-03 16:53:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 18:48:30 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn lint_deref(cx: &LateContext<'_>, method_name: &str, call_expr: &Expr<'_>, var_span: Span, expr_span: Span) {
|
2020-03-07 14:33:27 +00:00
|
|
|
match method_name {
|
2020-01-26 18:48:30 +00:00
|
|
|
"deref" => {
|
2020-07-14 12:59:59 +00:00
|
|
|
let impls_deref_trait = cx.tcx.lang_items().deref_trait().map_or(false, |id| {
|
2020-07-17 08:47:04 +00:00
|
|
|
implements_trait(cx, cx.typeck_results().expr_ty(&call_expr), id, &[])
|
2020-07-14 12:59:59 +00:00
|
|
|
});
|
|
|
|
if impls_deref_trait {
|
2020-02-25 22:06:24 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-03-07 14:33:27 +00:00
|
|
|
EXPLICIT_DEREF_METHODS,
|
2020-02-25 22:06:24 +00:00
|
|
|
expr_span,
|
|
|
|
"explicit deref method call",
|
|
|
|
"try this",
|
|
|
|
format!("&*{}", &snippet(cx, var_span, "..")),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-01-26 18:48:30 +00:00
|
|
|
},
|
|
|
|
"deref_mut" => {
|
2020-07-14 12:59:59 +00:00
|
|
|
let impls_deref_mut_trait = cx.tcx.lang_items().deref_mut_trait().map_or(false, |id| {
|
2020-07-17 08:47:04 +00:00
|
|
|
implements_trait(cx, cx.typeck_results().expr_ty(&call_expr), id, &[])
|
2020-07-14 12:59:59 +00:00
|
|
|
});
|
|
|
|
if impls_deref_mut_trait {
|
2020-02-25 22:06:24 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
2020-03-07 14:33:27 +00:00
|
|
|
EXPLICIT_DEREF_METHODS,
|
2020-02-25 22:06:24 +00:00
|
|
|
expr_span,
|
|
|
|
"explicit deref_mut method call",
|
|
|
|
"try this",
|
|
|
|
format!("&mut *{}", &snippet(cx, var_span, "..")),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
2020-01-26 18:48:30 +00:00
|
|
|
},
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|