Skip to content

Commit 062b66d

Browse files
committed
Auto merge of rust-lang#12892 - meithecatte:needless-borrows-mutrefs, r=xFrednet
needless_borrows_for_generic_args: Fix for &mut This commit fixes a bug introduced in rust-lang#12706, where the behavior of the lint has been changed, to avoid suggestions that introduce a move. The motivation in the commit message is quite poor (if the detection for significant drops is not sufficient because it's not transitive, the proper fix would be to make it transitive). However, rust-lang#12454, the linked issue, provides a good reason for the change — if the value being borrowed is bound to a variable, then moving it will only introduce friction into future refactorings. Thus rust-lang#12706 changes the logic so that the lint triggers if the value being borrowed is Copy, or is the result of a function call, simplifying the logic to the point where analysing "is this the only use of this value" isn't necessary. However, said PR also introduces an undocumented carveout, where referents that themselves are mutable references are treated as Copy, to catch some cases that we do want to lint against. However, that is not sound — it's possible to consume a mutable reference by moving it. To avoid emitting false suggestions, this PR reintroduces the referent_used_exactly_once logic and runs that check for referents that are themselves mutable references. Thinking about the code shape of &mut x, where x: &mut T, raises the point that while removing the &mut outright won't work, the extra indirection is still undesirable, and perhaps instead we should suggest reborrowing: &mut *x. That, however, is left as possible future work. Fixes rust-lang#12856 changelog: none
2 parents c7cfe7f + e63061d commit 062b66d

3 files changed

+59
-5
lines changed

clippy_lints/src/needless_borrows_for_generic_args.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_and_then;
4-
use clippy_utils::mir::PossibleBorrowerMap;
4+
use clippy_utils::mir::{enclosing_mir, expr_local, local_assignments, used_exactly_once, PossibleBorrowerMap};
55
use clippy_utils::source::snippet_with_context;
66
use clippy_utils::ty::{implements_trait, is_copy};
77
use clippy_utils::{expr_use_ctxt, peel_n_hir_expr_refs, DefinedTy, ExprUseNode};
@@ -12,6 +12,7 @@ use rustc_hir::{Body, Expr, ExprKind, Mutability, Path, QPath};
1212
use rustc_index::bit_set::BitSet;
1313
use rustc_infer::infer::TyCtxtInferExt;
1414
use rustc_lint::{LateContext, LateLintPass};
15+
use rustc_middle::mir::{Rvalue, StatementKind};
1516
use rustc_middle::ty::{
1617
self, ClauseKind, EarlyBinder, FnSig, GenericArg, GenericArgKind, ParamTy, ProjectionPredicate, Ty,
1718
};
@@ -107,6 +108,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBorrowsForGenericArgs<'tcx> {
107108
}
108109
&& let count = needless_borrow_count(
109110
cx,
111+
&mut self.possible_borrowers,
110112
fn_id,
111113
cx.typeck_results().node_args(hir_id),
112114
i,
@@ -155,9 +157,14 @@ fn path_has_args(p: &QPath<'_>) -> bool {
155157
/// The following constraints will be checked:
156158
/// * The borrowed expression meets all the generic type's constraints.
157159
/// * The generic type appears only once in the functions signature.
158-
/// * The borrowed value is Copy itself OR not a variable (created by a function call)
160+
/// * The borrowed value is:
161+
/// - `Copy` itself, or
162+
/// - the only use of a mutable reference, or
163+
/// - not a variable (created by a function call)
164+
#[expect(clippy::too_many_arguments)]
159165
fn needless_borrow_count<'tcx>(
160166
cx: &LateContext<'tcx>,
167+
possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
161168
fn_id: DefId,
162169
callee_args: ty::GenericArgsRef<'tcx>,
163170
arg_index: usize,
@@ -232,9 +239,9 @@ fn needless_borrow_count<'tcx>(
232239

233240
let referent_ty = cx.typeck_results().expr_ty(referent);
234241

235-
if (!is_copy(cx, referent_ty) && !referent_ty.is_ref())
236-
&& let ExprKind::AddrOf(_, _, inner) = reference.kind
237-
&& !matches!(inner.kind, ExprKind::Call(..) | ExprKind::MethodCall(..))
242+
if !(is_copy(cx, referent_ty)
243+
|| referent_ty.is_ref() && referent_used_exactly_once(cx, possible_borrowers, reference)
244+
|| matches!(referent.kind, ExprKind::Call(..) | ExprKind::MethodCall(..)))
238245
{
239246
return false;
240247
}
@@ -337,6 +344,37 @@ fn is_mixed_projection_predicate<'tcx>(
337344
}
338345
}
339346

