Only warn about unused mut in user-written code

This commit is contained in:
varkor 2018-10-03 14:14:11 +01:00
parent e7416d5650
commit 113141b6f5
5 changed files with 36 additions and 12 deletions

View File

@ -4058,16 +4058,16 @@ impl<'a> LoweringContext<'a> {
// expand <head>
let head = self.lower_expr(head);
let head_sp = head.span;
let desugared_span = self.allow_internal_unstable(
CompilerDesugaringKind::ForLoop,
head.span,
);
let iter = self.str_to_ident("iter");
let next_ident = self.str_to_ident("__next");
let next_sp = self.allow_internal_unstable(
CompilerDesugaringKind::ForLoop,
head_sp,
);
let next_pat = self.pat_ident_binding_mode(
next_sp,
desugared_span,
next_ident,
hir::BindingAnnotation::Mutable,
);
@ -4096,8 +4096,11 @@ impl<'a> LoweringContext<'a> {
};
// `mut iter`
let iter_pat =
self.pat_ident_binding_mode(head_sp, iter, hir::BindingAnnotation::Mutable);
let iter_pat = self.pat_ident_binding_mode(
desugared_span,
iter,
hir::BindingAnnotation::Mutable
);
// `match ::std::iter::Iterator::next(&mut iter) { ... }`
let match_expr = {
@ -4126,8 +4129,12 @@ impl<'a> LoweringContext<'a> {
let next_expr = P(self.expr_ident(head_sp, next_ident, next_pat.id));
// `let mut __next`
let next_let =
self.stmt_let_pat(head_sp, None, next_pat, hir::LocalSource::ForLoopDesugar);
let next_let = self.stmt_let_pat(
desugared_span,
None,
next_pat,
hir::LocalSource::ForLoopDesugar,
);
// `let <pat> = __next`
let pat = self.lower_pat(pat);

View File

@ -76,10 +76,14 @@ impl<'a, 'tcx> UnusedMutCx<'a, 'tcx> {
}
let (hir_id, span) = ids[0];
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
if span.compiler_desugaring_kind().is_some() {
// If the `mut` arises as part of a desugaring, we should ignore it.
continue;
}
// Ok, every name wasn't used mutably, so issue a warning that this
// didn't need to be mutable.
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
tcx.struct_span_lint_hir(UNUSED_MUT,
hir_id,
span,

View File

@ -316,7 +316,10 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
}
let span = local_decl.source_info.span;
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
if span.compiler_desugaring_kind().is_some() {
// If the `mut` arises as part of a desugaring, we should ignore it.
continue;
}
let mut err = tcx.struct_span_lint_node(
UNUSED_MUT,
@ -324,6 +327,7 @@ fn do_mir_borrowck<'a, 'gcx, 'tcx>(
span,
"variable does not need to be mutable",
);
let mut_span = tcx.sess.source_map().span_until_non_whitespace(span);
err.span_suggestion_short_with_applicability(
mut_span,
"remove this `mut`",

View File

@ -140,7 +140,8 @@ enum CallKind {
fn temp_decl(mutability: Mutability, ty: Ty, span: Span) -> LocalDecl {
let source_info = SourceInfo { scope: OUTERMOST_SOURCE_SCOPE, span };
LocalDecl {
mutability, ty,
mutability,
ty,
user_ty: None,
name: None,
source_info,

View File

@ -0,0 +1,8 @@
// run-pass
#![deny(unused_mut)]
#![allow(unreachable_code)]
fn main() {
for _ in { return (); 0..3 } {} // ok
}