Skip to content

Commit 9273eab

Browse files
committed
Auto merge of rust-lang#5490 - sinkuu:toplevel_ref_arg_for, r=phansch
Don't trigger toplevel_ref_arg for `for` loops The lint suggests turning `for ref x in 0..10 {` into `for ref x in let x = &0..10; {`. --- changelog: none
2 parents f6b07db + 554f47b commit 9273eab

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

clippy_lints/src/misc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_span::source_map::{ExpnKind, Span};
1414
use crate::consts::{constant, Constant};
1515
use crate::utils::sugg::Sugg;
1616
use crate::utils::{
17-
get_item_name, get_parent_expr, implements_trait, in_constant, is_integer_const, iter_input_pats,
17+
get_item_name, get_parent_expr, higher, implements_trait, in_constant, is_integer_const, iter_input_pats,
1818
last_path_segment, match_qpath, match_trait_method, paths, snippet, snippet_opt, span_lint, span_lint_and_sugg,
1919
span_lint_and_then, span_lint_hir_and_then, walk_ptrs_ty, SpanlessEq,
2020
};
@@ -267,6 +267,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints {
267267
if let StmtKind::Local(ref local) = stmt.kind;
268268
if let PatKind::Binding(an, .., name, None) = local.pat.kind;
269269
if let Some(ref init) = local.init;
270+
if !higher::is_from_for_desugar(local);
270271
then {
271272
if an == BindingAnnotation::Ref || an == BindingAnnotation::RefMut {
272273
let sugg_init = if init.span.from_expansion() {

tests/ui/toplevel_ref_arg.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ fn main() {
2323
// Make sure that allowing the lint works
2424
#[allow(clippy::toplevel_ref_arg)]
2525
let ref mut _x = 1_234_543;
26+
27+
// ok
28+
for ref _x in 0..10 {}
2629
}

tests/ui/toplevel_ref_arg.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,7 @@ fn main() {
2323
// Make sure that allowing the lint works
2424
#[allow(clippy::toplevel_ref_arg)]
2525
let ref mut _x = 1_234_543;
26+
27+
// ok
28+
for ref _x in 0..10 {}
2629
}

0 commit comments

Comments
 (0)