@@ -13,14 +13,19 @@ use rustc_span::{sym, ExpnKind, Span, Symbol};
1313/// The essential nodes of a desugared for loop as well as the entire span:
1414/// `for pat in arg { body }` becomes `(pat, arg, body)`. Return `(pat, arg, body, span)`.
1515pub struct ForLoop < ' tcx > {
16+ /// `for` loop item
1617 pub pat : & ' tcx hir:: Pat < ' tcx > ,
18+ /// `IntoIterator` argument
1719 pub arg : & ' tcx hir:: Expr < ' tcx > ,
20+ /// `for` loop body
1821 pub body : & ' tcx hir:: Expr < ' tcx > ,
22+ /// entire `for` loop span
1923 pub span : Span ,
2024}
2125
2226impl < ' tcx > ForLoop < ' tcx > {
2327 #[ inline]
28+ /// Parses a desugared `for` loop
2429 pub fn hir ( expr : & Expr < ' tcx > ) -> Option < Self > {
2530 if_chain ! {
2631 if let hir:: ExprKind :: Match ( iterexpr, arms, hir:: MatchSource :: ForLoopDesugar ) = expr. kind;
@@ -46,14 +51,19 @@ impl<'tcx> ForLoop<'tcx> {
4651 }
4752}
4853
54+ /// An `if` expression without `DropTemps`
4955pub struct If < ' hir > {
56+ /// `if` condition
5057 pub cond : & ' hir Expr < ' hir > ,
51- pub r#else : Option < & ' hir Expr < ' hir > > ,
58+ /// `if` then expression
5259 pub then : & ' hir Expr < ' hir > ,
60+ /// `else` expression
61+ pub r#else : Option < & ' hir Expr < ' hir > > ,
5362}
5463
5564impl < ' hir > If < ' hir > {
5665 #[ inline]
66+ /// Parses an `if` expression
5767 pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
5868 if let ExprKind :: If (
5969 Expr {
@@ -64,21 +74,27 @@ impl<'hir> If<'hir> {
6474 r#else,
6575 ) = expr. kind
6676 {
67- Some ( Self { cond, r#else, then } )
77+ Some ( Self { cond, then , r#else } )
6878 } else {
6979 None
7080 }
7181 }
7282}
7383
84+ /// An `if let` expression
7485pub struct IfLet < ' hir > {
86+ /// `if let` pattern
7587 pub let_pat : & ' hir Pat < ' hir > ,
88+ /// `if let` scrutinee
7689 pub let_expr : & ' hir Expr < ' hir > ,
90+ /// `if let` then expression
7791 pub if_then : & ' hir Expr < ' hir > ,
92+ /// `if let` else expression
7893 pub if_else : Option < & ' hir Expr < ' hir > > ,
7994}
8095
8196impl < ' hir > IfLet < ' hir > {
97+ /// Parses an `if let` expression
8298 pub fn hir ( cx : & LateContext < ' _ > , expr : & Expr < ' hir > ) -> Option < Self > {
8399 if let ExprKind :: If (
84100 Expr {
@@ -115,7 +131,9 @@ impl<'hir> IfLet<'hir> {
115131 }
116132}
117133
134+ /// An `if let` or `match` expression. Useful for lints that trigger on one or the other.
118135pub enum IfLetOrMatch < ' hir > {
136+ /// Any `match` expression
119137 Match ( & ' hir Expr < ' hir > , & ' hir [ Arm < ' hir > ] , MatchSource ) ,
120138 /// scrutinee, pattern, then block, else block
121139 IfLet (
@@ -127,6 +145,7 @@ pub enum IfLetOrMatch<'hir> {
127145}
128146
129147impl < ' hir > IfLetOrMatch < ' hir > {
148+ /// Parses an `if let` or `match` expression
130149 pub fn parse ( cx : & LateContext < ' _ > , expr : & Expr < ' hir > ) -> Option < Self > {
131150 match expr. kind {
132151 ExprKind :: Match ( expr, arms, source) => Some ( Self :: Match ( expr, arms, source) ) ,
@@ -142,14 +161,19 @@ impl<'hir> IfLetOrMatch<'hir> {
142161 }
143162}
144163
164+ /// An `if` or `if let` expression
145165pub struct IfOrIfLet < ' hir > {
166+ /// `if` condition that is maybe a `let` expression
146167 pub cond : & ' hir Expr < ' hir > ,
147- pub r#else : Option < & ' hir Expr < ' hir > > ,
168+ /// `if` then expression
148169 pub then : & ' hir Expr < ' hir > ,
170+ /// `else` expression
171+ pub r#else : Option < & ' hir Expr < ' hir > > ,
149172}
150173
151174impl < ' hir > IfOrIfLet < ' hir > {
152175 #[ inline]
176+ /// Parses an `if` or `if let` expression
153177 pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
154178 if let ExprKind :: If ( cond, then, r#else) = expr. kind {
155179 if let ExprKind :: DropTemps ( new_cond) = cond. kind {
@@ -160,7 +184,7 @@ impl<'hir> IfOrIfLet<'hir> {
160184 } ) ;
161185 }
162186 if let ExprKind :: Let ( ..) = cond. kind {
163- return Some ( Self { cond, r#else, then } ) ;
187+ return Some ( Self { cond, then , r#else } ) ;
164188 }
165189 }
166190 None
@@ -281,14 +305,17 @@ impl<'a> VecArgs<'a> {
281305 }
282306}
283307
308+ /// A desugared `while` loop
284309pub 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 > ,
288314}
289315
290316impl < ' hir > While < ' hir > {
291317 #[ inline]
318+ /// Parses a desugared `while` loop
292319 pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
293320 if let ExprKind :: Loop (
294321 Block {
@@ -297,11 +324,11 @@ impl<'hir> While<'hir> {
297324 kind :
298325 ExprKind :: If (
299326 Expr {
300- kind : ExprKind :: DropTemps ( if_cond ) ,
327+ kind : ExprKind :: DropTemps ( condition ) ,
301328 ..
302329 } ,
303- if_then ,
304- if_else_ref ,
330+ body ,
331+ _ ,
305332 ) ,
306333 ..
307334 } ) ,
@@ -312,59 +339,53 @@ impl<'hir> While<'hir> {
312339 _,
313340 ) = expr. kind
314341 {
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 } ) ;
321343 }
322344 None
323345 }
324346}
325347
348+ /// A desugared `while let` loop
326349pub struct WhileLet < ' hir > {
327- pub if_expr : & ' hir Expr < ' hir > ,
350+ /// `while let` loop item pattern
328351 pub let_pat : & ' hir Pat < ' hir > ,
352+ /// `while let` loop scrutinee
329353 pub let_expr : & ' hir Expr < ' hir > ,
354+ /// `while let` loop body
330355 pub if_then : & ' hir Expr < ' hir > ,
331- pub if_else : Option < & ' hir Expr < ' hir > > ,
332356}
333357
334358impl < ' hir > WhileLet < ' hir > {
335359 #[ inline]
360+ /// Parses a desugared `while let` loop
336361 pub const fn hir ( expr : & Expr < ' hir > ) -> Option < Self > {
337362 if let ExprKind :: Loop (
338363 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+ ..
340378 } ,
341379 _,
342380 LoopSource :: While ,
343381 _,
344382 ) = expr. kind
345383 {
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+ } ) ;
368389 }
369390 None
370391 }
0 commit comments