Skip to content

Commit 3810657

Browse files
authored
Rollup merge of rust-lang#54787 - varkor:unused-mut-in-desugaring, r=nikomatsakis
Only warn about unused `mut` in user-written code Fixes rust-lang#54586. r? @pnkfelix cc @blitzerr
2 parents 9e8f522 + ea3d8f5 commit 3810657

File tree

6 files changed

+53
-17
lines changed

6 files changed

+53
-17
lines changed

src/librustc/hir/lowering.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,16 +4132,16 @@ impl<'a> LoweringContext<'a> {
41324132
// expand <head>
41334133
let head = self.lower_expr(head);
41344134
let head_sp = head.span;
4135+
let desugared_span = self.allow_internal_unstable(
4136+
CompilerDesugaringKind::ForLoop,
4137+
head_sp,
4138+
);
41354139

41364140
let iter = self.str_to_ident("iter");
41374141

41384142
let next_ident = self.str_to_ident("__next");
4139-
let next_sp = self.allow_internal_unstable(
4140-
CompilerDesugaringKind::ForLoop,
4141-
head_sp,
4142-
);
41434143
let next_pat = self.pat_ident_binding_mode(
4144-
next_sp,
4144+
desugared_span,
41454145
next_ident,
41464146
hir::BindingAnnotation::Mutable,
41474147
);
@@ -4170,8 +4170,11 @@ impl<'a> LoweringContext<'a> {
41704170
};
41714171

41724172
// `mut iter`
4173-
let iter_pat =
4174-
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable);
4173+
let iter_pat = self.pat_ident_binding_mode(
4174+
desugared_span,
4175+
iter,
4176+
hir::BindingAnnotation::Mutable
4177+
);
41754178

41764179
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
41774180
let match_expr = {
@@ -4200,8 +4203,12 @@ impl<'a> LoweringContext<'a> {
42004203
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
42014204

42024205
// `let mut __next`
4203-
let next_let =
4204-
self.stmt_let_pat(head_sp, None, next_pat, hir::LocalSource::ForLoopDesugar);
4206+
let next_let = self.stmt_let_pat(
4207+
desugared_span,
4208+
None,
4209+
next_pat,
4210+
hir::LocalSource::ForLoopDesugar,
4211+
);
42054212

42064213
// `let <pat> = __next`
42074214
let pat = self.lower_pat(pat);

src/librustc/traits/error_reporting.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use ty::subst::Subst;
4646
use ty::SubtypePredicate;
4747
use util::nodemap::{FxHashMap, FxHashSet};
4848

49-
use syntax_pos::{DUMMY_SP, Span};
49+
use syntax_pos::{DUMMY_SP, Span, ExpnInfo, ExpnFormat};
5050

5151
impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
5252
pub fn report_fulfillment_errors(&self,
@@ -68,18 +68,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
6868
}).collect();
6969

7070
for (index, error) in errors.iter().enumerate() {
71-
error_map.entry(error.obligation.cause.span).or_default().push(
71+
// We want to ignore desugarings here: spans are equivalent even
72+
// if one is the result of a desugaring and the other is not.
73+
let mut span = error.obligation.cause.span;
74+
if let Some(ExpnInfo {
75+
format: ExpnFormat::CompilerDesugaring(_),
76+
def_site: Some(def_span),
77+
..
78+
}) = span.ctxt().outer().expn_info() {
79+
span = def_span;
80+
}
81+
82+
error_map.entry(span).or_default().push(
7283
ErrorDescriptor {
7384
predicate: error.obligation.predicate.clone(),
7485
index: Some(index)
75-
});
86+
}
87+
);
7688

7789
self.reported_trait_errors.borrow_mut()
78-
.entry(error.obligation.cause.span).or_default()
90+
.entry(span).or_default()
7991
.push(error.obligation.predicate.clone());
8092
}
8193

82-
// We do this in 2 passes because we want to display errors in order, tho
94+
// We do this in 2 passes because we want to display errors in order, though
8395
// maybe it *is* better to sort errors by span or something.
8496
let mut is_suppressed = vec![false; errors.len()];
8597
for (_, error_set) in error_map.iter() {

src/librustc_borrowck/borrowck/unused.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
7676
}
7777

7878
let (hir_id, span) = ids[0];
79-
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
79+
if span.compiler_desugaring_kind().is_some() {
80+
// If the `mut` arises as part of a desugaring, we should ignore it.
81+
continue;
82+
}
8083

8184
// Ok, every name wasn't used mutably, so issue a warning that this
8285
// didn't need to be mutable.
86+
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
8387
tcx.struct_span_lint_hir(UNUSED_MUT,
8488
hir_id,
8589
span,

src/librustc_mir/borrow_check/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,18 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
316316
}
317317

318318
let span = local_decl.source_info.span;
319-
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
319+
if span.compiler_desugaring_kind().is_some() {
320+
// If the `mut` arises as part of a desugaring, we should ignore it.
321+
continue;
322+
}
320323

321324
let mut err = tcx.struct_span_lint_node(
322325
UNUSED_MUT,
323326
vsi[local_decl.source_info.scope].lint_root,
324327
span,
325328
"variable does not need to be mutable",
326329
);
330+
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
327331
err.span_suggestion_short_with_applicability(
328332
mut_span,
329333
"remove this `mut`",

src/librustc_mir/shim.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ enum CallKind {
140140
fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
141141
let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
142142
LocalDecl {
143-
mutability, ty,
143+
mutability,
144+
ty,
144145
user_ty: None,
145146
name: None,
146147
source_info,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-pass
2+
3+
#![deny(unused_mut)]
4+
#![allow(unreachable_code)]
5+
6+
fn main() {
7+
for _ in { return (); 0..3 } {} // ok
8+
}

0 commit comments

Comments
 (0)