2016-02-01 05:47:31 -06:00
|
|
|
//! lint on enum variants that are prefixed or suffixed by the same characters
|
|
|
|
|
|
|
|
use rustc::lint::*;
|
|
|
|
use syntax::ast::*;
|
|
|
|
use syntax::parse::token::InternedString;
|
2016-06-08 08:32:30 -05:00
|
|
|
use utils::{span_help_and_lint, span_lint};
|
2016-02-15 09:59:56 -06:00
|
|
|
use utils::{camel_case_from, camel_case_until};
|
2016-02-01 05:47:31 -06:00
|
|
|
|
|
|
|
/// **What it does:** Warns on enum variants that are prefixed or suffixed by the same characters
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Enum variant names should specify their variant, not the enum, too.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None
|
|
|
|
///
|
|
|
|
/// **Example:** enum Cake { BlackForestCake, HummingbirdCake }
|
2016-02-05 17:13:29 -06:00
|
|
|
declare_lint! {
|
|
|
|
pub ENUM_VARIANT_NAMES, Warn,
|
|
|
|
"finds enums where all variants share a prefix/postfix"
|
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
|
|
|
|
pub struct EnumVariantNames;
|
|
|
|
|
|
|
|
impl LintPass for EnumVariantNames {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ENUM_VARIANT_NAMES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn var2str(var: &Variant) -> InternedString {
|
|
|
|
var.node.name.name.as_str()
|
|
|
|
}
|
|
|
|
|
2016-06-08 08:32:30 -05:00
|
|
|
/// Returns the number of chars that match from the start
|
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
|
|
|
|
pre.chars().zip(name_iter).take_while(|&(l, r)| l == r).count()
|
|
|
|
}
|
|
|
|
|
2016-06-08 08:32:30 -05:00
|
|
|
/// Returns the number of chars that match from the end
|
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
|
|
|
|
post.chars().rev().zip(name_iter.rev()).take_while(|&(l, r)| l == r).count()
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for EnumVariantNames {
|
2016-02-15 09:59:56 -06:00
|
|
|
// FIXME: #600
|
|
|
|
#[allow(while_let_on_iterator)]
|
2016-02-01 05:47:31 -06:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
|
2016-06-08 08:32:30 -05:00
|
|
|
let item_name = item.ident.name.as_str();
|
|
|
|
let item_name_chars = item_name.chars().count();
|
2016-02-12 11:35:44 -06:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.node {
|
2016-06-08 08:32:30 -05:00
|
|
|
for var in &def.variants {
|
|
|
|
let name = var2str(var);
|
|
|
|
let matching = partial_match(&item_name, &name);
|
|
|
|
let rmatching = partial_rmatch(&item_name, &name);
|
|
|
|
if matching == item_name_chars {
|
|
|
|
span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name starts with the enum's name");
|
|
|
|
}
|
|
|
|
if rmatching == item_name_chars {
|
|
|
|
span_lint(cx, ENUM_VARIANT_NAMES, var.span, "Variant name ends with the enum's name");
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 05:47:31 -06:00
|
|
|
if def.variants.len() < 2 {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-13 06:17:48 -06:00
|
|
|
let first = var2str(&def.variants[0]);
|
2016-02-15 09:59:56 -06:00
|
|
|
let mut pre = &first[..camel_case_until(&*first)];
|
|
|
|
let mut post = &first[camel_case_from(&*first)..];
|
|
|
|
for var in &def.variants {
|
2016-02-01 05:47:31 -06:00
|
|
|
let name = var2str(var);
|
2016-02-15 09:59:56 -06:00
|
|
|
|
2016-04-26 10:05:39 -05:00
|
|
|
let pre_match = partial_match(pre, &name);
|
2016-02-15 09:59:56 -06:00
|
|
|
pre = &pre[..pre_match];
|
2016-04-26 10:05:39 -05:00
|
|
|
let pre_camel = camel_case_until(pre);
|
2016-02-15 09:59:56 -06:00
|
|
|
pre = &pre[..pre_camel];
|
|
|
|
while let Some((next, last)) = name[pre.len()..].chars().zip(pre.chars().rev()).next() {
|
|
|
|
if next.is_lowercase() {
|
|
|
|
let last = pre.len() - last.len_utf8();
|
|
|
|
let last_camel = camel_case_until(&pre[..last]);
|
|
|
|
pre = &pre[..last_camel];
|
|
|
|
} else {
|
|
|
|
break;
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 09:59:56 -06:00
|
|
|
|
2016-04-26 10:05:39 -05:00
|
|
|
let post_match = partial_rmatch(post, &name);
|
2016-02-15 09:59:56 -06:00
|
|
|
let post_end = post.len() - post_match;
|
|
|
|
post = &post[post_end..];
|
2016-04-26 10:05:39 -05:00
|
|
|
let post_camel = camel_case_from(post);
|
2016-02-15 09:59:56 -06:00
|
|
|
post = &post[post_camel..];
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
2016-02-29 02:45:36 -06:00
|
|
|
let (what, value) = match (pre.is_empty(), post.is_empty()) {
|
|
|
|
(true, true) => return,
|
|
|
|
(false, _) => ("pre", pre),
|
|
|
|
(true, false) => ("post", post),
|
2016-02-01 05:47:31 -06:00
|
|
|
};
|
|
|
|
span_help_and_lint(cx,
|
|
|
|
ENUM_VARIANT_NAMES,
|
|
|
|
item.span,
|
|
|
|
&format!("All variants have the same {}fix: `{}`", what, value),
|
|
|
|
&format!("remove the {}fixes and use full paths to \
|
2016-02-29 05:19:32 -06:00
|
|
|
the variants instead of glob imports",
|
|
|
|
what));
|
2016-02-01 05:47:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|