Skip to content

Commit c1898b9

Browse files
author
Keegan McAllister
committed
Stop using Default for initializing builtin lints
It wasn't a very appropriate use of the trait. Instead, just enumerate unit structs and those with a `fn new()` separately.
1 parent c7af606 commit c1898b9

File tree

2 files changed

+27
-43
lines changed

2 files changed

+27
-43
lines changed

src/librustc/lint/builtin.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
//! as a `LintPass`.
2222
//!
2323
//! 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.
24+
//! `add_builtin!` or `add_builtin_with_new!` invocation in `context.rs`.
25+
//! Use the former for unit-like structs and the latter for structs with
26+
//! a `pub fn new()`.
2627
2728
use metadata::csearch;
2829
use middle::def::*;
@@ -45,7 +46,6 @@ use std::u16;
4546
use std::u32;
4647
use std::u64;
4748
use std::u8;
48-
use std::default::Default;
4949
use syntax::abi;
5050
use syntax::ast_map;
5151
use syntax::attr::AttrMetaMethods;
@@ -57,7 +57,6 @@ use syntax::{ast, ast_util, visit};
5757
declare_lint!(while_true, Warn,
5858
"suggest using `loop { }` instead of `while true { }`")
5959

60-
#[deriving(Default)]
6160
pub struct WhileTrue;
6261

