Skip to content

Commit 935c912

Browse files
author
Palmer Cox
committed
Add lint for variable names that contain uppercase characters
1 parent 6d9bdf9 commit 935c912

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

src/librustc/middle/lint.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub enum Lint {
8080
NonCamelCaseTypes,
8181
NonUppercaseStatics,
8282
NonUppercasePatternStatics,
83+
UppercaseVariables,
8384
UnnecessaryParens,
8485
TypeLimits,
8586
TypeOverflow,
@@ -208,7 +209,14 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
208209
default: warn
209210
}),
210211

211-
("unnecessary_parens",
212+
("uppercase_variables",
213+
LintSpec {
214+
lint: UppercaseVariables,
215+
desc: "variable names should start with a lowercase character",
216+
default: warn
217+
}),
218+
219+
("unnecessary_parens",
212220
LintSpec {
213221
lint: UnnecessaryParens,
214222
desc: "`if`, `match`, `while` and `return` do not need parentheses",
@@ -1169,6 +1177,30 @@ fn check_pat_non_uppercase_statics(cx: &Context, p: &ast::Pat) {
11691177
}
11701178
}
11711179

1180+
fn check_pat_uppercase_variable(cx: &Context, p: &ast::Pat) {
1181+
let def_map = cx.tcx.def_map.borrow();
1182+
match &p.node {
1183+
&ast::PatIdent(_, ref path, _) => {
1184+
match def_map.get().find(&p.id) {
1185+
Some(&ast::DefLocal(_, _)) | Some(&ast::DefBinding(_, _)) |
1186+
Some(&ast::DefArg(_, _)) => {
1187+
// last identifier alone is right choice for this lint.
1188+
let ident = path.segments.last().unwrap().identifier;
1189+
let s = token::get_ident(ident);
1190+
if s.get().char_at(0).is_uppercase() {
1191+
cx.span_lint(
1192+
UppercaseVariables,
1193+
path.span,
1194+
"variable names should start with a lowercase character");
1195+
}
1196+
}
1197+
_ => {}
1198+
}
1199+
}
1200+
_ => {}
1201+
}
1202+
}
1203+
11721204
fn check_unnecessary_parens_core(cx: &Context, value: &ast::Expr, msg: &str) {
11731205
match value.node {
11741206
ast::ExprParen(_) => {
@@ -1553,6 +1585,7 @@ impl<'a> Visitor<()> for Context<'a> {
15531585

15541586
fn visit_pat(&mut self, p: &ast::Pat, _: ()) {
15551587
check_pat_non_uppercase_statics(self, p);
1588+
check_pat_uppercase_variable(self, p);
15561589
check_unused_mut_pat(self, p);
15571590

15581591
visit::walk_pat(self, p, ());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#[deny(uppercase_variables)];
12+
13+
use std::io::File;
14+
use std::io::IoError;
15+
16+
fn test(Xx: uint) { //~ ERROR variable names should start with a lowercase character
17+
println!("{}", Xx);
18+
}
19+
20+
fn main() {
21+
let Test: uint = 0; //~ ERROR variable names should start with a lowercase character
22+
println!("{}", Test);
23+
24+
let mut f = File::open(&Path::new("something.txt"));
25+
let mut buff = [0u8, ..16];
26+
match f.read(buff) {
27+
Ok(cnt) => println!("read this many bytes: {}", cnt),
28+
Err(IoError{ kind: EndOfFile, .. }) => println!("Got end of file: {}", EndOfFile.to_str()),
29+
//~^ ERROR variable names should start with a lowercase character
30+
}
31+
32+
test(1);
33+
}
34+

0 commit comments

Comments
 (0)