diff --git a/src/librustc/middle/lint.rs b/src/librustc/middle/lint.rs index 2ba8121b479ea..6711ec4d1cd91 100644 --- a/src/librustc/middle/lint.rs +++ b/src/librustc/middle/lint.rs @@ -1127,13 +1127,27 @@ fn check_deprecated_owned_vector(cx: &Context, e: &ast::Expr) { fn check_item_non_camel_case_types(cx: &Context, it: &ast::Item) { fn is_camel_case(ident: ast::Ident) -> bool { + // Returns true if char after underscore is invalid. + // Accepts trailing underscores to separate types. + fn invalid_following_underscore(ident: &str) -> bool { + let mut last_was_underscore = false; + for c in ident.chars() { + if last_was_underscore && !"_0123456789".contains_char(c) { + return true; + } + last_was_underscore = c == '_'; + } + false + } + let ident = token::get_ident(ident); assert!(!ident.get().is_empty()); let ident = ident.get().trim_chars(&'_'); // start with a non-lowercase letter rather than non-uppercase // ones (some scripts don't have a concept of upper/lowercase) - !ident.char_at(0).is_lowercase() && !ident.contains_char('_') + !ident.char_at(0).is_lowercase() + && !invalid_following_underscore(ident) } fn check_case(cx: &Context, sort: &str, ident: ast::Ident, span: Span) { diff --git a/src/test/run-pass/lint-non-camel-case-with-numbers-after-underscore.rs b/src/test/run-pass/lint-non-camel-case-with-numbers-after-underscore.rs new file mode 100644 index 0000000000000..005da59dbe2fa --- /dev/null +++ b/src/test/run-pass/lint-non-camel-case-with-numbers-after-underscore.rs @@ -0,0 +1,19 @@ +// Copyright 2012 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// This is ok because we often use underscores to separate numbers + +#[forbid(non_camel_case_types)]; + +type Foo_1_2 = int; +type _Bar = int; +type __Baz__1 = int; + +pub fn main() { }