suggest inserting underscores for simple cases

This commit is contained in:
Oliver Schneider 2016-03-01 10:34:45 +01:00
parent 06ca1fc0a6
commit 5373ffdeb8
2 changed files with 26 additions and 8 deletions

View File

@ -3,7 +3,7 @@ use syntax::codemap::Span;
use syntax::parse::token::InternedString; use syntax::parse::token::InternedString;
use syntax::ast::*; use syntax::ast::*;
use syntax::visit::{self, FnKind}; use syntax::visit::{self, FnKind};
use utils::{span_note_and_lint, in_macro}; use utils::{span_lint_and_then, in_macro};
use strsim::levenshtein; use strsim::levenshtein;
/// **What it does:** This lint warns about names that are very similar and thus confusing /// **What it does:** This lint warns about names that are very similar and thus confusing
@ -95,17 +95,35 @@ impl<'a, 'b, 'c> SimilarNamesNameVisitor<'a, 'b, 'c> {
existing_name.ends_with(&*interned_name) { existing_name.ends_with(&*interned_name) {
continue; continue;
} }
let mut split_at = None;
if dist == 1 { if dist == 1 {
// are we doing stuff like a_bar, b_bar, c_bar? // are we doing stuff like a_bar, b_bar, c_bar?
if interned_name.chars().next() != existing_name.chars().next() && interned_name.chars().nth(1) == Some('_') { if interned_name.chars().next() != existing_name.chars().next() {
continue; if interned_name.chars().nth(1) == Some('_') {
continue;
}
split_at = interned_name.chars().next().map(|c| c.len_utf8());
} }
// are we doing stuff like foo_x, foo_y, foo_z? // are we doing stuff like foo_x, foo_y, foo_z?
if interned_name.chars().rev().next() != existing_name.chars().rev().next() && interned_name.chars().rev().nth(1) == Some('_') { if interned_name.chars().rev().next() != existing_name.chars().rev().next() {
continue; if interned_name.chars().rev().nth(1) == Some('_') {
continue;
}
split_at = interned_name.char_indices().rev().next().map(|(i, _)| i);
} }
} }
span_note_and_lint(self.0.cx, SIMILAR_NAMES, span, "binding's name is too similar to existing binding", sp, "existing binding defined here"); span_lint_and_then(self.0.cx,
SIMILAR_NAMES,
span,
"binding's name is too similar to existing binding",
|diag| {
diag.span_note(sp, "existing binding defined here");
if let Some(split) = split_at {
diag.span_help(span, &format!("separate the discriminating character by an underscore like: `{}_{}`",
&interned_name[..split],
&interned_name[split..]));
}
});
return; return;
} }
self.0.names.push((interned_name, span)); self.0.names.push((interned_name, span));

View File

@ -29,9 +29,9 @@ fn main() {
let blubx: i32; //~ NOTE: existing binding defined here let blubx: i32; //~ NOTE: existing binding defined here
let bluby: i32; //~ ERROR: name is too similar let bluby: i32; //~ ERROR: name is too similar
//~| HELP: separate the discriminating character by an underscore like: `blub_y`
let cake: i32; //~ NOTE: existing binding defined here let cake: i32; //~ NOTE: existing binding defined here
let caked: i32; //~ NOTE: existing binding defined here let cakes: i32;
let cakes: i32; //~ ERROR: name is too similar
let coke: i32; //~ ERROR: name is too similar let coke: i32; //~ ERROR: name is too similar
} }