about summary refs log tree commit diff
path: root/src/rustc
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2012-07-31 18:58:03 -0700
committerBrian Anderson <banderson@mozilla.com>2012-07-31 18:58:23 -0700
commita841789a41941ad73a57cd9972dfbd5fdff02685 (patch)
tree70f8e1237967409d5c8d11b415bc6c4238f37d27 /src/rustc
parent2a3084b527120db7c0645a191ef6147962dbe23d (diff)
downloadrust-a841789a41941ad73a57cd9972dfbd5fdff02685.tar.gz
rust-a841789a41941ad73a57cd9972dfbd5fdff02685.zip
rustc: Add non_camel_case_types lint check
Diffstat (limited to 'src/rustc')
-rw-r--r--src/rustc/middle/lint.rs41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/rustc/middle/lint.rs b/src/rustc/middle/lint.rs
index 94be6865094..2d103398b2e 100644
--- a/src/rustc/middle/lint.rs
+++ b/src/rustc/middle/lint.rs
@@ -49,6 +49,7 @@ enum lint {
     non_implicitly_copyable_typarams,
     vecs_implicitly_copyable,
     deprecated_mode,
+    non_camel_case_types
 }
 
 // This is pretty unfortunate. We really want some sort of "deriving Enum"
@@ -64,6 +65,7 @@ fn int_to_lint(i: int) -> lint {
       6 { non_implicitly_copyable_typarams }
       7 { vecs_implicitly_copyable }
       8 { deprecated_mode }
+      9 { non_camel_case_types }
     }
 }
 
@@ -136,6 +138,11 @@ fn get_lint_dict() -> lint_dict {
         (~"deprecated_mode",
          @{lint: deprecated_mode,
            desc: ~"warn about deprecated uses of modes",
+           default: allow}),
+
+        (~"non_camel_case_types",
+         @{lint: non_camel_case_types,
+           desc: ~"types, variants and traits must have camel case names",
            default: allow})
     ];
     hash_from_strs(v)
@@ -333,6 +340,7 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
     check_item_ctypes(cx, i);
     check_item_while_true(cx, i);
     check_item_path_statement(cx, i);
+    check_item_non_camel_case_types(cx, i);
 }
 
 // Take a visitor, and modify it so that it will not proceed past subitems.
@@ -433,6 +441,39 @@ fn check_item_path_statement(cx: ty::ctxt, it: @ast::item) {
     visit::visit_item(it, (), visit);
 }
 
+fn check_item_non_camel_case_types(cx: ty::ctxt, it: @ast::item) {
+    fn is_camel_case(ident: ast::ident) -> bool {
+        assert ident.is_not_empty();
+        char::is_uppercase(str::char_at(*ident, 0)) &&
+            !ident.contains_char('_')
+    }
+
+    fn check_case(cx: ty::ctxt, ident: ast::ident,
+                  expr_id: ast::node_id, item_id: ast::node_id,
+                  span: span) {
+        if !is_camel_case(ident) {
+            cx.sess.span_lint(
+                non_camel_case_types, expr_id, item_id, span,
+                ~"type, variant, or trait must be camel case");
+        }
+    }
+
+    alt it.node {
+      ast::item_ty(*) | ast::item_class(*) |
+      ast::item_trait(*) | ast::item_impl(*) {
+        check_case(cx, it.ident, it.id, it.id, it.span)
+      }
+      ast::item_enum(variants, _) {
+        check_case(cx, it.ident, it.id, it.id, it.span);
+        for variants.each |variant| {
+            check_case(cx, variant.node.name,
+                       variant.node.id, it.id, variant.span);
+        }
+      }
+      _ { }
+    }
+}
+
 fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
             _body: ast::blk, span: span, id: ast::node_id) {
     debug!{"lint check_fn fk=%? id=%?", fk, id};