Skip to content

Commit

Permalink
Improve needless_borrow lint
Browse files Browse the repository at this point in the history
Suggest changing usages of ref bindings to match the new type
Split out some cases into new lint `ref_binding_to_reference`
  • Loading branch information
Jarcho committed May 19, 2021
1 parent e0204af commit 7f40dc0
Show file tree
Hide file tree
Showing 13 changed files with 626 additions and 102 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2622,6 +2622,7 @@ Released 2018-09-13
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
[`redundant_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_slicing
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
[`ref_binding_to_reference`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_binding_to_reference
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
needless_bool::BOOL_COMPARISON,
needless_bool::NEEDLESS_BOOL,
needless_borrow::NEEDLESS_BORROW,
needless_borrow::REF_BINDING_TO_REFERENCE,
needless_borrowed_ref::NEEDLESS_BORROWED_REFERENCE,
needless_continue::NEEDLESS_CONTINUE,
needless_for_each::NEEDLESS_FOR_EACH,
Expand Down Expand Up @@ -1093,7 +1094,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box zero_div_zero::ZeroDiv);
store.register_late_pass(|| box mutex_atomic::Mutex);
store.register_late_pass(|| box needless_update::NeedlessUpdate);
store.register_late_pass(|| box needless_borrow::NeedlessBorrow);
store.register_late_pass(|| box needless_borrow::NeedlessBorrow::default());
store.register_late_pass(|| box needless_borrowed_ref::NeedlessBorrowedRef);
store.register_late_pass(|| box no_effect::NoEffect);
store.register_late_pass(|| box temporary_assignment::TemporaryAssignment);
Expand Down Expand Up @@ -1401,6 +1402,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(misc_early::UNSEPARATED_LITERAL_SUFFIX),
LintId::of(mut_mut::MUT_MUT),
LintId::of(needless_bitwise_bool::NEEDLESS_BITWISE_BOOL),
LintId::of(needless_borrow::REF_BINDING_TO_REFERENCE),
LintId::of(needless_continue::NEEDLESS_CONTINUE),
LintId::of(needless_for_each::NEEDLESS_FOR_EACH),
LintId::of(needless_pass_by_value::NEEDLESS_PASS_BY_VALUE),
Expand Down
203 changes: 178 additions & 25 deletions clippy_lints/src/needless_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
//! This lint is **warn** by default
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::source::snippet_opt;
use clippy_utils::source::{snippet_opt, snippet_with_applicability, snippet_with_context};
use clippy_utils::{get_parent_expr, in_macro, path_to_local};
use if_chain::if_chain;
use rustc_ast::util::parser::PREC_POSTFIX;
use rustc_data_structures::fx::FxIndexMap;
use rustc_errors::Applicability;
use rustc_hir::{BindingAnnotation, BorrowKind, Expr, ExprKind, Mutability, Pat, PatKind};
use rustc_hir::{BindingAnnotation, Body, BodyId, BorrowKind, Expr, ExprKind, HirId, Mutability, Pat, PatKind, UnOp};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty;
use rustc_middle::ty::adjustment::{Adjust, Adjustment};
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::Span;

declare_clippy_lint! {
/// **What it does:** Checks for address of operations (`&`) that are going to
Expand All @@ -34,13 +38,65 @@ declare_clippy_lint! {
"taking a reference that is going to be automatically dereferenced"
}

declare_clippy_lint! {
/// **What it does:** Checks for `ref` bindings which create a reference to a reference.
///
/// **Why is this bad?** The address-of operator at the use site is clearer about the need for a reference.
///
/// **Known problems:** None.
///
/// **Example:**
/// ```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]);
#[derive(Default)]
pub struct NeedlessBorrow;
pub struct NeedlessBorrow {
/// 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>>,
}

impl_lint_pass!(NeedlessBorrow => [NEEDLESS_BORROW]);
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)>,
}

impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
if let Some(local) = path_to_local(e) {
self.check_local_usage(cx, e, local);
}

if e.span.from_expansion() {
return;
}
Expand Down Expand Up @@ -81,35 +137,132 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrow {
}
}
}

fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) {
if pat.span.from_expansion() {
return;
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())],
}),
);
}
}
}
if_chain! {
if let PatKind::Binding(BindingAnnotation::Ref, .., name, _) = pat.kind;
if let ty::Ref(_, tam, mutbl) = *cx.typeck_results().pat_ty(pat).kind();
if mutbl == Mutability::Not;
if let ty::Ref(_, _, mutbl) = *tam.kind();
// only lint immutable refs, because borrowed `&mut T` cannot be moved out
if mutbl == Mutability::Not;
then {
}

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;
span_lint_and_then(
cx,
NEEDLESS_BORROW,
pat.span,
if pat.always_deref {
NEEDLESS_BORROW
} else {
REF_BINDING_TO_REFERENCE
},
pat.spans,
"this pattern creates a reference to a reference",
|diag| {
if let Some(snippet) = snippet_opt(cx, name.span) {
diag.span_suggestion(
pat.span,
"change this to",
snippet,
Applicability::MachineApplicable,
);
}
}
diag.multipart_suggestion("try this", replacements, app);
},
)
}
self.current_body = None;
}
}
}
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,
}
}
}
}
}
}
2 changes: 1 addition & 1 deletion clippy_utils/src/higher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub fn range<'a>(expr: &'a hir::Expr<'_>) -> Option<Range<'a>> {
limits: ast::RangeLimits::Closed,
})
},
hir::ExprKind::Struct(ref path, ref fields, None) => match path {
hir::ExprKind::Struct(path, ref fields, None) => match path {
hir::QPath::LangItem(hir::LangItem::RangeFull, _) => Some(Range {
start: None,
end: None,
Expand Down
4 changes: 2 additions & 2 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1244,12 +1244,12 @@ pub fn match_function_call<'tcx>(
path: &[&str],
) -> Option<&'tcx [Expr<'tcx>]> {
if_chain! {
if let ExprKind::Call(ref fun, ref args) = expr.kind;
if let ExprKind::Call(ref fun, args) = expr.kind;
if let ExprKind::Path(ref qpath) = fun.kind;
if let Some(fun_def_id) = cx.qpath_res(qpath, fun.hir_id).opt_def_id();
if match_def_path(cx, fun_def_id, path);
then {
return Some(&args)
return Some(args)
}
};
None
Expand Down
37 changes: 12 additions & 25 deletions clippy_utils/src/visitors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,34 +189,21 @@ impl<'v> Visitor<'v> for LocalUsedVisitor<'v> {
}
}

/// A type which can be visited.
pub trait Visitable<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V);
/// Calls the corresponding `visit_*` function on the visitor.
fn visit<V: Visitor<'tcx>>(self, visitor: &mut V);
}
impl Visitable<'tcx> for &'tcx Expr<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
v.visit_expr(self)
}
}
impl Visitable<'tcx> for &'tcx Block<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
v.visit_block(self)
}
}
impl<'tcx> Visitable<'tcx> for &'tcx Stmt<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
v.visit_stmt(self)
}
}
impl<'tcx> Visitable<'tcx> for &'tcx Body<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
v.visit_body(self)
}
}
impl<'tcx> Visitable<'tcx> for &'tcx Arm<'tcx> {
fn visit<V: Visitor<'tcx>>(self, v: &mut V) {
v.visit_arm(self)
}
macro_rules! visitable_ref {
($t:ident, $f:ident) => {
impl Visitable<'tcx> for &'tcx $t<'tcx> {
fn visit<V: Visitor<'tcx>>(self, visitor: &mut V) {
visitor.$f(self);
}
}
};
}
visitable_ref!(Block, visit_block);

/// Calls the given function for each break expression.
pub fn visit_break_exprs<'tcx>(
Expand Down
17 changes: 0 additions & 17 deletions tests/ui/needless_borrow.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ fn main() {
let vec = Vec::new();
let vec_val = g(&vec); // should not error, because `&Vec<T>` derefs to `&[T]`
h(&"foo"); // should not error, because the `&&str` is required, due to `&Trait`
if let Some(cake) = Some(&5) {}
let garbl = match 42 {
44 => &a,
45 => {
Expand All @@ -43,19 +42,3 @@ trait Trait {}
impl<'a> Trait for &'a str {}

fn h(_: &dyn Trait) {}
#[warn(clippy::needless_borrow)]
#[allow(dead_code)]
fn issue_1432() {
let mut v = Vec::<String>::new();
let _ = v.iter_mut().filter(|&ref a| a.is_empty());
let _ = v.iter().filter(|&a| a.is_empty());

let _ = v.iter().filter(|&a| a.is_empty());
}

#[allow(dead_code)]
#[warn(clippy::needless_borrow)]
#[derive(Debug)]
enum Foo<'a> {
Str(&'a str),
}
Loading

0 comments on commit 7f40dc0

Please sign in to comment.