2016-02-01 05:47:31 -06:00
|
|
|
//! lint on enum variants that are prefixed or suffixed by the same characters
|
|
|
|
|
2019-08-19 11:30:32 -05:00
|
|
|
use crate::utils::{camel_case, is_present_in_source};
|
2018-11-27 14:14:15 -06:00
|
|
|
use crate::utils::{span_help_and_lint, span_lint};
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc::impl_lint_pass;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::lint::{EarlyContext, EarlyLintPass, Lint, LintArray, LintPass};
|
2019-12-03 17:16:03 -06:00
|
|
|
use rustc_session::declare_tool_lint;
|
2018-12-29 09:04:45 -06:00
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::source_map::Span;
|
2019-09-05 01:56:10 -05:00
|
|
|
use syntax::symbol::Symbol;
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Detects enumeration variants that are prefixed or suffixed
|
|
|
|
/// by the same characters.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Enumeration variant names should specify their variant,
|
|
|
|
/// not repeat the enumeration name.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// enum Cake {
|
|
|
|
/// BlackForestCake,
|
|
|
|
/// HummingbirdCake,
|
|
|
|
/// BattenbergCake,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-08-06 03:18:36 -05:00
|
|
|
pub ENUM_VARIANT_NAMES,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-08-06 03:18:36 -05:00
|
|
|
"enums where all variants share a prefix/postfix"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Detects enumeration variants that are prefixed or suffixed
|
|
|
|
/// by the same characters.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Enumeration variant names should specify their variant,
|
|
|
|
/// not repeat the enumeration name.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// enum Cake {
|
|
|
|
/// BlackForestCake,
|
|
|
|
/// HummingbirdCake,
|
|
|
|
/// BattenbergCake,
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-12-21 06:30:57 -06:00
|
|
|
pub PUB_ENUM_VARIANT_NAMES,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2016-12-21 06:30:57 -06:00
|
|
|
"enums where all variants share a prefix/postfix"
|
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Detects type names that are prefixed or suffixed by the
|
|
|
|
/// containing module's name.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** It requires the user to type the module name twice.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// mod cake {
|
|
|
|
/// struct BlackForestCake;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2018-12-17 07:29:19 -06:00
|
|
|
pub MODULE_NAME_REPETITIONS,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2016-08-06 03:18:36 -05:00
|
|
|
"type names prefixed/postfixed with their containing module's name"
|
2016-06-16 11:37:56 -05:00
|
|
|
}
|
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 10:50:33 -06:00
|
|
|
/// **What it does:** Checks for modules that have the same name as their
|
|
|
|
/// parent module
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** A typical beginner mistake is to have `mod foo;` and
|
|
|
|
/// again `mod foo { ..
|
|
|
|
/// }` in `foo.rs`.
|
|
|
|
/// The expectation is that items inside the inner `mod foo { .. }` are then
|
|
|
|
/// available
|
|
|
|
/// through `foo::x`, but they are only available through
|
|
|
|
/// `foo::foo::x`.
|
|
|
|
/// If this is done on purpose, it would be better to choose a more
|
|
|
|
/// representative module name.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-05 16:23:50 -06:00
|
|
|
/// ```ignore
|
2019-03-05 10:50:33 -06:00
|
|
|
/// // lib.rs
|
|
|
|
/// mod foo;
|
|
|
|
/// // foo.rs
|
|
|
|
/// mod foo {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
2016-09-12 03:30:42 -05:00
|
|
|
pub MODULE_INCEPTION,
|
2018-03-28 08:24:26 -05:00
|
|
|
style,
|
2016-09-12 03:30:42 -05:00
|
|
|
"modules that have the same name as their parent module"
|
|
|
|
}
|
|
|
|
|
2016-06-08 10:43:27 -05:00
|
|
|
pub struct EnumVariantNames {
|
2019-09-05 01:56:10 -05:00
|
|
|
modules: Vec<(Symbol, String)>,
|
2016-08-06 13:59:27 -05:00
|
|
|
threshold: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumVariantNames {
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2017-08-21 06:32:12 -05:00
|
|
|
pub fn new(threshold: u64) -> Self {
|
|
|
|
Self {
|
2016-12-20 11:21:30 -06:00
|
|
|
modules: Vec::new(),
|
2018-03-15 10:07:15 -05:00
|
|
|
threshold,
|
2016-12-20 11:21:30 -06:00
|
|
|
}
|
2016-08-06 13:59:27 -05:00
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
impl_lint_pass!(EnumVariantNames => [
|
|
|
|
ENUM_VARIANT_NAMES,
|
|
|
|
PUB_ENUM_VARIANT_NAMES,
|
|
|
|
MODULE_NAME_REPETITIONS,
|
|
|
|
MODULE_INCEPTION
|
|
|
|
]);
|
2016-02-01 05:47:31 -06:00
|
|
|
|
2016-06-08 08:32:30 -05:00
|
|
|
/// Returns the number of chars that match from the start
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2016-02-15 09:59:56 -06:00
|
|
|
fn partial_match(pre: &str, name: &str) -> usize {
|
|
|
|
let mut name_iter = name.chars();
|
|
|
|
let _ = name_iter.next_back(); // make sure the name is never fully matched
|
2018-11-27 14:14:15 -06:00
|
|
|
pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
|
2016-02-15 09:59:56 -06:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:32:30 -05:00
|
|
|
/// Returns the number of chars that match from the end
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2016-02-15 09:59:56 -06:00
|
|
|
fn partial_rmatch(post: &str, name: &str) -> usize {
|
|
|
|
let mut name_iter = name.chars();
|
|
|
|
let _ = name_iter.next(); // make sure the name is never fully matched
|
2017-08-09 02:30:56 -05:00
|
|
|
post.chars()
|
|
|
|
.rev()
|
|
|
|
.zip(name_iter.rev())
|
|
|
|
.take_while(|&(l, r)| l == r)
|
|
|
|
.count()
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
|
2016-12-21 05:14:54 -06:00
|
|
|
fn check_variant(
|
2018-07-23 06:01:12 -05:00
|
|
|
cx: &EarlyContext<'_>,
|
2016-12-21 05:14:54 -06:00
|
|
|
threshold: u64,
|
|
|
|
def: &EnumDef,
|
|
|
|
item_name: &str,
|
|
|
|
item_name_chars: usize,
|
2016-12-21 06:30:57 -06:00
|
|
|
span: Span,
|
2017-08-09 02:30:56 -05:00
|
|
|
lint: &'static Lint,
|
2016-12-21 05:14:54 -06:00
|
|
|
) {
|
2016-08-06 13:59:27 -05:00
|
|
|
if (def.variants.len() as u64) < threshold {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
for var in &def.variants {
|
2019-09-05 01:56:10 -05:00
|
|
|
let name = var.ident.name.as_str();
|
2017-11-04 14:55:56 -05:00
|
|
|
if partial_match(item_name, &name) == item_name_chars
|
2018-11-27 14:14:15 -06:00
|
|
|
&& name.chars().nth(item_name_chars).map_or(false, |c| !c.is_lowercase())
|
2019-08-06 13:45:36 -05:00
|
|
|
&& name.chars().nth(item_name_chars + 1).map_or(false, |c| !c.is_numeric())
|
2017-11-04 14:55:56 -05:00
|
|
|
{
|
2016-12-21 06:30:57 -06:00
|
|
|
span_lint(cx, lint, var.span, "Variant name starts with the enum's name");
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
|
|
|
if partial_rmatch(item_name, &name) == item_name_chars {
|
2016-12-21 06:30:57 -06:00
|
|
|
span_lint(cx, lint, var.span, "Variant name ends with the enum's name");
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 01:56:10 -05:00
|
|
|
let first = &def.variants[0].ident.name.as_str();
|
2018-10-11 17:36:40 -05:00
|
|
|
let mut pre = &first[..camel_case::until(&*first)];
|
|
|
|
let mut post = &first[camel_case::from(&*first)..];
|
2016-06-08 10:43:27 -05:00
|
|
|
for var in &def.variants {
|
2019-09-05 01:56:10 -05:00
|
|
|
let name = var.ident.name.as_str();
|
2016-06-08 10:43:27 -05:00
|
|
|
|
|
|
|
let pre_match = partial_match(pre, &name);
|
|
|
|
pre = &pre[..pre_match];
|
2018-10-11 17:36:40 -05:00
|
|
|
let pre_camel = camel_case::until(pre);
|
2016-06-08 10:43:27 -05:00
|
|
|
pre = &pre[..pre_camel];
|
|
|
|
while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
|
2019-08-06 13:45:36 -05:00
|
|
|
if next.is_numeric() {
|
|
|
|
return;
|
|
|
|
}
|
2016-06-08 10:43:27 -05:00
|
|
|
if next.is_lowercase() {
|
|
|
|
let last = pre.len() - last.len_utf8();
|
2018-10-11 17:36:40 -05:00
|
|
|
let last_camel = camel_case::until(&pre[..last]);
|
2016-06-08 10:43:27 -05:00
|
|
|
pre = &pre[..last_camel];
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let post_match = partial_rmatch(post, &name);
|
|
|
|
let post_end = post.len() - post_match;
|
|
|
|
post = &post[post_end..];
|
2018-10-11 17:36:40 -05:00
|
|
|
let post_camel = camel_case::from(post);
|
2016-06-08 10:43:27 -05:00
|
|
|
post = &post[post_camel..];
|
|
|
|
}
|
|
|
|
let (what, value) = match (pre.is_empty(), post.is_empty()) {
|
|
|
|
(true, true) => return,
|
|
|
|
(false, _) => ("pre", pre),
|
|
|
|
(true, false) => ("post", post),
|
|
|
|
};
|
2017-08-09 02:30:56 -05:00
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
lint,
|
|
|
|
span,
|
|
|
|
&format!("All variants have the same {}fix: `{}`", what, value),
|
|
|
|
&format!(
|
|
|
|
"remove the {}fixes and use full paths to \
|
2017-09-05 04:33:04 -05:00
|
|
|
the variants instead of glob imports",
|
2017-08-09 02:30:56 -05:00
|
|
|
what
|
|
|
|
),
|
|
|
|
);
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
|
|
|
|
2019-09-18 01:37:41 -05:00
|
|
|
#[must_use]
|
2016-06-08 10:43:27 -05:00
|
|
|
fn to_camel_case(item_name: &str) -> String {
|
|
|
|
let mut s = String::new();
|
|
|
|
let mut up = true;
|
|
|
|
for c in item_name.chars() {
|
|
|
|
if c.is_uppercase() {
|
|
|
|
// we only turn snake case text into CamelCase
|
|
|
|
return item_name.to_string();
|
|
|
|
}
|
|
|
|
if c == '_' {
|
|
|
|
up = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if up {
|
|
|
|
up = false;
|
|
|
|
s.extend(c.to_uppercase());
|
|
|
|
} else {
|
|
|
|
s.push(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s
|
|
|
|
}
|
|
|
|
|
2016-02-01 05:47:31 -06:00
|
|
|
impl EarlyLintPass for EnumVariantNames {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_item_post(&mut self, _cx: &EarlyContext<'_>, _item: &Item) {
|
2016-06-08 10:43:27 -05:00
|
|
|
let last = self.modules.pop();
|
|
|
|
assert!(last.is_some());
|
|
|
|
}
|
|
|
|
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2018-07-23 06:01:12 -05:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext<'_>, item: &Item) {
|
2019-09-05 01:56:10 -05:00
|
|
|
let item_name = item.ident.name.as_str();
|
2016-06-08 08:32:30 -05:00
|
|
|
let item_name_chars = item_name.chars().count();
|
2016-06-08 10:43:27 -05:00
|
|
|
let item_camel = to_camel_case(&item_name);
|
2019-08-19 11:30:32 -05:00
|
|
|
if !item.span.from_expansion() && is_present_in_source(cx, item.span) {
|
2016-09-12 03:30:42 -05:00
|
|
|
if let Some(&(ref mod_name, ref mod_camel)) = self.modules.last() {
|
2016-06-10 09:30:39 -05:00
|
|
|
// constants don't have surrounding modules
|
|
|
|
if !mod_camel.is_empty() {
|
2019-09-05 01:56:10 -05:00
|
|
|
if mod_name == &item.ident.name {
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Mod(..) = item.kind {
|
2017-08-09 02:30:56 -05:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
MODULE_INCEPTION,
|
|
|
|
item.span,
|
|
|
|
"module has the same name as its containing module",
|
|
|
|
);
|
2016-09-13 03:19:55 -05:00
|
|
|
}
|
2016-09-12 03:30:42 -05:00
|
|
|
}
|
2018-07-14 17:00:27 -05:00
|
|
|
if item.vis.node.is_pub() {
|
2016-09-13 03:19:55 -05:00
|
|
|
let matching = partial_match(mod_camel, &item_camel);
|
|
|
|
let rmatching = partial_rmatch(mod_camel, &item_camel);
|
|
|
|
let nchars = mod_camel.chars().count();
|
2017-11-26 11:36:12 -06:00
|
|
|
|
2018-11-27 14:14:15 -06:00
|
|
|
let is_word_beginning = |c: char| c == '_' || c.is_uppercase() || c.is_numeric();
|
2017-11-26 11:36:12 -06:00
|
|
|
|
2016-09-13 03:19:55 -05:00
|
|
|
if matching == nchars {
|
2017-11-26 11:36:12 -06:00
|
|
|
match item_camel.chars().nth(nchars) {
|
2018-11-27 14:14:15 -06:00
|
|
|
Some(c) if is_word_beginning(c) => span_lint(
|
|
|
|
cx,
|
2018-12-17 07:29:19 -06:00
|
|
|
MODULE_NAME_REPETITIONS,
|
2018-11-27 14:14:15 -06:00
|
|
|
item.span,
|
|
|
|
"item name starts with its containing module's name",
|
|
|
|
),
|
|
|
|
_ => (),
|
2017-11-26 11:36:12 -06:00
|
|
|
}
|
2016-09-13 03:19:55 -05:00
|
|
|
}
|
|
|
|
if rmatching == nchars {
|
2018-11-27 14:14:15 -06:00
|
|
|
span_lint(
|
|
|
|
cx,
|
2018-12-17 07:29:19 -06:00
|
|
|
MODULE_NAME_REPETITIONS,
|
2018-11-27 14:14:15 -06:00
|
|
|
item.span,
|
|
|
|
"item name ends with its containing module's name",
|
|
|
|
);
|
2016-09-13 03:19:55 -05:00
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.kind {
|
2018-02-23 10:59:42 -06:00
|
|
|
let lint = match item.vis.node {
|
|
|
|
VisibilityKind::Public => PUB_ENUM_VARIANT_NAMES,
|
2016-12-21 06:30:57 -06:00
|
|
|
_ => ENUM_VARIANT_NAMES,
|
|
|
|
};
|
|
|
|
check_variant(cx, self.threshold, def, &item_name, item_name_chars, item.span, lint);
|
2016-06-08 10:43:27 -05:00
|
|
|
}
|
2019-09-05 01:56:10 -05:00
|
|
|
self.modules.push((item.ident.name, item_camel));
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|