Skip to content

Commit eac0b26

Browse files
committed
Auto merge of rust-lang#88214 - notriddle:notriddle/for-loop-span-drop-temps-mut, r=nagisa
rustc: use more correct span data in for loop desugaring Fixes rust-lang#82462 Before: help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped | LL | for x in DroppingSlice(&*v).iter(); { | + After: help: consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped | LL | }; | + This seems like a reasonable fix: since the desugared "expr_drop_temps_mut" contains the entire desugared loop construct, its span should contain the entire loop construct as well.
2 parents 81ce2fb + 48268f5 commit eac0b26

File tree

4 files changed

+9
-10
lines changed

4 files changed

+9
-10
lines changed

clippy_lints/src/loops/for_kv_map.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ pub(super) fn check<'tcx>(
1515
pat: &'tcx Pat<'_>,
1616
arg: &'tcx Expr<'_>,
1717
body: &'tcx Expr<'_>,
18-
expr: &'tcx Expr<'_>,
1918
) {
2019
let pat_span = pat.span;
2120

@@ -43,7 +42,7 @@ pub(super) fn check<'tcx>(
4342
span_lint_and_then(
4443
cx,
4544
FOR_KV_MAP,
46-
expr.span,
45+
arg_span,
4746
&format!("you seem to want to iterate on a map's {}s", kind),
4847
|diag| {
4948
let map = sugg::Sugg::hir(cx, arg, "map");

clippy_lints/src/loops/iter_next_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ use rustc_hir::Expr;
55
use rustc_lint::LateContext;
66
use rustc_span::sym;
77

8-
pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>, expr: &Expr<'_>) -> bool {
8+
pub(super) fn check(cx: &LateContext<'_>, arg: &Expr<'_>) -> bool {
99
if is_trait_method(cx, arg, sym::Iterator) {
1010
span_lint(
1111
cx,
1212
ITER_NEXT_LOOP,
13-
expr.span,
13+
arg.span,
1414
"you are iterating over `Iterator::next()` which is an Option; this will compile but is \
1515
probably not what you want",
1616
);

clippy_lints/src/loops/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -616,15 +616,15 @@ fn check_for_loop<'tcx>(
616616
needless_range_loop::check(cx, pat, arg, body, expr);
617617
explicit_counter_loop::check(cx, pat, arg, body, expr);
618618
}
619-
check_for_loop_arg(cx, pat, arg, expr);
620-
for_kv_map::check(cx, pat, arg, body, expr);
619+
check_for_loop_arg(cx, pat, arg);
620+
for_kv_map::check(cx, pat, arg, body);
621621
mut_range_bound::check(cx, arg, body);
622622
single_element_loop::check(cx, pat, arg, body, expr);
623623
same_item_push::check(cx, pat, arg, body, expr);
624624
manual_flatten::check(cx, pat, arg, body, span);
625625
}
626626

627-
fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr: &Expr<'_>) {
627+
fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>) {
628628
let mut next_loop_linted = false; // whether or not ITER_NEXT_LOOP lint was used
629629

630630
if let ExprKind::MethodCall(method, _, [self_arg], _) = arg.kind {
@@ -637,7 +637,7 @@ fn check_for_loop_arg(cx: &LateContext<'_>, pat: &Pat<'_>, arg: &Expr<'_>, expr:
637637
explicit_into_iter_loop::check(cx, self_arg, arg);
638638
},
639639
"next" => {
640-
next_loop_linted = iter_next_loop::check(cx, arg, expr);
640+
next_loop_linted = iter_next_loop::check(cx, arg);
641641
},
642642
_ => {},
643643
}

clippy_lints/src/loops/needless_range_loop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub(super) fn check<'tcx>(
144144
span_lint_and_then(
145145
cx,
146146
NEEDLESS_RANGE_LOOP,
147-
expr.span,
147+
arg.span,
148148
&format!("the loop variable `{}` is used to index `{}`", ident.name, indexed),
149149
|diag| {
150150
multispan_sugg(
@@ -170,7 +170,7 @@ pub(super) fn check<'tcx>(
170170
span_lint_and_then(
171171
cx,
172172
NEEDLESS_RANGE_LOOP,
173-
expr.span,
173+
arg.span,
174174
&format!("the loop variable `{}` is only used to index `{}`", ident.name, indexed),
175175
|diag| {
176176
multispan_sugg(

0 commit comments

Comments
 (0)