@@ -13,14 +13,19 @@ use rustc_span::{sym, ExpnKind, Span, Symbol};
13
13
/// The essential nodes of a desugared for loop as well as the entire span:
14
14
/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`.
15
15
pub struct ForLoop < ' tcx > {
16
+ /// `for` loop item
16
17
pub pat : & ' tcx hir:: Pat < ' tcx > ,
18
+ /// `IntoIterator` argument
17
19
pub arg : & ' tcx hir:: Expr < ' tcx > ,
20
+ /// `for` loop body
18
21
pub body : & ' tcx hir:: Expr < ' tcx > ,
22
+ /// entire `for` loop span
19
23
pub span : Span ,
20
24
}
21
25
22
26
impl < ' tcx > ForLoop < ' tcx > {
23
27
#[ inline]
28
+ /// Parses a desugared `for` loop
24
29
pub fn hir ( expr : & Expr < ' tcx > ) -> Option < Self > {
25
30
if_chain ! {
26
31
if let hir:: ExprKind :: Match ( iterexpr, arms, hir:: MatchSource :: ForLoopDesugar ) = expr. kind;
@@ -46,14 +51,19 @@ impl<'tcx> ForLoop<'tcx> {
46
51
}
47
52
}
48
53
54
+ /// An `if` expression without `DropTemps`
49
55
pub struct If < ' hir > {
56
+ /// `if` condition
50
57
pub cond : & ' hir Expr < ' hir > ,
51
- pub r#else : Option < & ' hir Expr < ' hir > > ,
58
+ /// `if` then expression
52
59
pub then : & ' hir Expr < ' hir > ,
60
+ /// `else` expression
61
+ pub r#else : Option < & ' hir Expr < ' hir > > ,
53
62
}
54
63
55
64
impl < ' hir > If < ' hir > {
56
65
#[ inline]
66
+ /// Parses an `if` expression
57
67
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
58
68
if let ExprKind :: If (
59
69
Expr {
@@ -64,21 +74,27 @@ impl<'hir> If<'hir> {
64
74
r#else,
65
75
) = expr. kind
66
76
{
67
- Some ( Self { cond, r#else, then } )
77
+ Some ( Self { cond, then , r#else } )
68
78
} else {
69
79
None
70
80
}
71
81
}
72
82
}
73
83
84
+ /// An `if let` expression
74
85
pub struct IfLet < ' hir > {
86
+ /// `if let` pattern
75
87
pub let_pat : & ' hir Pat < ' hir > ,
88
+ /// `if let` scrutinee
76
89
pub let_expr : & ' hir Expr < ' hir > ,
90
+ /// `if let` then expression
77
91
pub if_then : & ' hir Expr < ' hir > ,
92
+ /// `if let` else expression
78
93
pub if_else : Option < & ' hir Expr < ' hir > > ,
79
94
}
80
95
81
96
impl < ' hir > IfLet < ' hir > {
97
+ /// Parses an `if let` expression
82
98
pub fn hir ( cx : & LateContext < ' _ > , expr : & Expr < ' hir > ) -> Option < Self > {
83
99
if let ExprKind :: If (
84
100
Expr {
@@ -115,7 +131,9 @@ impl<'hir> IfLet<'hir> {
115
131
}
116
132
}
117
133
134
+ /// An `if let` or `match` expression. Useful for lints that trigger on one or the other.
118
135
pub enum IfLetOrMatch < ' hir > {
136
+ /// Any `match` expression
119
137
Match ( & ' hir Expr < ' hir > , & ' hir [ Arm < ' hir > ] , MatchSource ) ,
120
138
/// scrutinee, pattern, then block, else block
121
139
IfLet (
@@ -127,6 +145,7 @@ pub enum IfLetOrMatch<'hir> {
127
145
}
128
146
129
147
impl < ' hir > IfLetOrMatch < ' hir > {
148
+ /// Parses an `if let` or `match` expression
130
149
pub fn parse ( cx : & LateContext < ' _ > , expr : & Expr < ' hir > ) -> Option < Self > {
131
150
match expr. kind {
132
151
ExprKind :: Match ( expr, arms, source) => Some ( Self :: Match ( expr, arms, source) ) ,
@@ -142,14 +161,19 @@ impl<'hir> IfLetOrMatch<'hir> {
142
161
}
143
162
}
144
163
164
+ /// An `if` or `if let` expression
145
165
pub struct IfOrIfLet < ' hir > {
166
+ /// `if` condition that is maybe a `let` expression
146
167
pub cond : & ' hir Expr < ' hir > ,
147
- pub r#else : Option < & ' hir Expr < ' hir > > ,
168
+ /// `if` then expression
148
169
pub then : & ' hir Expr < ' hir > ,
170
+ /// `else` expression
171
+ pub r#else : Option < & ' hir Expr < ' hir > > ,
149
172
}
150
173
151
174
impl < ' hir > IfOrIfLet < ' hir > {
152
175
#[ inline]
176
+ /// Parses an `if` or `if let` expression
153
177
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
154
178
if let ExprKind :: If ( cond, then, r#else) = expr. kind {
155
179
if let ExprKind :: DropTemps ( new_cond) = cond. kind {
@@ -160,7 +184,7 @@ impl<'hir> IfOrIfLet<'hir> {
160
184
} ) ;
161
185
}
162
186
if let ExprKind :: Let ( ..) = cond. kind {
163
- return Some ( Self { cond, r#else, then } ) ;
187
+ return Some ( Self { cond, then , r#else } ) ;
164
188
}
165
189
}
166
190
None
@@ -281,14 +305,17 @@ impl<'a> VecArgs<'a> {
281
305
}
282
306
}
283
307
308
+ /// A desugared `while` loop
284
309
pub struct While < ' hir > {
285
- pub if_cond : & ' hir Expr < ' hir > ,
286
- pub if_then : & ' hir Expr < ' hir > ,
287
- pub if_else : Option < & ' hir Expr < ' hir > > ,
310
+ /// `while` loop condition
311
+ pub condition : & ' hir Expr < ' hir > ,
312
+ /// `while` loop body
313
+ pub body : & ' hir Expr < ' hir > ,
288
314
}
289
315
290
316
impl < ' hir > While < ' hir > {
291
317
#[ inline]
318
+ /// Parses a desugared `while` loop
292
319
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
293
320
if let ExprKind :: Loop (
294
321
Block {
@@ -297,11 +324,11 @@ impl<'hir> While<'hir> {
297
324
kind :
298
325
ExprKind :: If (
299
326
Expr {
300
- kind : ExprKind :: DropTemps ( if_cond ) ,
327
+ kind : ExprKind :: DropTemps ( condition ) ,
301
328
..
302
329
} ,
303
- if_then ,
304
- if_else_ref ,
330
+ body ,
331
+ _ ,
305
332
) ,
306
333
..
307
334
} ) ,
@@ -312,59 +339,53 @@ impl<'hir> While<'hir> {
312
339
_,
313
340
) = expr. kind
314
341
{
315
- let if_else = * if_else_ref;
316
- return Some ( Self {
317
- if_cond,
318
- if_then,
319
- if_else,
320
- } ) ;
342
+ return Some ( Self { condition, body } ) ;
321
343
}
322
344
None
323
345
}
324
346
}
325
347
348
+ /// A desugared `while let` loop
326
349
pub struct WhileLet < ' hir > {
327
- pub if_expr : & ' hir Expr < ' hir > ,
350
+ /// `while let` loop item pattern
328
351
pub let_pat : & ' hir Pat < ' hir > ,
352
+ /// `while let` loop scrutinee
329
353
pub let_expr : & ' hir Expr < ' hir > ,
354
+ /// `while let` loop body
330
355
pub if_then : & ' hir Expr < ' hir > ,
331
- pub if_else : Option < & ' hir Expr < ' hir > > ,
332
356
}
333
357
334
358
impl < ' hir > WhileLet < ' hir > {
335
359
#[ inline]
360
+ /// Parses a desugared `while let` loop
336
361
pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
337
362
if let ExprKind :: Loop (
338
363
Block {
339
- expr : Some ( if_expr) , ..
364
+ expr :
365
+ Some ( Expr {
366
+ kind :
367
+ ExprKind :: If (
368
+ Expr {
369
+ kind : ExprKind :: Let ( let_pat, let_expr, _) ,
370
+ ..
371
+ } ,
372
+ if_then,
373
+ _,
374
+ ) ,
375
+ ..
376
+ } ) ,
377
+ ..
340
378
} ,
341
379
_,
342
380
LoopSource :: While ,
343
381
_,
344
382
) = expr. kind
345
383
{
346
- if let Expr {
347
- kind :
348
- ExprKind :: If (
349
- Expr {
350
- kind : ExprKind :: Let ( let_pat, let_expr, _) ,
351
- ..
352
- } ,
353
- if_then,
354
- if_else_ref,
355
- ) ,
356
- ..
357
- } = if_expr
358
- {
359
- let if_else = * if_else_ref;
360
- return Some ( Self {
361
- if_expr,
362
- let_pat,
363
- let_expr,
364
- if_then,
365
- if_else,
366
- } ) ;
367
- }
384
+ return Some ( Self {
385
+ let_pat,
386
+ let_expr,
387
+ if_then,
388
+ } ) ;
368
389
}
369
390
None
370
391
}
0 commit comments