2018-11-27 02:59:49 +00:00
|
|
|
use rustc::hir::{self, GenericParamKind, PatKind};
|
2016-03-29 12:54:26 +03:00
|
|
|
use rustc::hir::def::Def;
|
2018-11-27 02:59:49 +00:00
|
|
|
use rustc::hir::intravisit::FnKind;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
2018-11-27 02:59:49 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2018-12-16 22:21:47 -05:00
|
|
|
use lint::{EarlyContext, LateContext, LintContext, LintArray};
|
|
|
|
use lint::{EarlyLintPass, LintPass, LateLintPass};
|
2015-09-14 22:36:39 -04:00
|
|
|
use syntax::ast;
|
2016-08-23 03:54:53 +00:00
|
|
|
use syntax::attr;
|
2019-01-04 10:19:52 -05:00
|
|
|
use syntax::errors::Applicability;
|
|
|
|
use syntax_pos::{BytePos, symbol::Ident, Span};
|
2015-09-14 22:36:39 -04:00
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum MethodLateContext {
|
2017-10-09 13:59:20 -03:00
|
|
|
TraitAutoImpl,
|
2015-09-14 22:36:39 -04:00
|
|
|
TraitImpl,
|
2016-10-09 09:38:07 +05:30
|
|
|
PlainImpl,
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2017-04-18 10:54:47 -04:00
|
|
|
pub fn method_context(cx: &LateContext, id: ast::NodeId) -> MethodLateContext {
|
2018-12-04 13:45:36 +01:00
|
|
|
let def_id = cx.tcx.hir().local_def_id(id);
|
2017-04-18 10:54:47 -04:00
|
|
|
let item = cx.tcx.associated_item(def_id);
|
|
|
|
match item.container {
|
2017-10-09 13:59:20 -03:00
|
|
|
ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl,
|
2017-04-18 10:54:47 -04:00
|
|
|
ty::ImplContainer(cid) => {
|
|
|
|
match cx.tcx.impl_trait_ref(cid) {
|
|
|
|
Some(_) => MethodLateContext::TraitImpl,
|
|
|
|
None => MethodLateContext::PlainImpl,
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_CAMEL_CASE_TYPES,
|
|
|
|
Warn,
|
|
|
|
"types, variants, traits and type parameters should have camel case names"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NonCamelCaseTypes;
|
|
|
|
|
|
|
|
impl NonCamelCaseTypes {
|
2018-12-16 22:21:47 -05:00
|
|
|
fn check_case(&self, cx: &EarlyContext, sort: &str, name: ast::Name, span: Span) {
|
2018-01-02 01:06:17 +00:00
|
|
|
fn char_has_case(c: char) -> bool {
|
|
|
|
c.is_lowercase() || c.is_uppercase()
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:46:23 +03:00
|
|
|
fn is_camel_case(name: ast::Name) -> bool {
|
|
|
|
let name = name.as_str();
|
2018-09-10 16:43:50 +03:00
|
|
|
let name = name.trim_matches('_');
|
2015-09-22 20:46:23 +03:00
|
|
|
if name.is_empty() {
|
2015-09-14 22:36:39 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start with a non-lowercase letter rather than non-uppercase
|
|
|
|
// ones (some scripts don't have a concept of upper/lowercase)
|
2017-12-21 11:22:18 +00:00
|
|
|
!name.is_empty() && !name.chars().next().unwrap().is_lowercase() &&
|
2018-01-02 01:06:17 +00:00
|
|
|
!name.contains("__") && !name.chars().collect::<Vec<_>>().windows(2).any(|pair| {
|
|
|
|
// contains a capitalisable character followed by, or preceded by, an underscore
|
|
|
|
char_has_case(pair[0]) && pair[1] == '_' ||
|
|
|
|
char_has_case(pair[1]) && pair[0] == '_'
|
|
|
|
})
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_camel_case(s: &str) -> String {
|
2018-01-02 01:06:17 +00:00
|
|
|
s.trim_matches('_')
|
|
|
|
.split('_')
|
|
|
|
.map(|word| {
|
2016-10-09 09:38:07 +05:30
|
|
|
word.chars().enumerate().map(|(i, c)| if i == 0 {
|
|
|
|
c.to_uppercase().collect::<String>()
|
|
|
|
} else {
|
|
|
|
c.to_lowercase().collect()
|
|
|
|
})
|
2018-03-16 22:35:26 +09:00
|
|
|
.collect::<String>()
|
2016-10-09 09:38:07 +05:30
|
|
|
})
|
2018-01-02 01:06:17 +00:00
|
|
|
.filter(|x| !x.is_empty())
|
2018-03-16 22:35:26 +09:00
|
|
|
.fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
|
2018-01-02 01:06:17 +00:00
|
|
|
// separate two components with an underscore if their boundary cannot
|
|
|
|
// be distinguished using a uppercase/lowercase case distinction
|
|
|
|
let join = if let Some(prev) = prev {
|
|
|
|
let l = prev.chars().last().unwrap();
|
|
|
|
let f = next.chars().next().unwrap();
|
|
|
|
!char_has_case(l) && !char_has_case(f)
|
|
|
|
} else { false };
|
2018-03-16 22:35:26 +09:00
|
|
|
(acc + if join { "_" } else { "" } + &next, Some(next))
|
2018-01-02 01:06:17 +00:00
|
|
|
}).0
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2015-09-22 20:46:23 +03:00
|
|
|
if !is_camel_case(name) {
|
2016-11-17 14:04:20 +00:00
|
|
|
let c = to_camel_case(&name.as_str());
|
2015-09-14 22:36:39 -04:00
|
|
|
let m = if c.is_empty() {
|
2016-11-17 14:04:20 +00:00
|
|
|
format!("{} `{}` should have a camel case name such as `CamelCase`", sort, name)
|
2015-09-14 22:36:39 -04:00
|
|
|
} else {
|
2016-11-17 14:04:20 +00:00
|
|
|
format!("{} `{}` should have a camel case name such as `{}`", sort, name, c)
|
2015-09-14 22:36:39 -04:00
|
|
|
};
|
2017-03-24 09:31:26 +01:00
|
|
|
cx.span_lint(NON_CAMEL_CASE_TYPES, span, &m);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonCamelCaseTypes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_CAMEL_CASE_TYPES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 22:21:47 -05:00
|
|
|
impl EarlyLintPass for NonCamelCaseTypes {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) {
|
2018-01-07 22:05:32 +01:00
|
|
|
let has_repr_c = it.attrs
|
2016-10-09 09:38:07 +05:30
|
|
|
.iter()
|
2018-01-07 22:05:32 +01:00
|
|
|
.any(|attr| {
|
2018-12-16 22:21:47 -05:00
|
|
|
attr::find_repr_attrs(&cx.sess.parse_sess, attr)
|
2016-10-09 09:38:07 +05:30
|
|
|
.iter()
|
2018-01-07 22:05:32 +01:00
|
|
|
.any(|r| r == &attr::ReprC)
|
|
|
|
});
|
2015-09-14 22:36:39 -04:00
|
|
|
|
2018-01-07 22:05:32 +01:00
|
|
|
if has_repr_c {
|
2015-09-14 22:36:39 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match it.node {
|
2018-12-16 22:21:47 -05:00
|
|
|
ast::ItemKind::Ty(..) |
|
|
|
|
ast::ItemKind::Enum(..) |
|
|
|
|
ast::ItemKind::Struct(..) |
|
|
|
|
ast::ItemKind::Union(..) => self.check_case(cx, "type", it.ident.name, it.span),
|
|
|
|
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", it.ident.name, it.span),
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => (),
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 22:21:47 -05:00
|
|
|
fn check_variant(&mut self, cx: &EarlyContext, v: &ast::Variant, _: &ast::Generics) {
|
|
|
|
self.check_case(cx, "variant", v.node.ident.name, v.span);
|
2017-02-14 19:46:48 +09:00
|
|
|
}
|
|
|
|
|
2018-12-16 22:21:47 -05:00
|
|
|
fn check_generic_param(&mut self, cx: &EarlyContext, param: &ast::GenericParam) {
|
|
|
|
if let ast::GenericParamKind::Type { .. } = param.kind {
|
|
|
|
self.check_case(cx, "type parameter", param.ident.name, param.ident.span);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_SNAKE_CASE,
|
|
|
|
Warn,
|
2015-11-03 17:39:51 +01:00
|
|
|
"variables, methods, functions, lifetime parameters and modules should have snake case names"
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NonSnakeCase;
|
|
|
|
|
|
|
|
impl NonSnakeCase {
|
|
|
|
fn to_snake_case(mut str: &str) -> String {
|
|
|
|
let mut words = vec![];
|
|
|
|
// Preserve leading underscores
|
2018-12-05 06:42:56 -08:00
|
|
|
str = str.trim_start_matches(|c: char| {
|
2015-09-14 22:36:39 -04:00
|
|
|
if c == '_' {
|
|
|
|
words.push(String::new());
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for s in str.split('_') {
|
|
|
|
let mut last_upper = false;
|
|
|
|
let mut buf = String::new();
|
|
|
|
if s.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for ch in s.chars() {
|
2016-10-09 09:38:07 +05:30
|
|
|
if !buf.is_empty() && buf != "'" && ch.is_uppercase() && !last_upper {
|
2015-09-14 22:36:39 -04:00
|
|
|
words.push(buf);
|
|
|
|
buf = String::new();
|
|
|
|
}
|
|
|
|
last_upper = ch.is_uppercase();
|
|
|
|
buf.extend(ch.to_lowercase());
|
|
|
|
}
|
|
|
|
words.push(buf);
|
|
|
|
}
|
|
|
|
words.join("_")
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:19:52 -05:00
|
|
|
/// Checks if a given identifier is snake case, and reports a diagnostic if not.
|
|
|
|
fn check_snake_case(&self, cx: &LateContext, sort: &str, ident: &Ident) {
|
2015-09-14 22:36:39 -04:00
|
|
|
fn is_snake_case(ident: &str) -> bool {
|
|
|
|
if ident.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-05 06:42:56 -08:00
|
|
|
let ident = ident.trim_start_matches('\'');
|
2015-09-14 22:36:39 -04:00
|
|
|
let ident = ident.trim_matches('_');
|
|
|
|
|
|
|
|
let mut allow_underscore = true;
|
|
|
|
ident.chars().all(|c| {
|
|
|
|
allow_underscore = match c {
|
|
|
|
'_' if !allow_underscore => return false,
|
|
|
|
'_' => false,
|
|
|
|
// It would be more obvious to use `c.is_lowercase()`,
|
|
|
|
// but some characters do not have a lowercase form
|
|
|
|
c if !c.is_uppercase() => true,
|
|
|
|
_ => return false,
|
|
|
|
};
|
|
|
|
true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:19:52 -05:00
|
|
|
let name = &ident.name.as_str();
|
|
|
|
|
2015-09-14 22:36:39 -04:00
|
|
|
if !is_snake_case(name) {
|
|
|
|
let sc = NonSnakeCase::to_snake_case(name);
|
2019-01-04 10:19:52 -05:00
|
|
|
|
|
|
|
let msg = format!("{} `{}` should have a snake case name", sort, name);
|
|
|
|
let mut err = cx.struct_span_lint(NON_SNAKE_CASE, ident.span, &msg);
|
|
|
|
|
|
|
|
// We have a valid span in almost all cases, but we don't have one when linting a crate
|
|
|
|
// name provided via the command line.
|
|
|
|
if !ident.span.is_dummy() {
|
|
|
|
err.span_suggestion_with_applicability(
|
|
|
|
ident.span,
|
|
|
|
"convert the identifier to snake case",
|
|
|
|
sc,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
2015-09-14 22:36:39 -04:00
|
|
|
} else {
|
2019-01-04 10:19:52 -05:00
|
|
|
err.help(&format!("convert the identifier to snake case: `{}`", sc));
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
2019-01-04 10:19:52 -05:00
|
|
|
|
|
|
|
err.emit();
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonSnakeCase {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_SNAKE_CASE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_crate(&mut self, cx: &LateContext, cr: &hir::Crate) {
|
2019-01-04 10:19:52 -05:00
|
|
|
let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name {
|
|
|
|
Some(Ident::from_str(name))
|
|
|
|
} else {
|
|
|
|
attr::find_by_name(&cr.attrs, "crate_name")
|
|
|
|
.and_then(|attr| attr.meta())
|
|
|
|
.and_then(|meta| {
|
|
|
|
meta.name_value_literal().and_then(|lit| {
|
|
|
|
if let ast::LitKind::Str(name, ..) = lit.node {
|
|
|
|
// Discard the double quotes surrounding the literal.
|
|
|
|
let sp = cx.sess().source_map().span_to_snippet(lit.span)
|
|
|
|
.ok()
|
|
|
|
.and_then(|snippet| {
|
|
|
|
let left = snippet.find('"')?;
|
|
|
|
let right = snippet.rfind('"').map(|pos| snippet.len() - pos)?;
|
|
|
|
|
|
|
|
Some(
|
|
|
|
lit.span
|
|
|
|
.with_lo(lit.span.lo() + BytePos(left as u32 + 1))
|
|
|
|
.with_hi(lit.span.hi() - BytePos(right as u32)),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| lit.span);
|
|
|
|
|
|
|
|
Some(Ident::new(name, sp))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(ident) = &crate_ident {
|
|
|
|
self.check_snake_case(cx, "crate", ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:07:26 +02:00
|
|
|
fn check_generic_param(&mut self, cx: &LateContext, param: &hir::GenericParam) {
|
2019-01-04 10:19:52 -05:00
|
|
|
if let GenericParamKind::Lifetime { .. } = param.kind {
|
|
|
|
self.check_snake_case(cx, "lifetime", ¶m.name.ident());
|
2017-10-16 21:07:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-04 10:19:52 -05:00
|
|
|
fn check_fn(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext,
|
|
|
|
fk: FnKind,
|
|
|
|
_: &hir::FnDecl,
|
|
|
|
_: &hir::Body,
|
|
|
|
_: Span,
|
|
|
|
id: ast::NodeId,
|
|
|
|
) {
|
|
|
|
match &fk {
|
|
|
|
FnKind::Method(ident, ..) => {
|
2017-04-18 10:54:47 -04:00
|
|
|
match method_context(cx, id) {
|
2016-10-09 09:38:07 +05:30
|
|
|
MethodLateContext::PlainImpl => {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "method", ident);
|
2016-10-09 09:38:07 +05:30
|
|
|
}
|
2017-10-09 13:59:20 -03:00
|
|
|
MethodLateContext::TraitAutoImpl => {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "trait method", ident);
|
2016-10-09 09:38:07 +05:30
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 10:19:52 -05:00
|
|
|
FnKind::ItemFn(ident, _, header, _, attrs) => {
|
2017-10-01 16:44:33 -07:00
|
|
|
// Skip foreign-ABI #[no_mangle] functions (Issue #31924)
|
2019-01-04 10:19:52 -05:00
|
|
|
if header.abi != Abi::Rust && attr::contains_name(attrs, "no_mangle") {
|
2017-10-01 16:44:33 -07:00
|
|
|
return;
|
|
|
|
}
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "function", ident);
|
2016-10-09 09:38:07 +05:30
|
|
|
}
|
2016-01-25 14:11:51 +01:00
|
|
|
FnKind::Closure(_) => (),
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2018-07-11 23:36:06 +08:00
|
|
|
if let hir::ItemKind::Mod(_) = it.node {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "module", &it.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 22:46:11 +02:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext, item: &hir::TraitItem) {
|
2019-01-04 10:19:52 -05:00
|
|
|
if let hir::TraitItemKind::Method(_, hir::TraitMethod::Required(pnames)) = &item.node {
|
|
|
|
self.check_snake_case(cx, "trait method", &item.ident);
|
2018-06-10 19:33:30 +03:00
|
|
|
for param_name in pnames {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "variable", param_name);
|
2016-12-20 22:46:11 +02:00
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
|
2019-01-04 10:19:52 -05:00
|
|
|
if let &PatKind::Binding(_, _, ident, _) = &p.node {
|
|
|
|
self.check_snake_case(cx, "variable", &ident);
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-01-04 10:19:52 -05:00
|
|
|
fn check_struct_def(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext,
|
|
|
|
s: &hir::VariantData,
|
|
|
|
_: ast::Name,
|
|
|
|
_: &hir::Generics,
|
|
|
|
_: ast::NodeId,
|
|
|
|
) {
|
2015-10-08 23:45:46 +03:00
|
|
|
for sf in s.fields() {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "structure field", &sf.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub NON_UPPER_CASE_GLOBALS,
|
|
|
|
Warn,
|
|
|
|
"static constants should have uppercase identifiers"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct NonUpperCaseGlobals;
|
|
|
|
|
|
|
|
impl NonUpperCaseGlobals {
|
2019-01-04 15:00:15 -05:00
|
|
|
fn check_upper_case(cx: &LateContext, sort: &str, ident: &Ident) {
|
|
|
|
let name = &ident.name.as_str();
|
|
|
|
|
|
|
|
if name.chars().any(|c| c.is_lowercase()) {
|
|
|
|
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
|
|
|
|
|
|
|
|
let msg = format!("{} `{}` should have an upper case name", sort, name);
|
|
|
|
cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, ident.span, &msg)
|
|
|
|
.span_suggestion_with_applicability(
|
|
|
|
ident.span,
|
|
|
|
"convert the identifier to upper case",
|
|
|
|
uc,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for NonUpperCaseGlobals {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(NON_UPPER_CASE_GLOBALS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonUpperCaseGlobals {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
|
2015-09-14 22:36:39 -04:00
|
|
|
match it.node {
|
2019-01-04 15:00:15 -05:00
|
|
|
hir::ItemKind::Static(..) if !attr::contains_name(&it.attrs, "no_mangle") => {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "static variable", &it.ident);
|
2016-10-14 13:12:42 +03:00
|
|
|
}
|
2018-07-11 23:36:06 +08:00
|
|
|
hir::ItemKind::Const(..) => {
|
2019-01-04 15:00:15 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext, ti: &hir::TraitItem) {
|
2019-01-04 15:00:15 -05:00
|
|
|
if let hir::TraitItemKind::Const(..) = ti.node {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ti.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext, ii: &hir::ImplItem) {
|
2019-01-04 15:00:15 -05:00
|
|
|
if let hir::ImplItemKind::Const(..) = ii.node {
|
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_pat(&mut self, cx: &LateContext, p: &hir::Pat) {
|
2015-09-14 22:36:39 -04:00
|
|
|
// Lint for constants that look like binding identifiers (#7526)
|
2016-10-27 05:17:42 +03:00
|
|
|
if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.node {
|
2017-01-09 17:46:11 +02:00
|
|
|
if let Def::Const(..) = path.def {
|
|
|
|
if path.segments.len() == 1 {
|
2019-01-04 15:00:15 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(
|
|
|
|
cx,
|
|
|
|
"constant in pattern",
|
|
|
|
&path.segments[0].ident
|
|
|
|
);
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|