6362
impl LintPass for WhileTrue {
@@ -90,7 +89,6 @@ impl LintPass for WhileTrue {
9089
declare_lint!(unnecessary_typecast, Allow,
9190
"detects unnecessary type casts, that can be removed")
9291

93-
#[deriving(Default)]
9492
pub struct UnusedCasts;
9593

9694
impl LintPass for UnusedCasts {
@@ -125,8 +123,8 @@ pub struct TypeLimits {
125123
negated_expr_id: ast::NodeId,
126124
}
127125

128-
impl Default for TypeLimits {
129-
fn default() -> TypeLimits {
126+
impl TypeLimits {
127+
pub fn new() -> TypeLimits {
130128
TypeLimits {
131129
negated_expr_id: -1,
132130
}
@@ -320,7 +318,6 @@ impl LintPass for TypeLimits {
320318
declare_lint!(ctypes, Warn,
321319
"proper use of libc types in foreign modules")
322320

323-
#[deriving(Default)]
324321
pub struct CTypes;
325322

326323
impl LintPass for CTypes {
@@ -389,7 +386,6 @@ declare_lint!(owned_heap_memory, Allow,
389386
declare_lint!(heap_memory, Allow,
390387
"use of any (Box type or @ type) heap memory")
391388

392-
#[deriving(Default)]
393389
pub struct HeapMemory;
394390

395391
impl HeapMemory {
@@ -492,8 +488,8 @@ pub struct RawPointerDeriving {
492488
checked_raw_pointers: NodeSet,
493489
}
494490

495-
impl Default for RawPointerDeriving {
496-
fn default() -> RawPointerDeriving {
491+
impl RawPointerDeriving {
492+
pub fn new() -> RawPointerDeriving {
497493
RawPointerDeriving {
498494
checked_raw_pointers: NodeSet::new(),
499495
}
@@ -538,7 +534,6 @@ impl LintPass for RawPointerDeriving {
538534
declare_lint!(unused_attribute, Warn,
539535
"detects attributes that were not used by the compiler")
540536

541-
#[deriving(Default)]
542537
pub struct UnusedAttribute;
543538

544539
impl LintPass for UnusedAttribute {
@@ -620,7 +615,6 @@ impl LintPass for UnusedAttribute {
620615
declare_lint!(path_statement, Warn,
621616
"path statements with no effect")
622617

623-
#[deriving(Default)]
624618
pub struct PathStatement;
625619

626620
impl LintPass for PathStatement {
@@ -648,7 +642,6 @@ declare_lint!(unused_must_use, Warn,
648642
declare_lint!(unused_result, Allow,
649643
"unused result of an expression in a statement")
650644

651-
#[deriving(Default)]
652645
pub struct UnusedResult;
653646

654647
impl LintPass for UnusedResult {
@@ -709,7 +702,6 @@ impl LintPass for UnusedResult {
709702
declare_lint!(deprecated_owned_vector, Allow,
710703
"use of a `~[T]` vector")
711704

712-
#[deriving(Default)]
713705
pub struct DeprecatedOwnedVector;
714706

715707
impl LintPass for DeprecatedOwnedVector {
@@ -735,7 +727,6 @@ impl LintPass for DeprecatedOwnedVector {
735727
declare_lint!(non_camel_case_types, Warn,
736728
"types, variants and traits should have camel case names")
737729

738-
#[deriving(Default)]
739730
pub struct NonCamelCaseTypes;
740731

741732
impl LintPass for NonCamelCaseTypes {
@@ -821,7 +812,6 @@ fn method_context(cx: &Context, m: &ast::Method) -> MethodContext {
821812
declare_lint!(non_snake_case_functions, Warn,
822813
"methods and functions should have snake case names")
823814

824-
#[deriving(Default)]
825815
pub struct NonSnakeCaseFunctions;
826816

827817
impl NonSnakeCaseFunctions {
@@ -899,7 +889,6 @@ impl LintPass for NonSnakeCaseFunctions {
899889
declare_lint!(non_uppercase_statics, Allow,
900890
"static constants should have uppercase identifiers")
901891

902-
#[deriving(Default)]
903892
pub struct NonUppercaseStatics;
904893

905894
impl LintPass for NonUppercaseStatics {
@@ -931,7 +920,6 @@ impl LintPass for NonUppercaseStatics {
931920
declare_lint!(non_uppercase_pattern_statics, Warn,
932921
"static constants in match patterns should be all caps")
933922

934-
#[deriving(Default)]
935923
pub struct NonUppercasePatternStatics;
936924

937925
impl LintPass for NonUppercasePatternStatics {
@@ -962,7 +950,6 @@ impl LintPass for NonUppercasePatternStatics {
962950
declare_lint!(uppercase_variables, Warn,
963951
"variable and structure field names should start with a lowercase character")
964952

965-
#[deriving(Default)]
966953
pub struct UppercaseVariables;
967954

968955
impl LintPass for UppercaseVariables {
@@ -1011,7 +998,6 @@ impl LintPass for UppercaseVariables {
1011998
declare_lint!(unnecessary_parens, Warn,
1012999
"`if`, `match`, `while` and `return` do not need parentheses")
10131000

1014-
#[deriving(Default)]
10151001
pub struct UnnecessaryParens;
10161002

10171003
impl UnnecessaryParens {
@@ -1062,7 +1048,6 @@ impl LintPass for UnnecessaryParens {
10621048
declare_lint!(unused_unsafe, Warn,
10631049
"unnecessary use of an `unsafe` block")
10641050

1065-
#[deriving(Default)]
10661051
pub struct UnusedUnsafe;
10671052

10681053
impl LintPass for UnusedUnsafe {
@@ -1087,7 +1072,6 @@ impl LintPass for UnusedUnsafe {
10871072
declare_lint!(unsafe_block, Allow,
10881073
"usage of an `unsafe` block")
10891074

1090-
#[deriving(Default)]
10911075
pub struct UnsafeBlock;
10921076

10931077
impl LintPass for UnsafeBlock {
@@ -1109,7 +1093,6 @@ impl LintPass for UnsafeBlock {
11091093
declare_lint!(unused_mut, Warn,
11101094
"detect mut variables which don't need to be mutable")
11111095

1112-
#[deriving(Default)]
11131096
pub struct UnusedMut;
11141097

11151098
impl UnusedMut {
@@ -1195,7 +1178,6 @@ enum Allocation {
11951178
declare_lint!(unnecessary_allocation, Warn,
11961179
"detects unnecessary allocations that can be eliminated")
11971180

1198-
#[deriving(Default)]
11991181
pub struct UnnecessaryAllocation;
12001182

12011183
impl LintPass for UnnecessaryAllocation {
@@ -1267,17 +1249,15 @@ pub struct MissingDoc {
12671249
doc_hidden_stack: Vec<bool>,
12681250
}
12691251

1270-
impl Default for MissingDoc {
1271-
fn default() -> MissingDoc {
1252+
impl MissingDoc {
1253+
pub fn new() -> MissingDoc {
12721254
MissingDoc {
12731255
exported_items: None,
12741256
struct_def_stack: vec!(),
12751257
doc_hidden_stack: vec!(false),
12761258
}
12771259
}
1278-
}
12791260

1280-
impl MissingDoc {
12811261
fn doc_hidden(&self) -> bool {
12821262
*self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
12831263
}
@@ -1419,7 +1399,6 @@ declare_lint!(unstable, Allow,
14191399

14201400
/// Checks for use of items with `#[deprecated]`, `#[experimental]` and
14211401
/// `#[unstable]` attributes, or no stability attribute.
1422-
#[deriving(Default)]
14231402
pub struct Stability;
14241403

14251404
impl LintPass for Stability {
@@ -1554,7 +1533,6 @@ declare_lint!(pub variant_size_difference, Allow,
15541533

15551534
/// Does nothing as a lint pass, but registers some `Lint`s
15561535
/// which are used by other parts of the compiler.
1557-
#[deriving(Default)]
15581536
pub struct HardwiredLints;
15591537

15601538
impl LintPass for HardwiredLints {

src/librustc/lint/context.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use lint::builtin;
3737
use std::collections::HashMap;
3838
use std::rc::Rc;
3939
use std::cell::RefCell;
40-
use std::default::Default;
4140
use std::tuple::Tuple2;
4241
use std::mem;
4342
use syntax::ast_util::IdVisitingOperation;
@@ -122,23 +121,30 @@ impl LintStore {
122121
}
123122

124123
pub fn register_builtin(&mut self, sess: Option<&Session>) {
125-
macro_rules! add_builtin_lints ( ( $sess:ident, $($name:ident),*, ) => (
124+
macro_rules! add_builtin ( ( $sess:ident, $($name:ident),*, ) => (
126125
{$(
127-
{
128-
let obj: builtin::$name = Default::default();
129-
self.register_pass($sess, false, box obj as LintPassObject);
130-
};
126+
self.register_pass($sess, false, box builtin::$name as LintPassObject);
131127
)*}
132128
))
133129

134-
add_builtin_lints!(sess, HardwiredLints,
135-
WhileTrue, UnusedCasts, TypeLimits, CTypes, HeapMemory,
136-
RawPointerDeriving, UnusedAttribute, PathStatement,
137-
UnusedResult, DeprecatedOwnedVector, NonCamelCaseTypes,
130+
macro_rules! add_builtin_with_new ( ( $sess:ident, $($name:ident),*, ) => (
131+
{$(
132+
self.register_pass($sess, false, box builtin::$name::new() as LintPassObject);
133+
)*}
134+
))
135+
136+
add_builtin!(sess, HardwiredLints,
137+
WhileTrue, UnusedCasts, CTypes, HeapMemory,
138+
UnusedAttribute, PathStatement, UnusedResult,
139+
DeprecatedOwnedVector, NonCamelCaseTypes,
138140
NonSnakeCaseFunctions, NonUppercaseStatics,
139141
NonUppercasePatternStatics, UppercaseVariables,
140-
UnnecessaryParens, UnusedUnsafe, UnsafeBlock, UnusedMut,
141-
UnnecessaryAllocation, MissingDoc, Stability,
142+
UnnecessaryParens, UnusedUnsafe, UnsafeBlock,
143+
UnusedMut, UnnecessaryAllocation, Stability,
144+
)
145+
146+
add_builtin_with_new!(sess,
147+
TypeLimits, RawPointerDeriving, MissingDoc,
142148
)
143149

144150
// We have one lint pass defined in this module.

0 commit comments

Comments
 (0)