2020-01-09 07:52:01 +01:00
|
|
|
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
2020-01-11 13:15:20 +01:00
|
|
|
use rustc_attr as attr;
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::Applicability;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_hir::def::{DefKind, Res};
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit::FnKind;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::{GenericParamKind, PatKind};
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{symbol::Ident, BytePos, Span};
|
2018-11-27 02:59:49 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2015-09-14 22:36:39 -04:00
|
|
|
use syntax::ast;
|
|
|
|
|
|
|
|
#[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
|
|
|
}
|
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
pub fn method_context(cx: &LateContext<'_, '_>, id: hir::HirId) -> MethodLateContext {
|
2019-06-27 11:28:14 +02: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,
|
2019-12-22 17:42:04 -05: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"
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:05:40 +02:00
|
|
|
declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]);
|
|
|
|
|
2019-02-12 12:35:38 -05:00
|
|
|
fn char_has_case(c: char) -> bool {
|
|
|
|
c.is_lowercase() || c.is_uppercase()
|
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
|
2019-02-12 12:35:38 -05:00
|
|
|
fn is_camel_case(name: &str) -> bool {
|
|
|
|
let name = name.trim_matches('_');
|
|
|
|
if name.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-01-02 01:06:17 +00:00
|
|
|
|
2019-02-12 12:35:38 -05:00
|
|
|
// start with a non-lowercase letter rather than non-uppercase
|
|
|
|
// ones (some scripts don't have a concept of upper/lowercase)
|
|
|
|
!name.chars().next().unwrap().is_lowercase()
|
|
|
|
&& !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] == '_'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_camel_case(s: &str) -> String {
|
|
|
|
s.trim_matches('_')
|
|
|
|
.split('_')
|
|
|
|
.filter(|component| !component.is_empty())
|
|
|
|
.map(|component| {
|
|
|
|
let mut camel_cased_component = String::new();
|
|
|
|
|
|
|
|
let mut new_word = true;
|
|
|
|
let mut prev_is_lower_case = true;
|
|
|
|
|
|
|
|
for c in component.chars() {
|
|
|
|
// Preserve the case if an uppercase letter follows a lowercase letter, so that
|
|
|
|
// `camelCase` is converted to `CamelCase`.
|
|
|
|
if prev_is_lower_case && c.is_uppercase() {
|
|
|
|
new_word = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if new_word {
|
|
|
|
camel_cased_component.push_str(&c.to_uppercase().to_string());
|
|
|
|
} else {
|
|
|
|
camel_cased_component.push_str(&c.to_lowercase().to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_is_lower_case = c.is_lowercase();
|
|
|
|
new_word = false;
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-02-12 12:35:38 -05:00
|
|
|
camel_cased_component
|
|
|
|
})
|
2019-12-22 17:42:04 -05:00
|
|
|
.fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
|
|
|
|
// 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
|
|
|
|
};
|
|
|
|
(acc + if join { "_" } else { "" } + &next, Some(next))
|
|
|
|
})
|
2019-02-12 12:35:38 -05:00
|
|
|
.0
|
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
|
2019-02-12 12:35:38 -05:00
|
|
|
impl NonCamelCaseTypes {
|
|
|
|
fn check_case(&self, cx: &EarlyContext<'_>, sort: &str, ident: &Ident) {
|
2019-01-04 15:59:07 -05:00
|
|
|
let name = &ident.name.as_str();
|
|
|
|
|
2015-09-22 20:46:23 +03:00
|
|
|
if !is_camel_case(name) {
|
2019-02-12 12:35:38 -05:00
|
|
|
let msg = format!("{} `{}` should have an upper camel case name", sort, name);
|
2020-01-31 22:24:57 +10:00
|
|
|
cx.struct_span_lint(NON_CAMEL_CASE_TYPES, ident.span, |lint| {
|
|
|
|
lint.build(&msg).span_suggestion(
|
2019-01-04 15:59:07 -05:00
|
|
|
ident.span,
|
2019-02-12 12:35:38 -05:00
|
|
|
"convert the identifier to upper camel case",
|
|
|
|
to_camel_case(name),
|
2019-01-04 15:59:07 -05:00
|
|
|
Applicability::MaybeIncorrect,
|
2020-01-31 22:24:57 +10:00
|
|
|
).emit()
|
|
|
|
})
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-16 22:21:47 -05:00
|
|
|
impl EarlyLintPass for NonCamelCaseTypes {
|
2019-02-08 20:35:41 +09:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
|
2019-12-22 17:42:04 -05:00
|
|
|
let has_repr_c = it
|
|
|
|
.attrs
|
2016-10-09 09:38:07 +05:30
|
|
|
.iter()
|
2019-02-12 12:35:38 -05:00
|
|
|
.any(|attr| attr::find_repr_attrs(&cx.sess.parse_sess, attr).contains(&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;
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:51:36 +01:00
|
|
|
match it.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
ast::ItemKind::TyAlias(..)
|
|
|
|
| ast::ItemKind::Enum(..)
|
|
|
|
| ast::ItemKind::Struct(..)
|
|
|
|
| ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident),
|
2019-01-04 15:59:07 -05:00
|
|
|
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident),
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => (),
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-06 09:51:23 -05:00
|
|
|
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
|
|
|
|
if let ast::AssocItemKind::TyAlias(..) = it.kind {
|
|
|
|
self.check_case(cx, "associated type", &it.ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-24 13:54:40 -03:00
|
|
|
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
|
2019-08-13 21:40:21 -03:00
|
|
|
self.check_case(cx, "variant", &v.ident);
|
2017-02-14 19:46:48 +09:00
|
|
|
}
|
|
|
|
|
2019-02-08 20:35:41 +09:00
|
|
|
fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) {
|
2018-12-16 22:21:47 -05:00
|
|
|
if let ast::GenericParamKind::Type { .. } = param.kind {
|
2019-01-04 15:59:07 -05:00
|
|
|
self.check_case(cx, "type parameter", ¶m.ident);
|
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
|
|
|
}
|
|
|
|
|
2019-04-03 16:05:40 +02:00
|
|
|
declare_lint_pass!(NonSnakeCase => [NON_SNAKE_CASE]);
|
2015-09-14 22:36:39 -04:00
|
|
|
|
|
|
|
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.
|
2019-02-08 20:35:41 +09:00
|
|
|
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);
|
2020-01-31 22:24:57 +10:00
|
|
|
cx.struct_span_lint(NON_SNAKE_CASE, ident.span, |lint| {
|
|
|
|
let mut err = lint.build(&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(
|
|
|
|
ident.span,
|
|
|
|
"convert the identifier to snake case",
|
|
|
|
sc,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.help(&format!("convert the identifier to snake case: `{}`", sc));
|
|
|
|
}
|
2019-01-04 10:19:52 -05:00
|
|
|
|
2020-01-31 22:24:57 +10:00
|
|
|
err.emit();
|
|
|
|
});
|
2019-01-04 10:19:52 -05:00
|
|
|
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSnakeCase {
|
2019-12-01 12:49:54 +01:00
|
|
|
fn check_mod(
|
|
|
|
&mut self,
|
|
|
|
cx: &LateContext<'_, '_>,
|
|
|
|
_: &'tcx hir::Mod<'tcx>,
|
|
|
|
_: Span,
|
|
|
|
id: hir::HirId,
|
|
|
|
) {
|
2019-03-03 18:47:54 +01:00
|
|
|
if id != hir::CRATE_HIR_ID {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-06-14 18:58:55 +02:00
|
|
|
attr::find_by_name(&cx.tcx.hir().attrs(hir::CRATE_HIR_ID), sym::crate_name)
|
2019-01-04 10:19:52 -05:00
|
|
|
.and_then(|attr| attr.meta())
|
|
|
|
.and_then(|meta| {
|
|
|
|
meta.name_value_literal().and_then(|lit| {
|
2019-09-26 16:56:53 +01:00
|
|
|
if let ast::LitKind::Str(name, ..) = lit.kind {
|
2019-01-04 10:19:52 -05:00
|
|
|
// Discard the double quotes surrounding the literal.
|
2019-12-22 17:42:04 -05:00
|
|
|
let sp = cx
|
|
|
|
.sess()
|
|
|
|
.source_map()
|
|
|
|
.span_to_snippet(lit.span)
|
2019-01-04 10:19:52 -05:00
|
|
|
.ok()
|
|
|
|
.and_then(|snippet| {
|
|
|
|
let left = snippet.find('"')?;
|
2019-12-22 17:42:04 -05:00
|
|
|
let right =
|
|
|
|
snippet.rfind('"').map(|pos| snippet.len() - pos)?;
|
2019-01-04 10:19:52 -05:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 16:08:58 +01: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,
|
2019-02-08 20:35:41 +09:00
|
|
|
cx: &LateContext<'_, '_>,
|
|
|
|
fk: FnKind<'_>,
|
2019-12-01 16:08:58 +01:00
|
|
|
_: &hir::FnDecl<'_>,
|
2019-11-29 11:09:23 +01:00
|
|
|
_: &hir::Body<'_>,
|
2019-01-04 10:19:52 -05:00
|
|
|
_: Span,
|
2019-02-06 14:16:11 +01:00
|
|
|
id: hir::HirId,
|
2019-01-04 10:19:52 -05:00
|
|
|
) {
|
|
|
|
match &fk {
|
2019-12-22 17:42:04 -05:00
|
|
|
FnKind::Method(ident, ..) => match method_context(cx, id) {
|
|
|
|
MethodLateContext::PlainImpl => {
|
|
|
|
self.check_snake_case(cx, "method", ident);
|
2016-10-09 09:38:07 +05:30
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
MethodLateContext::TraitAutoImpl => {
|
|
|
|
self.check_snake_case(cx, "trait method", ident);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
},
|
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-05-08 13:21:18 +10:00
|
|
|
if header.abi != Abi::Rust && attr::contains_name(attrs, sym::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:28:50 +01:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'_, '_>, it: &hir::Item<'_>) {
|
2019-09-26 17:51:36 +01:00
|
|
|
if let hir::ItemKind::Mod(_) = it.kind {
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "module", &it.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'_, '_>, item: &hir::TraitItem<'_>) {
|
2019-11-30 15:28:32 +01:00
|
|
|
if let hir::TraitItemKind::Method(_, hir::TraitMethod::Required(pnames)) = item.kind {
|
2019-01-04 10:19:52 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:08:22 +01:00
|
|
|
fn check_pat(&mut self, cx: &LateContext<'_, '_>, p: &hir::Pat<'_>) {
|
2019-11-23 14:52:40 +10:00
|
|
|
if let &PatKind::Binding(_, hid, ident, _) = &p.kind {
|
|
|
|
if let hir::Node::Pat(parent_pat) = cx.tcx.hir().get(cx.tcx.hir().get_parent_node(hid))
|
|
|
|
{
|
|
|
|
if let PatKind::Struct(_, field_pats, _) = &parent_pat.kind {
|
|
|
|
for field in field_pats.iter() {
|
|
|
|
if field.ident != ident {
|
|
|
|
// Only check if a new name has been introduced, to avoid warning
|
|
|
|
// on both the struct definition and this pattern.
|
|
|
|
self.check_snake_case(cx, "variable", &ident);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 10:19:52 -05:00
|
|
|
self.check_snake_case(cx, "variable", &ident);
|
2016-11-25 13:21:19 +02:00
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn check_struct_def(&mut self, cx: &LateContext<'_, '_>, s: &hir::VariantData<'_>) {
|
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"
|
|
|
|
}
|
|
|
|
|
2019-04-03 16:05:40 +02:00
|
|
|
declare_lint_pass!(NonUpperCaseGlobals => [NON_UPPER_CASE_GLOBALS]);
|
2015-09-14 22:36:39 -04:00
|
|
|
|
|
|
|
impl NonUpperCaseGlobals {
|
2019-02-08 20:35:41 +09:00
|
|
|
fn check_upper_case(cx: &LateContext<'_, '_>, sort: &str, ident: &Ident) {
|
2019-01-04 15:00:15 -05:00
|
|
|
let name = &ident.name.as_str();
|
|
|
|
|
|
|
|
if name.chars().any(|c| c.is_lowercase()) {
|
|
|
|
let uc = NonSnakeCase::to_snake_case(&name).to_uppercase();
|
|
|
|
|
2020-01-31 22:24:57 +10:00
|
|
|
cx.struct_span_lint(NON_UPPER_CASE_GLOBALS, ident.span, |lint| {
|
|
|
|
lint.build(&format!("{} `{}` should have an upper case name", sort, name))
|
2019-01-25 16:03:27 -05:00
|
|
|
.span_suggestion(
|
2019-01-04 15:00:15 -05:00
|
|
|
ident.span,
|
|
|
|
"convert the identifier to upper case",
|
|
|
|
uc,
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
)
|
|
|
|
.emit();
|
2020-01-31 22:24:57 +10:00
|
|
|
})
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonUpperCaseGlobals {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn check_item(&mut self, cx: &LateContext<'_, '_>, it: &hir::Item<'_>) {
|
2019-09-26 17:51:36 +01:00
|
|
|
match it.kind {
|
2019-05-08 13:21:18 +10:00
|
|
|
hir::ItemKind::Static(..) if !attr::contains_name(&it.attrs, sym::no_mangle) => {
|
2019-01-04 15:00:15 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn check_trait_item(&mut self, cx: &LateContext<'_, '_>, ti: &hir::TraitItem<'_>) {
|
2019-09-26 17:07:54 +01:00
|
|
|
if let hir::TraitItemKind::Const(..) = ti.kind {
|
2019-01-04 15:00:15 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ti.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn check_impl_item(&mut self, cx: &LateContext<'_, '_>, ii: &hir::ImplItem<'_>) {
|
2019-09-26 16:38:13 +01:00
|
|
|
if let hir::ImplItemKind::Const(..) = ii.kind {
|
2019-01-04 15:00:15 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 15:08:22 +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)
|
2019-09-26 16:18:31 +01:00
|
|
|
if let PatKind::Path(hir::QPath::Resolved(None, ref path)) = p.kind {
|
2019-04-20 19:36:05 +03:00
|
|
|
if let Res::Def(DefKind::Const, _) = path.res {
|
2017-01-09 17:46:11 +02:00
|
|
|
if path.segments.len() == 1 {
|
2019-01-04 15:00:15 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(
|
|
|
|
cx,
|
|
|
|
"constant in pattern",
|
2019-12-22 17:42:04 -05:00
|
|
|
&path.segments[0].ident,
|
2019-01-04 15:00:15 -05:00
|
|
|
);
|
2016-03-06 15:54:44 +03:00
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-15 22:26:29 +00:00
|
|
|
|
2019-12-01 16:08:58 +01:00
|
|
|
fn check_generic_param(&mut self, cx: &LateContext<'_, '_>, param: &hir::GenericParam<'_>) {
|
2019-02-15 22:26:29 +00:00
|
|
|
if let GenericParamKind::Const { .. } = param.kind {
|
2019-12-22 17:42:04 -05:00
|
|
|
NonUpperCaseGlobals::check_upper_case(cx, "const parameter", ¶m.name.ident());
|
2019-02-15 22:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-14 22:36:39 -04:00
|
|
|
}
|
2019-02-12 12:35:38 -05:00
|
|
|
|
|
|
|
#[cfg(test)]
|
2019-06-06 02:39:56 +09:00
|
|
|
mod tests;
|