@@ -64,38 +64,30 @@ declare_lint! {
64
64
65
65
declare_lint_pass ! ( WhileTrue => [ WHILE_TRUE ] ) ;
66
66
67
- fn as_while_cond ( expr : & hir:: Expr ) -> Option < & hir:: Expr > {
68
- if let hir:: ExprKind :: Loop ( blk, ..) = & expr. node {
69
- if let Some ( match_expr) = & blk. expr {
70
- if let hir:: ExprKind :: Match ( cond, .., hir:: MatchSource :: WhileDesugar )
71
- = & match_expr. node
72
- {
73
- if let hir:: ExprKind :: DropTemps ( cond) = & cond. node {
74
- return Some ( cond) ;
75
- }
76
- }
77
- }
67
+ /// Traverse through any amount of parenthesis and return the first non-parens expression.
68
+ fn pierce_parens ( mut expr : & ast:: Expr ) -> & ast:: Expr {
69
+ while let ast:: ExprKind :: Paren ( sub) = & expr. node {
70
+ expr = sub;
78
71
}
79
-
80
- None
72
+ expr
81
73
}
82
74
83
- impl < ' a , ' tcx > LateLintPass < ' a , ' tcx > for WhileTrue {
84
- fn check_expr ( & mut self , cx : & LateContext < ' _ , ' _ > , e : & hir :: Expr ) {
85
- if let Some ( ref cond) = as_while_cond ( e ) {
86
- if let hir :: ExprKind :: Lit ( ref lit) = cond. node {
75
+ impl EarlyLintPass for WhileTrue {
76
+ fn check_expr ( & mut self , cx : & EarlyContext < ' _ > , e : & ast :: Expr ) {
77
+ if let ast :: ExprKind :: While ( cond, .. ) = & e . node {
78
+ if let ast :: ExprKind :: Lit ( ref lit) = pierce_parens ( cond) . node {
87
79
if let ast:: LitKind :: Bool ( true ) = lit. node {
88
80
if lit. span . ctxt ( ) == SyntaxContext :: empty ( ) {
89
81
let msg = "denote infinite loops with `loop { ... }`" ;
90
- let condition_span = cx. tcx . sess . source_map ( ) . def_span ( e. span ) ;
91
- let mut err = cx. struct_span_lint ( WHILE_TRUE , condition_span, msg) ;
92
- err . span_suggestion_short (
93
- condition_span,
94
- "use `loop`" ,
95
- "loop" . to_owned ( ) ,
96
- Applicability :: MachineApplicable
97
- ) ;
98
- err . emit ( ) ;
82
+ let condition_span = cx. sess . source_map ( ) . def_span ( e. span ) ;
83
+ cx. struct_span_lint ( WHILE_TRUE , condition_span, msg)
84
+ . span_suggestion_short (
85
+ condition_span,
86
+ "use `loop`" ,
87
+ "loop" . to_owned ( ) ,
88
+ Applicability :: MachineApplicable
89
+ )
90
+ . emit ( ) ;
99
91
}
100
92
}
101
93
}
0 commit comments