2016-04-26 13:49:53 +00:00
|
|
|
//! Checks for needless address of operations (`&`)
|
|
|
|
//!
|
|
|
|
//! This lint is **warn** by default
|
|
|
|
|
2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-06-03 06:41:37 +00:00
|
|
|
use clippy_utils::source::{snippet_opt, snippet_with_applicability, snippet_with_context};
|
|
|
|
use clippy_utils::{get_parent_expr, in_macro, path_to_local};
|
2018-11-02 06:56:47 +00:00
|
|
|
use if_chain::if_chain;
|
2021-06-03 06:41:37 +00:00
|
|
|
use rustc_ast::util::parser::PREC_POSTFIX;
|
|
|
|
use rustc_data_structures::fx::FxIndexMap;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-06-03 06:41:37 +00:00
|
|
|
use rustc_hir::{BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, PatKind, UnOp};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty;
|
|
|
|
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-06-03 06:41:37 +00:00
|
|
|
use rustc_span::Span;
|
2016-04-26 13:49:53 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for address of operations (`&`) that are going to
|
2019-03-05 16:50:33 +00:00
|
|
|
/// be dereferenced immediately by the compiler.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Suggests that the receiver of the expression borrows
|
2019-03-05 16:50:33 +00:00
|
|
|
/// the expression.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2020-06-09 14:36:01 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// let x: &i32 = &&&&&&5;
|
|
|
|
///
|
2020-06-09 14:36:01 +00:00
|
|
|
/// // Good
|
|
|
|
/// let x: &i32 = &5;
|
|
|
|
/// ```
|
2016-04-26 13:49:53 +00:00
|
|
|
pub NEEDLESS_BORROW,
|
2021-06-03 06:41:37 +00:00
|
|
|
style,
|
2016-04-26 13:49:53 +00:00
|
|
|
"taking a reference that is going to be automatically dereferenced"
|
|
|
|
}
|
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for `ref` bindings which create a reference to a reference.
|
2021-06-03 06:41:37 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// The address-of operator at the use site is clearer about the need for a reference.
|
2021-06-03 06:41:37 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2021-06-03 06:41:37 +00:00
|
|
|
/// ```rust
|
|
|
|
/// // Bad
|
|
|
|
/// let x = Some("");
|
|
|
|
/// if let Some(ref x) = x {
|
|
|
|
/// // use `x` here
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// let x = Some("");
|
|
|
|
/// if let Some(x) = x {
|
|
|
|
/// // use `&x` here
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
pub REF_BINDING_TO_REFERENCE,
|
|
|
|
pedantic,
|
|
|
|
"`ref` binding to a reference"
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW, REF_BINDING_TO_REFERENCE]);
|
2018-11-02 06:56:47 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct NeedlessBorrow {
|
2021-06-03 06:41:37 +00:00
|
|
|
/// The body the first local was found in. Used to emit lints when the traversal of the body has
|
|
|
|
/// been finished. Note we can't lint at the end of every body as they can be nested within each
|
|
|
|
/// other.
|
|
|
|
current_body: Option<BodyId>,
|
|
|
|
/// The list of locals currently being checked by the lint.
|
|
|
|
/// If the value is `None`, then the binding has been seen as a ref pattern, but is not linted.
|
|
|
|
/// This is needed for or patterns where one of the branches can be linted, but another can not
|
|
|
|
/// be.
|
|
|
|
///
|
|
|
|
/// e.g. `m!(x) | Foo::Bar(ref x)`
|
|
|
|
ref_locals: FxIndexMap<HirId, Option<RefPat>>,
|
2018-11-02 06:56:47 +00:00
|
|
|
}
|
2016-04-26 13:49:53 +00:00
|
|
|
|
2021-06-03 06:41:37 +00:00
|
|
|
struct RefPat {
|
|
|
|
/// Whether every usage of the binding is dereferenced.
|
|
|
|
always_deref: bool,
|
|
|
|
/// The spans of all the ref bindings for this local.
|
|
|
|
spans: Vec<Span>,
|
|
|
|
/// The applicability of this suggestion.
|
|
|
|
app: Applicability,
|
|
|
|
/// All the replacements which need to be made.
|
|
|
|
replacements: Vec<(Span, String)>,
|
|
|
|
}
|
2016-04-26 13:49:53 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
2021-06-03 06:41:37 +00:00
|
|
|
if let Some(local) = path_to_local(e) {
|
|
|
|
self.check_local_usage(cx, e, local);
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.span.from_expansion() {
|
2016-04-26 13:49:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-04-08 15:50:13 +00:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, Mutability::Not, inner) = e.kind {
|
2020-12-20 16:19:49 +00:00
|
|
|
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty(inner).kind() {
|
2020-07-17 08:47:04 +00:00
|
|
|
for adj3 in cx.typeck_results().expr_adjustments(e).windows(3) {
|
2017-09-05 09:33:04 +00:00
|
|
|
if let [Adjustment {
|
2018-11-27 20:14:15 +00:00
|
|
|
kind: Adjust::Deref(_), ..
|
2017-09-05 09:33:04 +00:00
|
|
|
}, Adjustment {
|
2018-11-27 20:14:15 +00:00
|
|
|
kind: Adjust::Deref(_), ..
|
2017-09-05 09:33:04 +00:00
|
|
|
}, Adjustment {
|
|
|
|
kind: Adjust::Borrow(_),
|
|
|
|
..
|
|
|
|
}] = *adj3
|
|
|
|
{
|
2017-09-16 02:27:24 +00:00
|
|
|
span_lint_and_then(
|
2017-09-05 09:33:04 +00:00
|
|
|
cx,
|
|
|
|
NEEDLESS_BORROW,
|
|
|
|
e.span,
|
2020-12-20 16:19:49 +00:00
|
|
|
&format!(
|
|
|
|
"this expression borrows a reference (`&{}`) that is immediately dereferenced \
|
2017-09-16 02:27:24 +00:00
|
|
|
by the compiler",
|
2020-12-20 16:19:49 +00:00
|
|
|
ty
|
|
|
|
),
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
2017-09-16 02:27:24 +00:00
|
|
|
if let Some(snippet) = snippet_opt(cx, inner.span) {
|
2020-04-17 06:08:00 +00:00
|
|
|
diag.span_suggestion(
|
2018-11-02 06:56:47 +00:00
|
|
|
e.span,
|
2018-09-18 15:07:54 +00:00
|
|
|
"change this to",
|
|
|
|
snippet,
|
2018-09-18 17:01:17 +00:00
|
|
|
Applicability::MachineApplicable,
|
2018-09-18 15:07:54 +00:00
|
|
|
);
|
2017-09-16 02:27:24 +00:00
|
|
|
}
|
2017-11-04 19:55:56 +00:00
|
|
|
},
|
2017-09-05 09:33:04 +00:00
|
|
|
);
|
2017-06-04 21:28:01 +00:00
|
|
|
}
|
2016-04-26 13:49:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
|
2021-06-03 06:41:37 +00:00
|
|
|
if let PatKind::Binding(BindingAnnotation::Ref, id, name, _) = pat.kind {
|
|
|
|
if let Some(opt_prev_pat) = self.ref_locals.get_mut(&id) {
|
|
|
|
// This binding id has been seen before. Add this pattern to the list of changes.
|
|
|
|
if let Some(prev_pat) = opt_prev_pat {
|
|
|
|
if in_macro(pat.span) {
|
|
|
|
// Doesn't match the context of the previous pattern. Can't lint here.
|
|
|
|
*opt_prev_pat = None;
|
|
|
|
} else {
|
|
|
|
prev_pat.spans.push(pat.span);
|
|
|
|
prev_pat.replacements.push((
|
|
|
|
pat.span,
|
|
|
|
snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut prev_pat.app)
|
|
|
|
.0
|
|
|
|
.into(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_chain! {
|
|
|
|
if !in_macro(pat.span);
|
|
|
|
if let ty::Ref(_, tam, _) = *cx.typeck_results().pat_ty(pat).kind();
|
|
|
|
// only lint immutable refs, because borrowed `&mut T` cannot be moved out
|
|
|
|
if let ty::Ref(_, _, Mutability::Not) = *tam.kind();
|
|
|
|
then {
|
|
|
|
let mut app = Applicability::MachineApplicable;
|
|
|
|
let snip = snippet_with_context(cx, name.span, pat.span.ctxt(), "..", &mut app).0;
|
|
|
|
self.current_body = self.current_body.or(cx.enclosing_body);
|
|
|
|
self.ref_locals.insert(
|
|
|
|
id,
|
|
|
|
Some(RefPat {
|
|
|
|
always_deref: true,
|
|
|
|
spans: vec![pat.span],
|
|
|
|
app,
|
|
|
|
replacements: vec![(pat.span, snip.into())],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-08-01 14:59:14 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_body_post(&mut self, cx: &LateContext<'tcx>, body: &'tcx Body<'_>) {
|
|
|
|
if Some(body.id()) == self.current_body {
|
|
|
|
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
|
|
|
|
let replacements = pat.replacements;
|
|
|
|
let app = pat.app;
|
2017-10-23 19:18:02 +00:00
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
2021-06-03 06:41:37 +00:00
|
|
|
if pat.always_deref {
|
|
|
|
NEEDLESS_BORROW
|
|
|
|
} else {
|
|
|
|
REF_BINDING_TO_REFERENCE
|
|
|
|
},
|
|
|
|
pat.spans,
|
2017-10-23 19:18:02 +00:00
|
|
|
"this pattern creates a reference to a reference",
|
2020-04-17 06:08:00 +00:00
|
|
|
|diag| {
|
2021-06-03 06:41:37 +00:00
|
|
|
diag.multipart_suggestion("try this", replacements, app);
|
|
|
|
},
|
|
|
|
);
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
self.current_body = None;
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-08-01 14:59:14 +00:00
|
|
|
}
|
2021-06-03 06:41:37 +00:00
|
|
|
}
|
|
|
|
impl NeedlessBorrow {
|
|
|
|
fn check_local_usage(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, local: HirId) {
|
|
|
|
if let Some(outer_pat) = self.ref_locals.get_mut(&local) {
|
|
|
|
if let Some(pat) = outer_pat {
|
|
|
|
// Check for auto-deref
|
|
|
|
if !matches!(
|
|
|
|
cx.typeck_results().expr_adjustments(e),
|
|
|
|
[
|
|
|
|
Adjustment {
|
|
|
|
kind: Adjust::Deref(_),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Adjustment {
|
|
|
|
kind: Adjust::Deref(_),
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
]
|
|
|
|
) {
|
|
|
|
match get_parent_expr(cx, e) {
|
|
|
|
// Field accesses are the same no matter the number of references.
|
|
|
|
Some(Expr {
|
|
|
|
kind: ExprKind::Field(..),
|
|
|
|
..
|
|
|
|
}) => (),
|
|
|
|
Some(&Expr {
|
|
|
|
span,
|
|
|
|
kind: ExprKind::Unary(UnOp::Deref, _),
|
|
|
|
..
|
|
|
|
}) if !in_macro(span) => {
|
|
|
|
// Remove explicit deref.
|
|
|
|
let snip = snippet_with_context(cx, e.span, span.ctxt(), "..", &mut pat.app).0;
|
|
|
|
pat.replacements.push((span, snip.into()));
|
|
|
|
},
|
|
|
|
Some(parent) if !in_macro(parent.span) => {
|
|
|
|
// Double reference might be needed at this point.
|
|
|
|
if parent.precedence().order() == PREC_POSTFIX {
|
|
|
|
// Parentheses would be needed here, don't lint.
|
|
|
|
*outer_pat = None;
|
|
|
|
} else {
|
|
|
|
pat.always_deref = false;
|
|
|
|
let snip = snippet_with_context(cx, e.span, parent.span.ctxt(), "..", &mut pat.app).0;
|
|
|
|
pat.replacements.push((e.span, format!("&{}", snip)));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ if !in_macro(e.span) => {
|
|
|
|
// Double reference might be needed at this point.
|
|
|
|
pat.always_deref = false;
|
|
|
|
let snip = snippet_with_applicability(cx, e.span, "..", &mut pat.app);
|
|
|
|
pat.replacements.push((e.span, format!("&{}", snip)));
|
|
|
|
},
|
|
|
|
// Edge case for macros. The span of the identifier will usually match the context of the
|
|
|
|
// binding, but not if the identifier was created in a macro. e.g. `concat_idents` and proc
|
|
|
|
// macros
|
|
|
|
_ => *outer_pat = None,
|
|
|
|
}
|
|
|
|
}
|
2018-11-02 06:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-04-26 13:49:53 +00:00
|
|
|
}
|