Skip to content

Commit a841789

Browse files
committed
rustc: Add non_camel_case_types lint check
1 parent 2a3084b commit a841789

6 files changed

+78
-0
lines changed

src/rustc/middle/lint.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ enum lint {
4949
non_implicitly_copyable_typarams,
5050
vecs_implicitly_copyable,
5151
deprecated_mode,
52+
non_camel_case_types
5253
}
5354

5455
// This is pretty unfortunate. We really want some sort of "deriving Enum"
@@ -64,6 +65,7 @@ fn int_to_lint(i: int) -> lint {
6465
6 { non_implicitly_copyable_typarams }
6566
7 { vecs_implicitly_copyable }
6667
8 { deprecated_mode }
68+
9 { non_camel_case_types }
6769
}
6870
}
6971

@@ -136,6 +138,11 @@ fn get_lint_dict() -> lint_dict {
136138
(~"deprecated_mode",
137139
@{lint: deprecated_mode,
138140
desc: ~"warn about deprecated uses of modes",
141+
default: allow}),
142+
143+
(~"non_camel_case_types",
144+
@{lint: non_camel_case_types,
145+
desc: ~"types, variants and traits must have camel case names",
139146
default: allow})
140147
];
141148
hash_from_strs(v)
@@ -333,6 +340,7 @@ fn check_item(i: @ast::item, cx: ty::ctxt) {
333340
check_item_ctypes(cx, i);
334341
check_item_while_true(cx, i);
335342
check_item_path_statement(cx, i);
343+
check_item_non_camel_case_types(cx, i);
336344
}
337345

338346
// 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) {
433441
visit::visit_item(it, (), visit);
434442
}
435443

444+
fn check_item_non_camel_case_types(cx: ty::ctxt, it: @ast::item) {
445+
fn is_camel_case(ident: ast::ident) -> bool {
446+
assert ident.is_not_empty();
447+
char::is_uppercase(str::char_at(*ident, 0)) &&
448+
!ident.contains_char('_')
449+
}
450+
451+
fn check_case(cx: ty::ctxt, ident: ast::ident,
452+
expr_id: ast::node_id, item_id: ast::node_id,
453+
span: span) {
454+
if !is_camel_case(ident) {
455+
cx.sess.span_lint(
456+
non_camel_case_types, expr_id, item_id, span,
457+
~"type, variant, or trait must be camel case");
458+
}
459+
}
460+
461+
alt it.node {
462+
ast::item_ty(*) | ast::item_class(*) |
463+
ast::item_trait(*) | ast::item_impl(*) {
464+
check_case(cx, it.ident, it.id, it.id, it.span)
465+
}
466+
ast::item_enum(variants, _) {
467+
check_case(cx, it.ident, it.id, it.id, it.span);
468+
for variants.each |variant| {
469+
check_case(cx, variant.node.name,
470+
variant.node.id, it.id, variant.span);
471+
}
472+
}
473+
_ { }
474+
}
475+
}
476+
436477
fn check_fn(tcx: ty::ctxt, fk: visit::fn_kind, decl: ast::fn_decl,
437478
_body: ast::blk, span: span, id: ast::node_id) {
438479
debug!{"lint check_fn fk=%? id=%?", fk, id};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[forbid(non_camel_case_types)]
2+
class foo { //~ ERROR type, variant, or trait must be camel case
3+
let bar: int;
4+
5+
new() {
6+
self.bar = 0;
7+
}
8+
}
9+
10+
fn main() {
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[forbid(non_camel_case_types)]
2+
enum foo { //~ ERROR type, variant, or trait must be camel case
3+
Bar
4+
}
5+
6+
fn main() {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[forbid(non_camel_case_types)]
2+
struct foo { //~ ERROR type, variant, or trait must be camel case
3+
bar: int;
4+
}
5+
6+
fn main() {
7+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[forbid(non_camel_case_types)]
2+
type foo = int; //~ ERROR type, variant, or trait must be camel case
3+
4+
fn main() {
5+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[forbid(non_camel_case_types)]
2+
enum Foo {
3+
bar //~ ERROR type, variant, or trait must be camel case
4+
}
5+
6+
fn main() {
7+
}

0 commit comments

Comments
 (0)