Add lint for variable names that contain uppercase characters
This commit is contained in:
parent
6d9bdf975a
commit
935c912335
2 changed files with 68 additions and 1 deletions
|
@ -80,6 +80,7 @@ pub enum Lint {
|
||||||
NonCamelCaseTypes,
|
NonCamelCaseTypes,
|
||||||
NonUppercaseStatics,
|
NonUppercaseStatics,
|
||||||
NonUppercasePatternStatics,
|
NonUppercasePatternStatics,
|
||||||
|
UppercaseVariables,
|
||||||
UnnecessaryParens,
|
UnnecessaryParens,
|
||||||
TypeLimits,
|
TypeLimits,
|
||||||
TypeOverflow,
|
TypeOverflow,
|
||||||
|
@ -208,7 +209,14 @@ static lint_table: &'static [(&'static str, LintSpec)] = &[
|
||||||
default: warn
|
default: warn
|
||||||
}),
|
}),
|
||||||
|
|
||||||
("unnecessary_parens",
|
("uppercase_variables",
|
||||||
|
LintSpec {
|
||||||
|
lint: UppercaseVariables,
|
||||||
|
desc: "variable names should start with a lowercase character",
|
||||||
|
default: warn
|
||||||
|
}),
|
||||||
|
|
||||||
|
("unnecessary_parens",
|
||||||
LintSpec {
|
LintSpec {
|
||||||
lint: UnnecessaryParens,
|
lint: UnnecessaryParens,
|
||||||
desc: "`if`, `match`, `while` and `return` do not need parentheses",
|
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) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn check_pat_uppercase_variable(cx: &Context, p: &ast::Pat) {
|
||||||
|
let def_map = cx.tcx.def_map.borrow();
|
||||||
|
match &p.node {
|
||||||
|
&ast::PatIdent(_, ref path, _) => {
|
||||||
|
match def_map.get().find(&p.id) {
|
||||||
|
Some(&ast::DefLocal(_, _)) | Some(&ast::DefBinding(_, _)) |
|
||||||
|
Some(&ast::DefArg(_, _)) => {
|
||||||
|
// last identifier alone is right choice for this lint.
|
||||||
|
let ident = path.segments.last().unwrap().identifier;
|
||||||
|
let s = token::get_ident(ident);
|
||||||
|
if s.get().char_at(0).is_uppercase() {
|
||||||
|
cx.span_lint(
|
||||||
|
UppercaseVariables,
|
||||||
|
path.span,
|
||||||
|
"variable names should start with a lowercase character");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn check_unnecessary_parens_core(cx: &Context, value: &ast::Expr, msg: &str) {
|
fn check_unnecessary_parens_core(cx: &Context, value: &ast::Expr, msg: &str) {
|
||||||
match value.node {
|
match value.node {
|
||||||
ast::ExprParen(_) => {
|
ast::ExprParen(_) => {
|
||||||
|
@ -1553,6 +1585,7 @@ impl<'a> Visitor<()> for Context<'a> {
|
||||||
|
|
||||||
fn visit_pat(&mut self, p: &ast::Pat, _: ()) {
|
fn visit_pat(&mut self, p: &ast::Pat, _: ()) {
|
||||||
check_pat_non_uppercase_statics(self, p);
|
check_pat_non_uppercase_statics(self, p);
|
||||||
|
check_pat_uppercase_variable(self, p);
|
||||||
check_unused_mut_pat(self, p);
|
check_unused_mut_pat(self, p);
|
||||||
|
|
||||||
visit::walk_pat(self, p, ());
|
visit::walk_pat(self, p, ());
|
||||||
|
|
34
src/test/compile-fail/lint-uppercase-variables.rs
Normal file
34
src/test/compile-fail/lint-uppercase-variables.rs
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
||||||
|
// file at the top-level directory of this distribution and at
|
||||||
|
// http://rust-lang.org/COPYRIGHT.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||||
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||||
|
// option. This file may not be copied, modified, or distributed
|
||||||
|
// except according to those terms.
|
||||||
|
|
||||||
|
#[deny(uppercase_variables)];
|
||||||
|
|
||||||
|
use std::io::File;
|
||||||
|
use std::io::IoError;
|
||||||
|
|
||||||
|
fn test(Xx: uint) { //~ ERROR variable names should start with a lowercase character
|
||||||
|
println!("{}", Xx);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let Test: uint = 0; //~ ERROR variable names should start with a lowercase character
|
||||||
|
println!("{}", Test);
|
||||||
|
|
||||||
|
let mut f = File::open(&Path::new("something.txt"));
|
||||||
|
let mut buff = [0u8, ..16];
|
||||||
|
match f.read(buff) {
|
||||||
|
Ok(cnt) => println!("read this many bytes: {}", cnt),
|
||||||
|
Err(IoError{ kind: EndOfFile, .. }) => println!("Got end of file: {}", EndOfFile.to_str()),
|
||||||
|
//~^ ERROR variable names should start with a lowercase character
|
||||||
|
}
|
||||||
|
|
||||||
|
test(1);
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue