@@ -5,24 +5,26 @@ import syntax::attr;
5
5
import syntax:: codemap:: span;
6
6
import std:: map:: { map, hashmap, hash_from_strs} ;
7
7
import io:: writer_util;
8
+ import syntax:: print:: pprust:: expr_to_str;
8
9
9
10
export lint, ctypes, unused_imports;
10
11
export level, ignore, warn, error;
11
12
export lookup_lint, lint_dict, get_lint_dict, check_crate;
12
13
13
14
#[ doc="
14
15
15
- A 'lint' check is a kind of miscallaneous constraint that a user _might_ want
16
+ A 'lint' check is a kind of miscellaneous constraint that a user _might_ want
16
17
to enforce, but might reasonably want to permit as well, on a module-by-module
17
18
basis. They contrast with static constraints enforced by other phases of the
18
19
compiler, which are generally required to hold in order to compile the program
19
- correctly at all.
20
+ at all.
20
21
21
22
" ]
22
23
23
24
enum lint {
24
25
ctypes,
25
26
unused_imports,
27
+ while_true
26
28
}
27
29
28
30
enum level {
@@ -35,6 +37,10 @@ type lint_spec = @{lint: lint,
35
37
36
38
type lint_dict = hashmap < str , lint_spec > ;
37
39
40
+ /*
41
+ Pass names should not contain a '-', as the compiler normalizes
42
+ '-' to '_' in command-line flags
43
+ */
38
44
fn get_lint_dict ( ) -> lint_dict {
39
45
let v = [
40
46
( "ctypes" ,
@@ -45,7 +51,12 @@ fn get_lint_dict() -> lint_dict {
45
51
( "unused_imports" ,
46
52
@{ lint: unused_imports,
47
53
desc: "imports that are never used" ,
48
- default : ignore} )
54
+ default : ignore} ) ,
55
+
56
+ ( "while_true" ,
57
+ @{ lint: while_true,
58
+ desc: "suggest using loop { } instead of while(true) { }" ,
59
+ default : warn} )
49
60
] ;
50
61
hash_from_strs ( v)
51
62
}
@@ -165,11 +176,34 @@ fn check_item(cx: ctxt, i: @ast::item) {
165
176
alt lint {
166
177
ctypes { check_item_ctypes( cx, level, i) ; }
167
178
unused_imports { check_item_unused_imports( cx, level, i) ; }
179
+ while_true { check_item_while_true( cx, level, i) ; }
168
180
}
169
181
}
170
182
}
171
183
}
172
184
185
+ fn check_item_while_true ( cx : ctxt , level : level , it : @ast:: item ) {
186
+ let visit = visit:: mk_simple_visitor ( @{
187
+ visit_expr : fn @( e: @ast:: expr) {
188
+ alt e. node {
189
+ ast:: expr_while ( cond, _) {
190
+ alt cond. node {
191
+ ast:: expr_lit ( @{ node: ast:: lit_bool ( true ) , _} ) {
192
+ cx. span_lint (
193
+ level, e. span ,
194
+ "Denote infinite loops with loop { ... }" ) ;
195
+ }
196
+ _ { }
197
+ }
198
+ }
199
+ _ { }
200
+ }
201
+ }
202
+ with * visit:: default_simple_visitor ( )
203
+ } ) ;
204
+ visit:: visit_item ( it, ( ) , visit) ;
205
+ }
206
+
173
207
fn check_item_unused_imports ( _cx : ctxt , _level : level , _it : @ast:: item ) {
174
208
// FIXME: Don't know how to check this in lint yet, it's currently being
175
209
// done over in resolve. When resolve is rewritten, do it here instead.
0 commit comments