Skip to content

Commit 258dbd0

Browse files
author
Palmer Cox
committed
Emit the uppercase variable lint for struct fields that have names with uppercase characters
1 parent e3723dc commit 258dbd0

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/librustc/middle/lint.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
212212
("uppercase_variables",
213213
LintSpec {
214214
lint: UppercaseVariables,
215-
desc: "variable names should start with a lowercase character",
215+
desc: "variable and structure field names should start with a lowercase character",
216216
default: warn
217217
}),
218218

@@ -1201,6 +1201,23 @@ fn check_pat_uppercase_variable(cx: &Context, p: &ast::Pat) {
12011201
}
12021202
}
12031203

1204+
fn check_struct_uppercase_variable(cx: &Context, s: &ast::StructDef) {
1205+
for sf in s.fields.iter() {
1206+
match sf.node {
1207+
ast::StructField_ { kind: ast::NamedField(ident, _), .. } => {
1208+
let s = token::get_ident(ident);
1209+
if s.get().char_at(0).is_uppercase() {
1210+
cx.span_lint(
1211+
UppercaseVariables,
1212+
sf.span,
1213+
"structure field names should start with a lowercase character");
1214+
}
1215+
}
1216+
_ => {}
1217+
}
1218+
}
1219+
}
1220+
12041221
fn check_unnecessary_parens_core(cx: &Context, value: &ast::Expr, msg: &str) {
12051222
match value.node {
12061223
ast::ExprParen(_) => {
@@ -1665,6 +1682,8 @@ impl<'a> Visitor<()> for Context<'a> {
16651682
g: &ast::Generics,
16661683
id: ast::NodeId,
16671684
_: ()) {
1685+
check_struct_uppercase_variable(self, s);
1686+
16681687
let old_id = self.cur_struct_def_id;
16691688
self.cur_struct_def_id = id;
16701689
visit::walk_struct_def(self, s, i, g, id, ());

src/test/compile-fail/lint-uppercase-variables.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use std::io::File;
1414
use std::io::IoError;
1515

16+
struct Something {
17+
X: uint //~ ERROR structure field names should start with a lowercase character
18+
}
19+
1620
fn test(Xx: uint) { //~ ERROR variable names should start with a lowercase character
1721
println!("{}", Xx);
1822
}
@@ -30,5 +34,7 @@ fn main() {
3034
}
3135

3236
test(1);
37+
38+
let _ = Something { X: 0 };
3339
}
3440

0 commit comments

Comments
 (0)