Skip to content

Commit 67ea84d

Browse files
committed
Add desugaring mark to while loop
1 parent f03eb6b commit 67ea84d

File tree

7 files changed

+28
-24
lines changed

7 files changed

+28
-24
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
100100
ExprKind::If(ref cond, ref then, ref else_opt) => {
101101
self.lower_expr_if(cond, then, else_opt.as_deref())
102102
}
103-
ExprKind::While(ref cond, ref body, opt_label) => self
104-
.with_loop_scope(e.id, |this| {
105-
this.lower_expr_while_in_loop_scope(e.span, cond, body, opt_label)
106-
}),
103+
ExprKind::While(ref cond, ref body, opt_label) => {
104+
self.with_loop_scope(e.id, |this| {
105+
let span =
106+
this.mark_span_with_reason(DesugaringKind::WhileLoop, e.span, None);
107+
this.lower_expr_while_in_loop_scope(span, cond, body, opt_label)
108+
})
109+
}
107110
ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| {
108111
hir::ExprKind::Loop(
109112
this.lower_block(body, false),

compiler/rustc_middle/src/lint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ pub fn struct_lint_level<'s, 'd>(
389389
pub fn in_external_macro(sess: &Session, span: Span) -> bool {
390390
let expn_data = span.ctxt().outer_expn_data();
391391
match expn_data.kind {
392-
ExpnKind::Inlined | ExpnKind::Root | ExpnKind::Desugaring(DesugaringKind::ForLoop(_)) => {
393-
false
394-
}
392+
ExpnKind::Inlined
393+
| ExpnKind::Root
394+
| ExpnKind::Desugaring(DesugaringKind::ForLoop(_) | DesugaringKind::WhileLoop) => false,
395395
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
396396
ExpnKind::Macro(MacroKind::Bang, _) => {
397397
// Dummy span for the `def_site` means it's an external macro.

compiler/rustc_span/src/hygiene.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,7 @@ pub enum DesugaringKind {
11011101
Await,
11021102
ForLoop(ForLoopLoc),
11031103
LetElse,
1104+
WhileLoop,
11041105
}
11051106

11061107
/// A location in the desugaring of a `for` loop
@@ -1122,6 +1123,7 @@ impl DesugaringKind {
11221123
DesugaringKind::OpaqueTy => "`impl Trait`",
11231124
DesugaringKind::ForLoop(_) => "`for` loop",
11241125
DesugaringKind::LetElse => "`let...else`",
1126+
DesugaringKind::WhileLoop => "`while` loop",
11251127
}
11261128
}
11271129
}

compiler/rustc_typeck/src/check/coercion.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use rustc_middle::ty::subst::SubstsRef;
5454
use rustc_middle::ty::{self, ToPredicate, Ty, TypeAndMut};
5555
use rustc_session::parse::feature_err;
5656
use rustc_span::symbol::sym;
57-
use rustc_span::{self, BytePos, Span};
57+
use rustc_span::{self, BytePos, DesugaringKind, Span};
5858
use rustc_target::spec::abi::Abi;
5959
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
6060
use rustc_trait_selection::traits::{self, ObligationCause, ObligationCauseCode};
@@ -1536,8 +1536,10 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15361536
// If the block is from an external macro or try (`?`) desugaring, then
15371537
// do not suggest adding a semicolon, because there's nowhere to put it.
15381538
// See issues #81943 and #87051.
1539-
if cond_expr.span.desugaring_kind().is_none()
1540-
&& !in_external_macro(fcx.tcx.sess, cond_expr.span)
1539+
if matches!(
1540+
cond_expr.span.desugaring_kind(),
1541+
None | Some(DesugaringKind::WhileLoop)
1542+
) && !in_external_macro(fcx.tcx.sess, cond_expr.span)
15411543
&& !matches!(
15421544
cond_expr.kind,
15431545
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar)

src/tools/clippy/tests/ui/crashes/issues_loop_mut_cond.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![allow(clippy::blocks_in_if_conditions)]
21
#![allow(dead_code)]
32

43
/// Issue: https://github.com/rust-lang/rust-clippy/issues/2596

src/tools/clippy/tests/ui/infinite_loop.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::blocks_in_if_conditions)]
2-
31
fn fn_val(i: i32) -> i32 {
42
unimplemented!()
53
}

src/tools/clippy/tests/ui/infinite_loop.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: variables in the condition are not mutated in the loop body
2-
--> $DIR/infinite_loop.rs:22:11
2+
--> $DIR/infinite_loop.rs:20:11
33
|
44
LL | while y < 10 {
55
| ^^^^^^
@@ -8,71 +8,71 @@ LL | while y < 10 {
88
= note: this may lead to an infinite or to a never running loop
99

1010
error: variables in the condition are not mutated in the loop body
11-
--> $DIR/infinite_loop.rs:27:11
11+
--> $DIR/infinite_loop.rs:25:11
1212
|
1313
LL | while y < 10 && x < 3 {
1414
| ^^^^^^^^^^^^^^^
1515
|
1616
= note: this may lead to an infinite or to a never running loop
1717

1818
error: variables in the condition are not mutated in the loop body
19-
--> $DIR/infinite_loop.rs:34:11
19+
--> $DIR/infinite_loop.rs:32:11
2020
|
2121
LL | while !cond {
2222
| ^^^^^
2323
|
2424
= note: this may lead to an infinite or to a never running loop
2525

2626
error: variables in the condition are not mutated in the loop body
27-
--> $DIR/infinite_loop.rs:78:11
27+
--> $DIR/infinite_loop.rs:76:11
2828
|
2929
LL | while i < 3 {
3030
| ^^^^^
3131
|
3232
= note: this may lead to an infinite or to a never running loop
3333

3434
error: variables in the condition are not mutated in the loop body
35-
--> $DIR/infinite_loop.rs:83:11
35+
--> $DIR/infinite_loop.rs:81:11
3636
|
3737
LL | while i < 3 && j > 0 {
3838
| ^^^^^^^^^^^^^^
3939
|
4040
= note: this may lead to an infinite or to a never running loop
4141

4242
error: variables in the condition are not mutated in the loop body
43-
--> $DIR/infinite_loop.rs:87:11
43+
--> $DIR/infinite_loop.rs:85:11
4444
|
4545
LL | while i < 3 {
4646
| ^^^^^
4747
|
4848
= note: this may lead to an infinite or to a never running loop
4949

5050
error: variables in the condition are not mutated in the loop body
51-
--> $DIR/infinite_loop.rs:102:11
51+
--> $DIR/infinite_loop.rs:100:11
5252
|
5353
LL | while i < 3 {
5454
| ^^^^^
5555
|
5656
= note: this may lead to an infinite or to a never running loop
5757

5858
error: variables in the condition are not mutated in the loop body
59-
--> $DIR/infinite_loop.rs:107:11
59+
--> $DIR/infinite_loop.rs:105:11
6060
|
6161
LL | while i < 3 {
6262
| ^^^^^
6363
|
6464
= note: this may lead to an infinite or to a never running loop
6565

6666
error: variables in the condition are not mutated in the loop body
67-
--> $DIR/infinite_loop.rs:173:15
67+
--> $DIR/infinite_loop.rs:171:15
6868
|
6969
LL | while self.count < n {
7070
| ^^^^^^^^^^^^^^
7171
|
7272
= note: this may lead to an infinite or to a never running loop
7373

7474
error: variables in the condition are not mutated in the loop body
75-
--> $DIR/infinite_loop.rs:181:11
75+
--> $DIR/infinite_loop.rs:179:11
7676
|
7777
LL | while y < 10 {
7878
| ^^^^^^
@@ -82,7 +82,7 @@ LL | while y < 10 {
8282
= help: rewrite it as `if cond { loop { } }`
8383

8484
error: variables in the condition are not mutated in the loop body
85-
--> $DIR/infinite_loop.rs:188:11
85+
--> $DIR/infinite_loop.rs:186:11
8686
|
8787
LL | while y < 10 {
8888
| ^^^^^^

0 commit comments

Comments
 (0)