1 // Copyright 2015 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.
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.
13 use lint::{LateContext, LintContext, LintArray};
14 use lint::{LintPass, LateLintPass};
17 use syntax::attr::{self, AttrMetaMethods};
18 use syntax::codemap::Span;
21 use rustc_front::visit::FnKind;
24 pub enum MethodLateContext {
30 pub fn method_context(cx: &LateContext, id: ast::NodeId, span: Span) -> MethodLateContext {
31 let def_id = cx.tcx.map.local_def_id(id);
32 match cx.tcx.impl_or_trait_items.borrow().get(&def_id) {
33 None => cx.sess().span_bug(span, "missing method descriptor?!"),
34 Some(item) => match item.container() {
35 ty::TraitContainer(..) => MethodLateContext::TraitDefaultImpl,
36 ty::ImplContainer(cid) => {
37 match cx.tcx.impl_trait_ref(cid) {
38 Some(_) => MethodLateContext::TraitImpl,
39 None => MethodLateContext::PlainImpl
47 pub NON_CAMEL_CASE_TYPES,
49 "types, variants, traits and type parameters should have camel case names"
52 #[derive(Copy, Clone)]
53 pub struct NonCamelCaseTypes;
55 impl NonCamelCaseTypes {
56 fn check_case(&self, cx: &LateContext, sort: &str, name: ast::Name, span: Span) {
57 fn is_camel_case(name: ast::Name) -> bool {
58 let name = name.as_str();
62 let name = name.trim_matches('_');
64 // start with a non-lowercase letter rather than non-uppercase
65 // ones (some scripts don't have a concept of upper/lowercase)
66 !name.is_empty() && !name.char_at(0).is_lowercase() && !name.contains('_')
69 fn to_camel_case(s: &str) -> String {
70 s.split('_').flat_map(|word| word.chars().enumerate().map(|(i, c)|
72 c.to_uppercase().collect::<String>()
74 c.to_lowercase().collect()
76 )).collect::<Vec<_>>().concat()
79 let s = name.as_str();
81 if !is_camel_case(name) {
82 let c = to_camel_case(&s);
83 let m = if c.is_empty() {
84 format!("{} `{}` should have a camel case name such as `CamelCase`", sort, s)
86 format!("{} `{}` should have a camel case name such as `{}`", sort, s, c)
88 cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m[..]);
93 impl LintPass for NonCamelCaseTypes {
94 fn get_lints(&self) -> LintArray {
95 lint_array!(NON_CAMEL_CASE_TYPES)
99 impl LateLintPass for NonCamelCaseTypes {
100 fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
101 let extern_repr_count = it.attrs.iter().filter(|attr| {
102 attr::find_repr_attrs(cx.tcx.sess.diagnostic(), attr).iter()
103 .any(|r| r == &attr::ReprExtern)
105 let has_extern_repr = extern_repr_count > 0;
112 hir::ItemTy(..) | hir::ItemStruct(..) => {
113 self.check_case(cx, "type", it.name, it.span)
115 hir::ItemTrait(..) => {
116 self.check_case(cx, "trait", it.name, it.span)
118 hir::ItemEnum(ref enum_definition, _) => {
122 self.check_case(cx, "type", it.name, it.span);
123 for variant in &enum_definition.variants {
124 self.check_case(cx, "variant", variant.node.name, variant.span);
131 fn check_generics(&mut self, cx: &LateContext, it: &hir::Generics) {
132 for gen in it.ty_params.iter() {
133 self.check_case(cx, "type parameter", gen.name, gen.span);
141 "methods, functions, lifetime parameters and modules should have snake case names"
144 #[derive(Copy, Clone)]
145 pub struct NonSnakeCase;
148 fn to_snake_case(mut str: &str) -> String {
149 let mut words = vec![];
150 // Preserve leading underscores
151 str = str.trim_left_matches(|c: char| {
153 words.push(String::new());
159 for s in str.split('_') {
160 let mut last_upper = false;
161 let mut buf = String::new();
165 for ch in s.chars() {
166 if !buf.is_empty() && buf != "'"
172 last_upper = ch.is_uppercase();
173 buf.extend(ch.to_lowercase());
180 fn check_snake_case(&self, cx: &LateContext, sort: &str, name: &str, span: Option<Span>) {
181 fn is_snake_case(ident: &str) -> bool {
182 if ident.is_empty() {
185 let ident = ident.trim_left_matches('\'');
186 let ident = ident.trim_matches('_');
188 let mut allow_underscore = true;
189 ident.chars().all(|c| {
190 allow_underscore = match c {
191 '_' if !allow_underscore => return false,
193 // It would be more obvious to use `c.is_lowercase()`,
194 // but some characters do not have a lowercase form
195 c if !c.is_uppercase() => true,
202 if !is_snake_case(name) {
203 let sc = NonSnakeCase::to_snake_case(name);
204 let msg = if sc != name {
205 format!("{} `{}` should have a snake case name such as `{}`",
208 format!("{} `{}` should have a snake case name",
212 Some(span) => cx.span_lint(NON_SNAKE_CASE, span, &msg),
213 None => cx.lint(NON_SNAKE_CASE, &msg),
219 impl LintPass for NonSnakeCase {
220 fn get_lints(&self) -> LintArray {
221 lint_array!(NON_SNAKE_CASE)
225 impl LateLintPass for NonSnakeCase {
226 fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
227 let attr_crate_name = cr.attrs.iter().find(|at| at.check_name("crate_name"))
228 .and_then(|at| at.value_str().map(|s| (at, s)));
229 if let Some(ref name) = cx.tcx.sess.opts.crate_name {
230 self.check_snake_case(cx, "crate", name, None);
231 } else if let Some((attr, ref name)) = attr_crate_name {
232 self.check_snake_case(cx, "crate", name, Some(attr.span));
236 fn check_fn(&mut self, cx: &LateContext,
237 fk: FnKind, _: &hir::FnDecl,
238 _: &hir::Block, span: Span, id: ast::NodeId) {
240 FnKind::Method(name, _, _) => match method_context(cx, id, span) {
241 MethodLateContext::PlainImpl => {
242 self.check_snake_case(cx, "method", &name.as_str(), Some(span))
244 MethodLateContext::TraitDefaultImpl => {
245 self.check_snake_case(cx, "trait method", &name.as_str(), Some(span))
249 FnKind::ItemFn(name, _, _, _, _, _) => {
250 self.check_snake_case(cx, "function", &name.as_str(), Some(span))
256 fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
257 if let hir::ItemMod(_) = it.node {
258 self.check_snake_case(cx, "module", &it.name.as_str(), Some(it.span));
262 fn check_trait_item(&mut self, cx: &LateContext, trait_item: &hir::TraitItem) {
263 if let hir::MethodTraitItem(_, None) = trait_item.node {
264 self.check_snake_case(cx, "trait method", &trait_item.name.as_str(),
265 Some(trait_item.span));
269 fn check_lifetime_def(&mut self, cx: &LateContext, t: &hir::LifetimeDef) {
270 self.check_snake_case(cx, "lifetime", &t.lifetime.name.as_str(),
271 Some(t.lifetime.span));
274 fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
275 if let &hir::PatIdent(_, ref path1, _) = &p.node {
276 let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
277 if let Some(def::DefLocal(..)) = def {
278 self.check_snake_case(cx, "variable", &path1.node.name.as_str(), Some(p.span));
283 fn check_struct_def(&mut self, cx: &LateContext, s: &hir::StructDef,
284 _: ast::Name, _: &hir::Generics, _: ast::NodeId) {
285 for sf in &s.fields {
286 if let hir::StructField_ { kind: hir::NamedField(name, _), .. } = sf.node {
287 self.check_snake_case(cx, "structure field", &name.as_str(),
295 pub NON_UPPER_CASE_GLOBALS,
297 "static constants should have uppercase identifiers"
300 #[derive(Copy, Clone)]
301 pub struct NonUpperCaseGlobals;
303 impl NonUpperCaseGlobals {
304 fn check_upper_case(cx: &LateContext, sort: &str, name: ast::Name, span: Span) {
305 let s = name.as_str();
307 if s.chars().any(|c| c.is_lowercase()) {
308 let uc = NonSnakeCase::to_snake_case(&s).to_uppercase();
310 cx.span_lint(NON_UPPER_CASE_GLOBALS, span,
311 &format!("{} `{}` should have an upper case name such as `{}`",
314 cx.span_lint(NON_UPPER_CASE_GLOBALS, span,
315 &format!("{} `{}` should have an upper case name",
322 impl LintPass for NonUpperCaseGlobals {
323 fn get_lints(&self) -> LintArray {
324 lint_array!(NON_UPPER_CASE_GLOBALS)
328 impl LateLintPass for NonUpperCaseGlobals {
329 fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
331 // only check static constants
332 hir::ItemStatic(_, hir::MutImmutable, _) => {
333 NonUpperCaseGlobals::check_upper_case(cx, "static constant", it.name, it.span);
335 hir::ItemConst(..) => {
336 NonUpperCaseGlobals::check_upper_case(cx, "constant", it.name, it.span);
342 fn check_trait_item(&mut self, cx: &LateContext, ti: &hir::TraitItem) {
344 hir::ConstTraitItem(..) => {
345 NonUpperCaseGlobals::check_upper_case(cx, "associated constant",
352 fn check_impl_item(&mut self, cx: &LateContext, ii: &hir::ImplItem) {
354 hir::ConstImplItem(..) => {
355 NonUpperCaseGlobals::check_upper_case(cx, "associated constant",
362 fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
363 // Lint for constants that look like binding identifiers (#7526)
364 match (&p.node, cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def())) {
365 (&hir::PatIdent(_, ref path1, _), Some(def::DefConst(..))) => {
366 NonUpperCaseGlobals::check_upper_case(cx, "constant in pattern",
367 path1.node.name, p.span);