use rustc_hir::def::{DefKind, Res};
use rustc_hir::intravisit::FnKind;
use rustc_hir::{GenericParamKind, PatKind};
use rustc_middle::ty;
use rustc_session::config::CrateType;
use rustc_session::{declare_lint, declare_lint_pass};
use rustc_span::def_id::LocalDefId;
use rustc_span::symbol::{sym, Ident};
use rustc_span::{BytePos, Span};
use rustc_target::spec::abi::Abi;
use {rustc_ast as ast, rustc_attr as attr, rustc_hir as hir};
use crate::lints::{
NonCamelCaseType, NonCamelCaseTypeSub, NonSnakeCaseDiag, NonSnakeCaseDiagSub,
NonUpperCaseGlobal, NonUpperCaseGlobalSub,
};
use crate::{EarlyContext, EarlyLintPass, LateContext, LateLintPass, LintContext};
#[derive(PartialEq)]
pub(crate) enum MethodLateContext {
TraitAutoImpl,
TraitImpl,
PlainImpl,
}
pub(crate) fn method_context(cx: &LateContext<'_>, id: LocalDefId) -> MethodLateContext {
let item = cx.tcx.associated_item(id);
match item.container {
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
Some(_) => MethodLateContext::TraitImpl,
None => MethodLateContext::PlainImpl,
},
}
}
fn assoc_item_in_trait_impl(cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) -> bool {
let item = cx.tcx.associated_item(ii.owner_id);
item.trait_item_def_id.is_some()
}
declare_lint! {
pub NON_CAMEL_CASE_TYPES,
Warn,
"types, variants, traits and type parameters should have camel case names"
}
declare_lint_pass!(NonCamelCaseTypes => [NON_CAMEL_CASE_TYPES]);
fn char_has_case(c: char) -> bool {
let mut l = c.to_lowercase();
let mut u = c.to_uppercase();
while let Some(l) = l.next() {
match u.next() {
Some(u) if l != u => return true,
_ => {}
}
}
u.next().is_some()
}
fn is_camel_case(name: &str) -> bool {
let name = name.trim_matches('_');
if name.is_empty() {
return true;
}
!name.chars().next().unwrap().is_lowercase()
&& !name.contains("__")
&& !name.chars().collect::<Vec<_>>().array_windows().any(|&[fst, snd]| {
char_has_case(fst) && snd == '_' || char_has_case(snd) && fst == '_'
})
}
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() {
if prev_is_lower_case && c.is_uppercase() {
new_word = true;
}
if new_word {
camel_cased_component.extend(c.to_uppercase());
} else {
camel_cased_component.extend(c.to_lowercase());
}
prev_is_lower_case = c.is_lowercase();
new_word = false;
}
camel_cased_component
})
.fold((String::new(), None), |(acc, prev): (String, Option<String>), next| {
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))
})
.0
}
impl NonCamelCaseTypes {
fn check_case(&self, cx: &EarlyContext<'_>, sort: &str, ident: &Ident) {
let name = ident.name.as_str();
if !is_camel_case(name) {
let cc = to_camel_case(name);
let sub = if *name != cc {
NonCamelCaseTypeSub::Suggestion { span: ident.span, replace: cc }
} else {
NonCamelCaseTypeSub::Label { span: ident.span }
};
cx.emit_span_lint(
NON_CAMEL_CASE_TYPES,
ident.span,
NonCamelCaseType { sort, name, sub },
);
}
}
}
impl EarlyLintPass for NonCamelCaseTypes {
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &ast::Item) {
let has_repr_c = it
.attrs
.iter()
.any(|attr| attr::find_repr_attrs(cx.sess(), attr).contains(&attr::ReprC));
if has_repr_c {
return;
}
match &it.kind {
ast::ItemKind::TyAlias(..)
| ast::ItemKind::Enum(..)
| ast::ItemKind::Struct(..)
| ast::ItemKind::Union(..) => self.check_case(cx, "type", &it.ident),
ast::ItemKind::Trait(..) => self.check_case(cx, "trait", &it.ident),
ast::ItemKind::TraitAlias(..) => self.check_case(cx, "trait alias", &it.ident),
ast::ItemKind::Impl(box ast::Impl { of_trait: None, items, .. }) => {
for it in items {
if let ast::AssocItemKind::Type(..) = it.kind {
self.check_case(cx, "associated type", &it.ident);
}
}
}
_ => (),
}
}
fn check_trait_item(&mut self, cx: &EarlyContext<'_>, it: &ast::AssocItem) {
if let ast::AssocItemKind::Type(..) = it.kind {
self.check_case(cx, "associated type", &it.ident);
}
}
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &ast::Variant) {
self.check_case(cx, "variant", &v.ident);
}
fn check_generic_param(&mut self, cx: &EarlyContext<'_>, param: &ast::GenericParam) {
if let ast::GenericParamKind::Type { .. } = param.kind {
self.check_case(cx, "type parameter", ¶m.ident);
}
}
}
declare_lint! {
pub NON_SNAKE_CASE,
Warn,
"variables, methods, functions, lifetime parameters and modules should have snake case names"
}
declare_lint_pass!(NonSnakeCase => [NON_SNAKE_CASE]);
impl NonSnakeCase {
fn to_snake_case(mut str: &str) -> String {
let mut words = vec![];
str = str.trim_start_matches(|c: char| {
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() {
if !buf.is_empty() && buf != "'" && ch.is_uppercase() && !last_upper {
words.push(buf);
buf = String::new();
}
last_upper = ch.is_uppercase();
buf.extend(ch.to_lowercase());
}
words.push(buf);
}
words.join("_")
}
fn check_snake_case(&self, cx: &LateContext<'_>, sort: &str, ident: &Ident) {
fn is_snake_case(ident: &str) -> bool {
if ident.is_empty() {
return true;
}
let ident = ident.trim_start_matches('\'');
let ident = ident.trim_matches('_');
let mut allow_underscore = true;
ident.chars().all(|c| {
allow_underscore = match c {
'_' if !allow_underscore => return false,
'_' => false,
c if !c.is_uppercase() => true,
_ => return false,
};
true
})
}
let name = ident.name.as_str();
if !is_snake_case(name) {
let span = ident.span;
let sc = NonSnakeCase::to_snake_case(name);
let sub = if name != sc {
if !span.is_dummy() {
let sc_ident = Ident::from_str_and_span(&sc, span);
if sc_ident.is_reserved() {
if sc_ident.name.can_be_raw() {
NonSnakeCaseDiagSub::RenameOrConvertSuggestion {
span,
suggestion: sc_ident,
}
} else {
NonSnakeCaseDiagSub::SuggestionAndNote { span }
}
} else {
NonSnakeCaseDiagSub::ConvertSuggestion { span, suggestion: sc.clone() }
}
} else {
NonSnakeCaseDiagSub::Help
}
} else {
NonSnakeCaseDiagSub::Label { span }
};
cx.emit_span_lint(NON_SNAKE_CASE, span, NonSnakeCaseDiag { sort, name, sc, sub });
}
}
}
impl<'tcx> LateLintPass<'tcx> for NonSnakeCase {
fn check_mod(&mut self, cx: &LateContext<'_>, _: &'tcx hir::Mod<'tcx>, id: hir::HirId) {
if id != hir::CRATE_HIR_ID {
return;
}
if cx.tcx.crate_types().iter().all(|&crate_type| crate_type == CrateType::Executable) {
return;
}
let crate_ident = if let Some(name) = &cx.tcx.sess.opts.crate_name {
Some(Ident::from_str(name))
} else {
attr::find_by_name(cx.tcx.hir().attrs(hir::CRATE_HIR_ID), sym::crate_name)
.and_then(|attr| attr.meta())
.and_then(|meta| {
meta.name_value_literal().and_then(|lit| {
if let ast::LitKind::Str(name, ..) = lit.kind {
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(lit.span);
Some(Ident::new(name, sp))
} else {
None
}
})
})
};
if let Some(ident) = &crate_ident {
self.check_snake_case(cx, "crate", ident);
}
}
fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) {
if let GenericParamKind::Lifetime { .. } = param.kind {
self.check_snake_case(cx, "lifetime", ¶m.name.ident());
}
}
fn check_fn(
&mut self,
cx: &LateContext<'_>,
fk: FnKind<'_>,
_: &hir::FnDecl<'_>,
_: &hir::Body<'_>,
_: Span,
id: LocalDefId,
) {
match &fk {
FnKind::Method(ident, sig, ..) => match method_context(cx, id) {
MethodLateContext::PlainImpl => {
if sig.header.abi != Abi::Rust && cx.tcx.has_attr(id, sym::no_mangle) {
return;
}
self.check_snake_case(cx, "method", ident);
}
MethodLateContext::TraitAutoImpl => {
self.check_snake_case(cx, "trait method", ident);
}
_ => (),
},
FnKind::ItemFn(ident, _, header) => {
if header.abi != Abi::Rust && cx.tcx.has_attr(id, sym::no_mangle) {
return;
}
self.check_snake_case(cx, "function", ident);
}
FnKind::Closure => (),
}
}
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
if let hir::ItemKind::Mod(_) = it.kind {
self.check_snake_case(cx, "module", &it.ident);
}
}
fn check_trait_item(&mut self, cx: &LateContext<'_>, item: &hir::TraitItem<'_>) {
if let hir::TraitItemKind::Fn(_, hir::TraitFn::Required(pnames)) = item.kind {
self.check_snake_case(cx, "trait method", &item.ident);
for param_name in pnames {
self.check_snake_case(cx, "variable", param_name);
}
}
}
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
if let PatKind::Binding(_, hid, ident, _) = p.kind {
if let hir::Node::PatField(field) = cx.tcx.parent_hir_node(hid) {
if !field.is_shorthand {
self.check_snake_case(cx, "variable", &ident);
}
return;
}
self.check_snake_case(cx, "variable", &ident);
}
}
fn check_struct_def(&mut self, cx: &LateContext<'_>, s: &hir::VariantData<'_>) {
for sf in s.fields() {
self.check_snake_case(cx, "structure field", &sf.ident);
}
}
}
declare_lint! {
pub NON_UPPER_CASE_GLOBALS,
Warn,
"static constants should have uppercase identifiers"
}
declare_lint_pass!(NonUpperCaseGlobals => [NON_UPPER_CASE_GLOBALS]);
impl NonUpperCaseGlobals {
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 sub = if *name != uc {
NonUpperCaseGlobalSub::Suggestion { span: ident.span, replace: uc }
} else {
NonUpperCaseGlobalSub::Label { span: ident.span }
};
cx.emit_span_lint(
NON_UPPER_CASE_GLOBALS,
ident.span,
NonUpperCaseGlobal { sort, name, sub },
);
}
}
}
impl<'tcx> LateLintPass<'tcx> for NonUpperCaseGlobals {
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
let attrs = cx.tcx.hir().attrs(it.hir_id());
match it.kind {
hir::ItemKind::Static(..) if !attr::contains_name(attrs, sym::no_mangle) => {
NonUpperCaseGlobals::check_upper_case(cx, "static variable", &it.ident);
}
hir::ItemKind::Const(..) => {
NonUpperCaseGlobals::check_upper_case(cx, "constant", &it.ident);
}
_ => {}
}
}
fn check_trait_item(&mut self, cx: &LateContext<'_>, ti: &hir::TraitItem<'_>) {
if let hir::TraitItemKind::Const(..) = ti.kind {
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ti.ident);
}
}
fn check_impl_item(&mut self, cx: &LateContext<'_>, ii: &hir::ImplItem<'_>) {
if let hir::ImplItemKind::Const(..) = ii.kind
&& !assoc_item_in_trait_impl(cx, ii)
{
NonUpperCaseGlobals::check_upper_case(cx, "associated constant", &ii.ident);
}
}
fn check_pat(&mut self, cx: &LateContext<'_>, p: &hir::Pat<'_>) {
if let PatKind::Path(hir::QPath::Resolved(None, path)) = p.kind {
if let Res::Def(DefKind::Const, _) = path.res {
if let [segment] = path.segments {
NonUpperCaseGlobals::check_upper_case(
cx,
"constant in pattern",
&segment.ident,
);
}
}
}
}
fn check_generic_param(&mut self, cx: &LateContext<'_>, param: &hir::GenericParam<'_>) {
if let GenericParamKind::Const { is_host_effect, .. } = param.kind {
if is_host_effect {
return;
}
NonUpperCaseGlobals::check_upper_case(cx, "const parameter", ¶m.name.ident());
}
}
}
#[cfg(test)]
mod tests;