2016-02-01 11:47:31 +00: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;
|
|
|
|
use utils::span_help_and_lint;
|
2016-02-15 15:59:56 +00:00
|
|
|
use utils::{camel_case_from, camel_case_until};
|
2016-02-01 11:47:31 +00: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 23:13:29 +00:00
|
|
|
declare_lint! {
|
|
|
|
pub ENUM_VARIANT_NAMES, Warn,
|
|
|
|
"finds enums where all variants share a prefix/postfix"
|
|
|
|
}
|
2016-02-01 11:47:31 +00: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-02-15 15:59:56 +00: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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 11:47:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for EnumVariantNames {
|
2016-02-15 15:59:56 +00:00
|
|
|
// FIXME: #600
|
|
|
|
#[allow(while_let_on_iterator)]
|
2016-02-01 11:47:31 +00:00
|
|
|
fn check_item(&mut self, cx: &EarlyContext, item: &Item) {
|
2016-02-12 17:35:44 +00:00
|
|
|
if let ItemKind::Enum(ref def, _) = item.node {
|
2016-02-01 11:47:31 +00:00
|
|
|
if def.variants.len() < 2 {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-13 12:17:48 +00:00
|
|
|
let first = var2str(&def.variants[0]);
|
2016-02-15 15:59:56 +00: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 11:47:31 +00:00
|
|
|
let name = var2str(var);
|
2016-02-15 15:59:56 +00:00
|
|
|
|
2016-04-26 15:05:39 +00:00
|
|
|
let pre_match = partial_match(pre, &name);
|
2016-02-15 15:59:56 +00:00
|
|
|
pre = &pre[..pre_match];
|
2016-04-26 15:05:39 +00:00
|
|
|
let pre_camel = camel_case_until(pre);
|
2016-02-15 15:59:56 +00: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 11:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 15:59:56 +00:00
|
|
|
|
2016-04-26 15:05:39 +00:00
|
|
|
let post_match = partial_rmatch(post, &name);
|
2016-02-15 15:59:56 +00:00
|
|
|
let post_end = post.len() - post_match;
|
|
|
|
post = &post[post_end..];
|
2016-04-26 15:05:39 +00:00
|
|
|
let post_camel = camel_case_from(post);
|
2016-02-15 15:59:56 +00:00
|
|
|
post = &post[post_camel..];
|
2016-02-01 11:47:31 +00:00
|
|
|
}
|
2016-02-29 08:45:36 +00:00
|
|
|
let (what, value) = match (pre.is_empty(), post.is_empty()) {
|
|
|
|
(true, true) => return,
|
|
|
|
(false, _) => ("pre", pre),
|
|
|
|
(true, false) => ("post", post),
|
2016-02-01 11:47:31 +00: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 11:19:32 +00:00
|
|
|
the variants instead of glob imports",
|
|
|
|
what));
|
2016-02-01 11:47:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|