From c1898b9acb324990367bb6672145c30b51e0399f Mon Sep 17 00:00:00 2001 From: Keegan McAllister Date: Thu, 12 Jun 2014 22:20:47 -0700 Subject: [PATCH] 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. --- src/librustc/lint/builtin.rs | 40 ++++++++---------------------------- src/librustc/lint/context.rs | 30 ++++++++++++++++----------- 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index d34014c2b65..f6172b8e7df 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -21,8 +21,9 @@ //! as a `LintPass`. //! //! If you define a new `LintPass`, you will also need to add it to the -//! `add_builtin_lints!()` invocation in `context.rs`. That macro -//! requires a `Default` impl for your `LintPass` type. +//! `add_builtin!` or `add_builtin_with_new!` invocation in `context.rs`. +//! Use the former for unit-like structs and the latter for structs with +//! a `pub fn new()`. use metadata::csearch; use middle::def::*; @@ -45,7 +46,6 @@ use std::u32; use std::u64; use std::u8; -use std::default::Default; use syntax::abi; use syntax::ast_map; use syntax::attr::AttrMetaMethods; @@ -57,7 +57,6 @@ declare_lint!(while_true, Warn, "suggest using `loop { }` instead of `while true { }`") -#[deriving(Default)] pub struct WhileTrue; impl LintPass for WhileTrue { @@ -90,7 +89,6 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { declare_lint!(unnecessary_typecast, Allow, "detects unnecessary type casts, that can be removed") -#[deriving(Default)] pub struct UnusedCasts; impl LintPass for UnusedCasts { @@ -125,8 +123,8 @@ pub struct TypeLimits { negated_expr_id: ast::NodeId, } -impl Default for TypeLimits { - fn default() -> TypeLimits { +impl TypeLimits { + pub fn new() -> TypeLimits { TypeLimits { negated_expr_id: -1, } @@ -320,7 +318,6 @@ fn is_comparison(binop: ast::BinOp) -> bool { declare_lint!(ctypes, Warn, "proper use of libc types in foreign modules") -#[deriving(Default)] pub struct CTypes; impl LintPass for CTypes { @@ -389,7 +386,6 @@ fn check_foreign_fn(cx: &Context, decl: &ast::FnDecl) { declare_lint!(heap_memory, Allow, "use of any (Box type or @ type) heap memory") -#[deriving(Default)] pub struct HeapMemory; impl HeapMemory { @@ -492,8 +488,8 @@ pub struct RawPointerDeriving { checked_raw_pointers: NodeSet, } -impl Default for RawPointerDeriving { - fn default() -> RawPointerDeriving { +impl RawPointerDeriving { + pub fn new() -> RawPointerDeriving { RawPointerDeriving { checked_raw_pointers: NodeSet::new(), } @@ -538,7 +534,6 @@ fn check_item(&mut self, cx: &Context, item: &ast::Item) { declare_lint!(unused_attribute, Warn, "detects attributes that were not used by the compiler") -#[deriving(Default)] pub struct UnusedAttribute; impl LintPass for UnusedAttribute { @@ -620,7 +615,6 @@ fn check_attribute(&mut self, cx: &Context, attr: &ast::Attribute) { declare_lint!(path_statement, Warn, "path statements with no effect") -#[deriving(Default)] pub struct PathStatement; impl LintPass for PathStatement { @@ -648,7 +642,6 @@ fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) { declare_lint!(unused_result, Allow, "unused result of an expression in a statement") -#[deriving(Default)] pub struct UnusedResult; impl LintPass for UnusedResult { @@ -709,7 +702,6 @@ fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) { declare_lint!(deprecated_owned_vector, Allow, "use of a `~[T]` vector") -#[deriving(Default)] pub struct DeprecatedOwnedVector; impl LintPass for DeprecatedOwnedVector { @@ -735,7 +727,6 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { declare_lint!(non_camel_case_types, Warn, "types, variants and traits should have camel case names") -#[deriving(Default)] pub struct NonCamelCaseTypes; impl LintPass for NonCamelCaseTypes { @@ -821,7 +812,6 @@ fn method_context(cx: &Context, m: &ast::Method) -> MethodContext { declare_lint!(non_snake_case_functions, Warn, "methods and functions should have snake case names") -#[deriving(Default)] pub struct NonSnakeCaseFunctions; impl NonSnakeCaseFunctions { @@ -899,7 +889,6 @@ fn check_ty_method(&mut self, cx: &Context, t: &ast::TypeMethod) { declare_lint!(non_uppercase_statics, Allow, "static constants should have uppercase identifiers") -#[deriving(Default)] pub struct NonUppercaseStatics; impl LintPass for NonUppercaseStatics { @@ -931,7 +920,6 @@ fn check_item(&mut self, cx: &Context, it: &ast::Item) { declare_lint!(non_uppercase_pattern_statics, Warn, "static constants in match patterns should be all caps") -#[deriving(Default)] pub struct NonUppercasePatternStatics; impl LintPass for NonUppercasePatternStatics { @@ -962,7 +950,6 @@ fn check_pat(&mut self, cx: &Context, p: &ast::Pat) { declare_lint!(uppercase_variables, Warn, "variable and structure field names should start with a lowercase character") -#[deriving(Default)] pub struct UppercaseVariables; impl LintPass for UppercaseVariables { @@ -1011,7 +998,6 @@ fn check_struct_def(&mut self, cx: &Context, s: &ast::StructDef, declare_lint!(unnecessary_parens, Warn, "`if`, `match`, `while` and `return` do not need parentheses") -#[deriving(Default)] pub struct UnnecessaryParens; impl UnnecessaryParens { @@ -1062,7 +1048,6 @@ fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) { declare_lint!(unused_unsafe, Warn, "unnecessary use of an `unsafe` block") -#[deriving(Default)] pub struct UnusedUnsafe; impl LintPass for UnusedUnsafe { @@ -1087,7 +1072,6 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { declare_lint!(unsafe_block, Allow, "usage of an `unsafe` block") -#[deriving(Default)] pub struct UnsafeBlock; impl LintPass for UnsafeBlock { @@ -1109,7 +1093,6 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { declare_lint!(unused_mut, Warn, "detect mut variables which don't need to be mutable") -#[deriving(Default)] pub struct UnusedMut; impl UnusedMut { @@ -1195,7 +1178,6 @@ enum Allocation { declare_lint!(unnecessary_allocation, Warn, "detects unnecessary allocations that can be eliminated") -#[deriving(Default)] pub struct UnnecessaryAllocation; impl LintPass for UnnecessaryAllocation { @@ -1267,17 +1249,15 @@ pub struct MissingDoc { doc_hidden_stack: Vec, } -impl Default for MissingDoc { - fn default() -> MissingDoc { +impl MissingDoc { + pub fn new() -> MissingDoc { MissingDoc { exported_items: None, struct_def_stack: vec!(), doc_hidden_stack: vec!(false), } } -} -impl MissingDoc { fn doc_hidden(&self) -> bool { *self.doc_hidden_stack.last().expect("empty doc_hidden_stack") } @@ -1419,7 +1399,6 @@ fn check_variant(&mut self, cx: &Context, v: &ast::Variant, _: &ast::Generics) { /// Checks for use of items with `#[deprecated]`, `#[experimental]` and /// `#[unstable]` attributes, or no stability attribute. -#[deriving(Default)] pub struct Stability; impl LintPass for Stability { @@ -1554,7 +1533,6 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { /// Does nothing as a lint pass, but registers some `Lint`s /// which are used by other parts of the compiler. -#[deriving(Default)] pub struct HardwiredLints; impl LintPass for HardwiredLints { diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 3f7a4ceb74b..bcb0193f344 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -37,7 +37,6 @@ use std::collections::HashMap; use std::rc::Rc; use std::cell::RefCell; -use std::default::Default; use std::tuple::Tuple2; use std::mem; use syntax::ast_util::IdVisitingOperation; @@ -122,23 +121,30 @@ pub fn register_pass(&mut self, sess: Option<&Session>, } pub fn register_builtin(&mut self, sess: Option<&Session>) { - macro_rules! add_builtin_lints ( ( $sess:ident, $($name:ident),*, ) => ( + macro_rules! add_builtin ( ( $sess:ident, $($name:ident),*, ) => ( {$( - { - let obj: builtin::$name = Default::default(); - self.register_pass($sess, false, box obj as LintPassObject); - }; + self.register_pass($sess, false, box builtin::$name as LintPassObject); )*} )) - add_builtin_lints!(sess, HardwiredLints, - WhileTrue, UnusedCasts, TypeLimits, CTypes, HeapMemory, - RawPointerDeriving, UnusedAttribute, PathStatement, - UnusedResult, DeprecatedOwnedVector, NonCamelCaseTypes, + macro_rules! add_builtin_with_new ( ( $sess:ident, $($name:ident),*, ) => ( + {$( + self.register_pass($sess, false, box builtin::$name::new() as LintPassObject); + )*} + )) + + add_builtin!(sess, HardwiredLints, + WhileTrue, UnusedCasts, CTypes, HeapMemory, + UnusedAttribute, PathStatement, UnusedResult, + DeprecatedOwnedVector, NonCamelCaseTypes, NonSnakeCaseFunctions, NonUppercaseStatics, NonUppercasePatternStatics, UppercaseVariables, - UnnecessaryParens, UnusedUnsafe, UnsafeBlock, UnusedMut, - UnnecessaryAllocation, MissingDoc, Stability, + UnnecessaryParens, UnusedUnsafe, UnsafeBlock, + UnusedMut, UnnecessaryAllocation, Stability, + ) + + add_builtin_with_new!(sess, + TypeLimits, RawPointerDeriving, MissingDoc, ) // We have one lint pass defined in this module. -- GitLab