Skip to content

Commit 88c1796

Browse files
committed
Use span before macro expansion in lint for-loops-over-falibles
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
1 parent c0f0b51 commit 88c1796

File tree

3 files changed

+15
-21
lines changed

3 files changed

+15
-21
lines changed

compiler/rustc_lint/src/for_loops_over_fallibles.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
4949
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
5050
let Some((pat, arg)) = extract_for_loop(expr) else { return };
5151

52+
let arg_span = arg.span.source_callsite();
53+
5254
let ty = cx.typeck_results().expr_ty(arg);
5355

5456
let (adt, args, ref_mutability) = match ty.kind() {
@@ -78,27 +80,27 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopsOverFallibles {
7880
&& let Ok(recv_snip) = cx.sess().source_map().span_to_snippet(recv.span)
7981
{
8082
ForLoopsOverFalliblesLoopSub::RemoveNext {
81-
suggestion: recv.span.between(arg.span.shrink_to_hi()),
83+
suggestion: recv.span.between(arg_span.shrink_to_hi()),
8284
recv_snip,
8385
}
8486
} else {
8587
ForLoopsOverFalliblesLoopSub::UseWhileLet {
8688
start_span: expr.span.with_hi(pat.span.lo()),
87-
end_span: pat.span.between(arg.span),
89+
end_span: pat.span.between(arg_span),
8890
var,
8991
}
9092
};
9193
let question_mark = suggest_question_mark(cx, adt, args, expr.span)
92-
.then(|| ForLoopsOverFalliblesQuestionMark { suggestion: arg.span.shrink_to_hi() });
94+
.then(|| ForLoopsOverFalliblesQuestionMark { suggestion: arg_span.shrink_to_hi() });
9395
let suggestion = ForLoopsOverFalliblesSuggestion {
9496
var,
9597
start_span: expr.span.with_hi(pat.span.lo()),
96-
end_span: pat.span.between(arg.span),
98+
end_span: pat.span.between(arg_span),
9799
};
98100

99101
cx.emit_span_lint(
100102
FOR_LOOPS_OVER_FALLIBLES,
101-
arg.span,
103+
arg_span,
102104
ForLoopsOverFalliblesDiag { article, ref_prefix, ty, sub, question_mark, suggestion },
103105
);
104106
}

tests/ui/lint/for-loops-over-falibles/macro-issue-140747.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
fn main() {
44
macro_rules! x {
55
() => {
6-
None::<i32> //~ ERROR for loop over an `Option`. This is more readably written as an `if let` statement [for_loops_over_fallibles]
6+
None::<i32>
77
};
88
}
9-
for _ in x! {} {}
9+
for _ in x! {} {} //~ ERROR for loop over an `Option`. This is more readably written as an `if let` statement [for_loops_over_fallibles]
1010
}

tests/ui/lint/for-loops-over-falibles/macro-issue-140747.stderr

+6-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
error: for loop over an `Option`. This is more readably written as an `if let` statement
2-
--> $DIR/macro-issue-140747.rs:6:13
2+
--> $DIR/macro-issue-140747.rs:9:14
33
|
4-
LL | None::<i32>
5-
| ^^^^^^^^^^^
6-
...
74
LL | for _ in x! {} {}
8-
| ----- in this macro invocation
5+
| ^^^^^
96
|
107
note: the lint level is defined here
118
--> $DIR/macro-issue-140747.rs:1:11
129
|
1310
LL | #![forbid(for_loops_over_fallibles)]
1411
| ^^^^^^^^^^^^^^^^^^^^^^^^
15-
= note: this error originates in the macro `x` (in Nightly builds, run with -Z macro-backtrace for more info)
1612
help: to check pattern in a loop use `while let`
1713
|
18-
LL ~ ) =
19-
LL | };
20-
LL | }
21-
LL ~ while let Some(_ in x! {} {}
14+
LL - for _ in x! {} {}
15+
LL + while let Some(_) = x! {} {}
2216
|
2317
help: consider using `if let` to clear intent
2418
|
25-
LL ~ ) =
26-
LL | };
27-
LL | }
28-
LL ~ if let Some(_ in x! {} {}
19+
LL - for _ in x! {} {}
20+
LL + if let Some(_) = x! {} {}
2921
|
3022

3123
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)