347+
fn referent_used_exactly_once<'tcx>(
348+
cx: &LateContext<'tcx>,
349+
possible_borrowers: &mut Vec<(LocalDefId, PossibleBorrowerMap<'tcx, 'tcx>)>,
350+
reference: &Expr<'tcx>,
351+
) -> bool {
352+
if let Some(mir) = enclosing_mir(cx.tcx, reference.hir_id)
353+
&& let Some(local) = expr_local(cx.tcx, reference)
354+
&& let [location] = *local_assignments(mir, local).as_slice()
355+
&& let block_data = &mir.basic_blocks[location.block]
356+
&& let Some(statement) = block_data.statements.get(location.statement_index)
357+
&& let StatementKind::Assign(box (_, Rvalue::Ref(_, _, place))) = statement.kind
358+
&& !place.is_indirect_first_projection()
359+
{
360+
let body_owner_local_def_id = cx.tcx.hir().enclosing_body_owner(reference.hir_id);
361+
if possible_borrowers
362+
.last()
363+
.map_or(true, |&(local_def_id, _)| local_def_id != body_owner_local_def_id)
364+
{
365+
possible_borrowers.push((body_owner_local_def_id, PossibleBorrowerMap::new(cx, mir)));
366+
}
367+
let possible_borrower = &mut possible_borrowers.last_mut().unwrap().1;
368+
// If `only_borrowers` were used here, the `copyable_iterator::warn` test would fail. The reason is
369+
// that `PossibleBorrowerVisitor::visit_terminator` considers `place.local` a possible borrower of
370+
// itself. See the comment in that method for an explanation as to why.
371+
possible_borrower.bounded_borrowers(&[local], &[local, place.local], place.local, location)
372+
&& used_exactly_once(mir, place.local).unwrap_or(false)
373+
} else {
374+
false
375+
}
376+
}
377+
340378
// Iteratively replaces `param_ty` with `new_ty` in `args`, and similarly for each resulting
341379
// projected type that is a type parameter. Returns `false` if replacing the types would have an
342380
// effect on the function signature beyond substituting `new_ty` for `param_ty`.

tests/ui/needless_borrows_for_generic_args.fixed

+8
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,12 @@ fn main() {
333333
f(&y); // Don't lint
334334
f("".to_owned()); // Lint
335335
}
336+
{
337+
fn takes_writer<T: std::io::Write>(_: T) {}
338+
339+
fn issue_12856(mut buffer: &mut Vec<u8>) {
340+
takes_writer(&mut buffer); // Don't lint, would make buffer unavailable later
341+
buffer.extend(b"\n");
342+
}
343+
}
336344
}

tests/ui/needless_borrows_for_generic_args.rs

+8
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,12 @@ fn main() {
333333
f(&y); // Don't lint
334334
f(&"".to_owned()); // Lint
335335
}
336+
{
337+
fn takes_writer<T: std::io::Write>(_: T) {}
338+
339+
fn issue_12856(mut buffer: &mut Vec<u8>) {
340+
takes_writer(&mut buffer); // Don't lint, would make buffer unavailable later
341+
buffer.extend(b"\n");
342+
}
343+
}
336344
}

0 commit comments

Comments
 (0)