Skip to content

Commit 7b4f436

Browse files
committed
add comments and cleanup
1 parent a860a72 commit 7b4f436

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

compiler/rustc_span/src/edit_distance.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ fn find_best_match_for_name_impl(
190190

191191
let mut dist = dist.unwrap_or_else(|| cmp::max(lookup.len(), 3) / 3);
192192
let mut best = None;
193+
// store the candidates with the same distance, only for `use_substring_score` current.
193194
let mut next_candidates = vec![];
194195
for c in candidates {
195196
match if use_substring_score {
@@ -200,19 +201,25 @@ fn find_best_match_for_name_impl(
200201
Some(0) => return Some(*c),
201202
Some(d) => {
202203
if use_substring_score {
203-
dist = d;
204+
if d < dist {
205+
dist = d;
206+
next_candidates.clear();
207+
} else {
208+
// `d == dist` here, we need to store the candidates with the same distance
209+
// so we won't decrease the distance in the next loop.
210+
}
204211
next_candidates.push(*c);
205-
best = Some(*c);
206212
} else {
207213
dist = d - 1;
208-
best = Some(*c);
209214
}
215+
best = Some(*c);
210216
}
211217
None => {}
212218
}
213219
}
214220

215221
if next_candidates.len() > 1 {
222+
debug_assert!(use_substring_score);
216223
best = find_best_match_for_name_impl(
217224
false,
218225
&next_candidates,

0 commit comments

Comments
 (0)