Skip to content

Commit d49075f

Browse files
committed
Suppress ref mut suggestion for for-loop bindings
1 parent f30eced commit d49075f

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3940,14 +3940,16 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
39403940
if let Some(decl) = local_decl
39413941
&& decl.can_be_made_mutable()
39423942
{
3943-
let message = if matches!(
3944-
decl.local_info(),
3945-
LocalInfo::User(BindingForm::Var(VarBindingForm {
3946-
opt_match_place: Some((_, match_span)),
3947-
..
3948-
})) if matches!(match_span.desugaring_kind(), Some(DesugaringKind::ForLoop))
3949-
) && let Ok(binding_name) =
3950-
self.infcx.tcx.sess.source_map().span_to_snippet(decl.source_info.span)
3943+
let is_for_loop = matches!(
3944+
decl.local_info(),
3945+
LocalInfo::User(BindingForm::Var(VarBindingForm {
3946+
opt_match_place: Some((_, match_span)),
3947+
..
3948+
})) if matches!(match_span.desugaring_kind(), Some(DesugaringKind::ForLoop))
3949+
);
3950+
let message = if is_for_loop
3951+
&& let Ok(binding_name) =
3952+
self.infcx.tcx.sess.source_map().span_to_snippet(decl.source_info.span)
39513953
{
39523954
format!("(mut {}) ", binding_name)
39533955
} else {
@@ -3961,6 +3963,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
39613963
);
39623964

39633965
if !from_arg
3966+
&& !is_for_loop
39643967
&& matches!(
39653968
decl.local_info(),
39663969
LocalInfo::User(BindingForm::Var(VarBindingForm {

tests/ui/borrowck/borrowck-for-loop-deref-pattern-assignment.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ help: consider making this binding mutable
1010
|
1111
LL | for &(mut num) num in nums {
1212
| +++++++++
13-
help: to modify the original value, take a borrow instead
14-
|
15-
LL | for &ref mut num in nums {
16-
| +++++++
1713

1814
error: aborting due to 1 previous error
1915

0 commit comments

Comments
 (0)