Skip to content

Commit 4bb7cf1

Browse files
committed
Introduce max_suggestion_distance function to avoid duplicating the heuristic
1 parent 9ba657c commit 4bb7cf1

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

src/librustc_resolve/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ use syntax::ext::mtwt;
7272
use syntax::parse::token::{self, special_names, special_idents};
7373
use syntax::ptr::P;
7474
use syntax::codemap::{self, Span, Pos};
75-
use syntax::util::lev_distance::lev_distance;
75+
use syntax::util::lev_distance::{lev_distance, max_suggestion_distance};
7676

7777
use rustc_front::intravisit::{self, FnKind, Visitor};
7878
use rustc_front::hir;
@@ -3384,11 +3384,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33843384
}
33853385
}
33863386

3387-
// As a loose rule to avoid obviously incorrect suggestions, clamp the
3388-
// maximum edit distance we will accept for a suggestion to one third of
3389-
// the typo'd name's length.
3390-
let max_distance = std::cmp::max(name.len(), 3) / 3;
3391-
3387+
let max_distance = max_suggestion_distance(name);
33923388
if !values.is_empty() && values[smallest] <= max_distance && name != &maybes[smallest][..] {
33933389

33943390
SuggestionType::Function(maybes[smallest].to_string())

src/libsyntax/ext/base.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use parse::token;
2424
use parse::token::{InternedString, intern, str_to_ident};
2525
use ptr::P;
2626
use util::small_vector::SmallVector;
27-
use util::lev_distance::lev_distance;
27+
use util::lev_distance::{lev_distance, max_suggestion_distance};
2828
use ext::mtwt;
2929
use fold::Folder;
3030

@@ -779,10 +779,8 @@ impl<'a> ExtCtxt<'a> {
779779
}
780780

781781
pub fn suggest_macro_name(&mut self, name: &str, span: Span) {
782-
use std::cmp::max;
783-
784782
let mut min: Option<(Name, usize)> = None;
785-
let max_dist = max(name.len() / 3, 1);
783+
let max_dist = max_suggestion_distance(name);
786784
for macro_name in self.syntax_env.names.iter() {
787785
let dist = lev_distance(name, &macro_name.as_str());
788786
if dist <= max_dist && (min.is_none() || min.unwrap().1 > dist) {

src/libsyntax/util/lev_distance.rs

+8
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ pub fn lev_distance(me: &str, t: &str) -> usize {
4141
dcol[t_last + 1]
4242
}
4343

44+
pub fn max_suggestion_distance(name: &str) -> usize {
45+
use std::cmp::max;
46+
// As a loose rule to avoid obviously incorrect suggestions, clamp the
47+
// maximum edit distance we will accept for a suggestion to one third of
48+
// the typo'd name's length.
49+
max(name.len(), 3) / 3
50+
}
51+
4452
#[test]
4553
fn test_lev_distance() {
4654
use std::char::{ from_u32, MAX };

0 commit comments

Comments
 (0)