Skip to content

Commit 113141b

Browse files
committed
Only warn about unused mut in user-written code
1 parent e7416d5 commit 113141b

File tree

5 files changed

+36
-12
lines changed

5 files changed

+36
-12
lines changed

src/librustc/hir/lowering.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -4058,16 +4058,16 @@ impl<'a> LoweringContext<'a> {
40584058
// expand <head>
40594059
let head = self.lower_expr(head);
40604060
let head_sp = head.span;
4061+
let desugared_span = self.allow_internal_unstable(
4062+
CompilerDesugaringKind::ForLoop,
4063+
head.span,
4064+
);
40614065

40624066
let iter = self.str_to_ident("iter");
40634067

40644068
let next_ident = self.str_to_ident("__next");
4065-
let next_sp = self.allow_internal_unstable(
4066-
CompilerDesugaringKind::ForLoop,
4067-
head_sp,
4068-
);
40694069
let next_pat = self.pat_ident_binding_mode(
4070-
next_sp,
4070+
desugared_span,
40714071
next_ident,
40724072
hir::BindingAnnotation::Mutable,
40734073
);
@@ -4096,8 +4096,11 @@ impl<'a> LoweringContext<'a> {
40964096
};
40974097

40984098
// `mut iter`
4099-
let iter_pat =
4100-
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable);
4099+
let iter_pat = self.pat_ident_binding_mode(
4100+
desugared_span,
4101+
iter,
4102+
hir::BindingAnnotation::Mutable
4103+
);
41014104

41024105
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
41034106
let match_expr = {
@@ -4126,8 +4129,12 @@ impl<'a> LoweringContext<'a> {
41264129
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
41274130

41284131
// `let mut __next`
4129-
let next_let =
4130-
self.stmt_let_pat(head_sp, None, next_pat, hir::LocalSource::ForLoopDesugar);
4132+
let next_let = self.stmt_let_pat(
4133+
desugared_span,
4134+
None,
4135+
next_pat,
4136+
hir::LocalSource::ForLoopDesugar,
4137+
);
41314138

41324139
// `let <pat> = __next`
41334140
let pat = self.lower_pat(pat);

src/librustc_borrowck/borrowck/unused.rs

+5-1
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

+5-1
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

+2-1
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,
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)