9
9
// except according to those terms.
10
10
11
11
//! Lints built in to rustc.
12
+ //!
13
+ //! This is a sibling of `lint::context` in order to ensure that
14
+ //! lints implemented here use the same public API as lint plugins.
15
+ //!
16
+ //! To add a new lint to rustc, declare it here using `declare_lint!()`.
17
+ //! Then add code to emit the new lint in the appropriate circumstances.
18
+ //! You can do that in an existing `LintPass` if it makes sense, or in
19
+ //! a new `LintPass`, or using `Session::add_lint` elsewhere in the
20
+ //! compiler. Only do the latter if the check can't be written cleanly
21
+ //! as a `LintPass`.
22
+ //!
23
+ //! If you define a new `LintPass`, you will also need to add it to the
24
+ //! `add_builtin_lints!()` invocation in `context.rs`. That macro
25
+ //! requires a `Default` impl for your `LintPass` type.
12
26
13
27
use metadata:: csearch;
14
28
use middle:: def:: * ;
15
29
use middle:: trans:: adt; // for `adt::is_ffi_safe`
16
- use middle:: typeck:: astconv:: { ast_ty_to_ty, AstConv } ;
30
+ use middle:: typeck:: astconv:: ast_ty_to_ty;
17
31
use middle:: typeck:: infer;
18
32
use middle:: privacy:: ExportedItems ;
19
33
use middle:: { typeck, ty, def, pat_util} ;
20
34
use util:: ppaux:: { ty_to_str} ;
21
35
use util:: nodemap:: NodeSet ;
22
36
use lint:: { Context , LintPass , LintArray } ;
23
- use lint;
24
37
25
38
use std:: cmp;
26
39
use std:: collections:: HashMap ;
@@ -169,7 +182,7 @@ impl LintPass for TypeLimits {
169
182
match ty:: get ( ty:: expr_ty ( cx. tcx , e) ) . sty {
170
183
ty:: ty_int( t) => {
171
184
let int_type = if t == ast:: TyI {
172
- cx. tcx . sess . targ_cfg . int_type
185
+ cx. sess ( ) . targ_cfg . int_type
173
186
} else { t } ;
174
187
let ( min, max) = int_ty_range ( int_type) ;
175
188
let mut lit_val: i64 = match lit. node {
@@ -188,7 +201,7 @@ impl LintPass for TypeLimits {
188
201
} ,
189
202
ty:: ty_uint( t) => {
190
203
let uint_type = if t == ast:: TyU {
191
- cx. tcx . sess . targ_cfg . uint_type
204
+ cx. sess ( ) . targ_cfg . uint_type
192
205
} else { t } ;
193
206
let ( min, max) = uint_ty_range ( uint_type) ;
194
207
let lit_val: u64 = match lit. node {
@@ -430,9 +443,9 @@ impl LintPass for HeapMemory {
430
443
ast:: ItemFn ( ..) |
431
444
ast:: ItemTy ( ..) |
432
445
ast:: ItemEnum ( ..) |
433
- ast:: ItemStruct ( ..) => self . check_heap_type ( cx , it . span ,
434
- ty :: node_id_to_type ( cx. tcx ,
435
- it. id ) ) ,
446
+ ast:: ItemStruct ( ..)
447
+ => self . check_heap_type ( cx, it . span ,
448
+ ty :: node_id_to_type ( cx . tcx , it. id ) ) ,
436
449
_ => ( )
437
450
}
438
451
@@ -441,8 +454,7 @@ impl LintPass for HeapMemory {
441
454
ast:: ItemStruct ( struct_def, _) => {
442
455
for struct_field in struct_def. fields . iter ( ) {
443
456
self . check_heap_type ( cx, struct_field. span ,
444
- ty:: node_id_to_type ( cx. tcx ,
445
- struct_field. node . id ) ) ;
457
+ ty:: node_id_to_type ( cx. tcx , struct_field. node . id ) ) ;
446
458
}
447
459
}
448
460
_ => ( )
@@ -677,7 +689,7 @@ impl LintPass for UnusedResult {
677
689
_ => { }
678
690
}
679
691
} else {
680
- csearch:: get_item_attrs ( & cx. tcx . sess . cstore , did, |attrs| {
692
+ csearch:: get_item_attrs ( & cx. sess ( ) . cstore , did, |attrs| {
681
693
if attr:: contains_name ( attrs. as_slice ( ) , "must_use" ) {
682
694
cx. span_lint ( unused_must_use, s. span ,
683
695
"unused result which must be used" ) ;
@@ -711,7 +723,7 @@ impl LintPass for DeprecatedOwnedVector {
711
723
ty:: ty_uniq( t) => match ty:: get ( t) . sty {
712
724
ty:: ty_vec( _, None ) => {
713
725
cx. span_lint ( deprecated_owned_vector, e. span ,
714
- "use of deprecated `~[]` vector; replaced by `std::vec::Vec`" )
726
+ "use of deprecated `~[]` vector; replaced by `std::vec::Vec`" )
715
727
}
716
728
_ => { }
717
729
} ,
@@ -791,7 +803,7 @@ fn method_context(cx: &Context, m: &ast::Method) -> MethodContext {
791
803
} ;
792
804
793
805
match cx. tcx . methods . borrow ( ) . find_copy ( & did) {
794
- None => cx. tcx . sess . span_bug ( m. span , "missing method descriptor?!" ) ,
806
+ None => cx. sess ( ) . span_bug ( m. span , "missing method descriptor?!" ) ,
795
807
Some ( md) => {
796
808
match md. container {
797
809
ty:: TraitContainer ( ..) => TraitDefaultImpl ,
@@ -1110,15 +1122,14 @@ impl UnusedMut {
1110
1122
match mode {
1111
1123
ast:: BindByValue ( ast:: MutMutable ) => {
1112
1124
if path. segments . len ( ) != 1 {
1113
- cx. tcx . sess . span_bug ( p. span ,
1125
+ cx. sess ( ) . span_bug ( p. span ,
1114
1126
"mutable binding that doesn't consist \
1115
1127
of exactly one segment") ;
1116
1128
}
1117
1129
let ident = path. segments . get ( 0 ) . identifier ;
1118
1130
if !token:: get_ident ( ident) . get ( ) . starts_with ( "_" ) {
1119
- mutables. insert_or_update_with ( ident. name as uint , vec ! ( id) , |_, old| {
1120
- old. push ( id) ;
1121
- } ) ;
1131
+ mutables. insert_or_update_with ( ident. name as uint ,
1132
+ vec ! ( id) , |_, old| { old. push ( id) ; } ) ;
1122
1133
}
1123
1134
}
1124
1135
_ => {
@@ -1279,7 +1290,7 @@ impl MissingDoc {
1279
1290
desc : & ' static str ) {
1280
1291
// If we're building a test harness, then warning about
1281
1292
// documentation is probably not really relevant right now.
1282
- if cx. tcx . sess . opts . test { return }
1293
+ if cx. sess ( ) . opts . test { return }
1283
1294
1284
1295
// `#[doc(hidden)]` disables missing_doc check.
1285
1296
if self . doc_hidden ( ) { return }
@@ -1358,7 +1369,8 @@ impl LintPass for MissingDoc {
1358
1369
}
1359
1370
1360
1371
fn check_fn ( & mut self , cx : & Context ,
1361
- fk : & visit:: FnKind , _: & ast:: FnDecl , _: & ast:: Block , _: Span , _: ast:: NodeId ) {
1372
+ fk : & visit:: FnKind , _: & ast:: FnDecl ,
1373
+ _: & ast:: Block , _: Span , _: ast:: NodeId ) {
1362
1374
match * fk {
1363
1375
visit:: FkMethod ( _, _, m) => {
1364
1376
// If the method is an impl for a trait, don't doc.
@@ -1381,7 +1393,8 @@ impl LintPass for MissingDoc {
1381
1393
fn check_struct_field ( & mut self , cx : & Context , sf : & ast:: StructField ) {
1382
1394
match sf. node . kind {
1383
1395
ast:: NamedField ( _, vis) if vis == ast:: Public => {
1384
- let cur_struct_def = * self . struct_def_stack . last ( ) . expect ( "empty struct_def_stack" ) ;
1396
+ let cur_struct_def = * self . struct_def_stack . last ( )
1397
+ . expect ( "empty struct_def_stack" ) ;
1385
1398
self . check_missing_doc_attrs ( cx, Some ( cur_struct_def) ,
1386
1399
sf. node . attrs . as_slice ( ) , sf. span , "a struct field" )
1387
1400
}
@@ -1404,8 +1417,8 @@ declare_lint!(experimental, Warn,
1404
1417
declare_lint ! ( unstable, Allow ,
1405
1418
"detects use of #[unstable] items (incl. items with no stability attribute)" )
1406
1419
1407
- /// Checks for use of items with #[deprecated], #[experimental] and
1408
- /// #[unstable] ( or none of them) attributes .
1420
+ /// Checks for use of items with ` #[deprecated]`, ` #[experimental]` and
1421
+ /// ` #[unstable]` attributes, or no stability attribute .
1409
1422
#[ deriving( Default ) ]
1410
1423
pub struct Stability ;
1411
1424
@@ -1472,7 +1485,7 @@ impl LintPass for Stability {
1472
1485
let mut s = None ;
1473
1486
// run through all the attributes and take the first
1474
1487
// stability one.
1475
- csearch:: get_item_attrs ( & cx. tcx . sess . cstore , id, |attrs| {
1488
+ csearch:: get_item_attrs ( & cx. sess ( ) . cstore , id, |attrs| {
1476
1489
if s. is_none ( ) {
1477
1490
s = attr:: find_stability ( attrs. as_slice ( ) )
1478
1491
}
@@ -1503,32 +1516,6 @@ impl LintPass for Stability {
1503
1516
}
1504
1517
}
1505
1518
1506
- /// Doesn't actually warn; just gathers information for use by
1507
- /// checks in trans.
1508
- #[ deriving( Default ) ]
1509
- pub struct GatherNodeLevels ;
1510
-
1511
- impl LintPass for GatherNodeLevels {
1512
- fn get_lints ( & self ) -> LintArray {
1513
- lint_array ! ( )
1514
- }
1515
-
1516
- fn check_item ( & mut self , cx : & Context , it : & ast:: Item ) {
1517
- match it. node {
1518
- ast:: ItemEnum ( ..) => {
1519
- let lint_id = lint:: LintId :: of ( variant_size_difference) ;
1520
- match cx. lints . get_level_source ( lint_id) {
1521
- lvlsrc @ ( lvl, _) if lvl != lint:: Allow => {
1522
- cx. insert_node_level ( it. id , lint_id, lvlsrc) ;
1523
- } ,
1524
- _ => { }
1525
- }
1526
- } ,
1527
- _ => { }
1528
- }
1529
- }
1530
- }
1531
-
1532
1519
declare_lint ! ( pub unused_imports, Warn ,
1533
1520
"imports that are never used" )
1534
1521
0 commit comments