Skip to content

Commit 95b4640

Browse files
committed
only suggest lev_distance < 4
1 parent 98e1bdb commit 95b4640

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/bin/cargo/main.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ fn find_closest(config: &Config, cmd: &str) -> Option<String> {
113113
let cmds = list_commands(config);
114114
// Only consider candidates with a lev_distance of 3 or less so we don't
115115
// suggest out-of-the-blue options.
116-
let mut filtered = cmds.iter()
117-
.map(|&(ref c, _)| (lev_distance(c, cmd), c))
116+
cmds.into_iter()
117+
.map(|(c, _)| (lev_distance(&c, cmd), c))
118118
.filter(|&(d, _)| d < 4)
119-
.collect::<Vec<_>>();
120-
filtered.sort_by(|a, b| a.0.cmp(&b.0));
121-
filtered.get(0).map(|slot| slot.1.clone())
119+
.min_by_key(|a| a.0)
120+
.map(|slot| slot.1)
122121
}
123122

124123
fn execute_external_subcommand(config: &Config, cmd: &str, args: &[&str]) -> CliResult {

src/cargo/core/resolver/mod.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,9 @@ fn activate_deps_loop(
231231
// to amortize the cost of the current time lookup.
232232
ticks += 1;
233233
if let Some(config) = config {
234-
if config.shell().is_err_tty() && !printed && ticks % 1000 == 0
234+
if config.shell().is_err_tty()
235+
&& !printed
236+
&& ticks % 1000 == 0
235237
&& start.elapsed() - deps_time > time_to_print
236238
{
237239
printed = true;
@@ -858,12 +860,14 @@ fn activation_error(
858860
msg.push_str("\nversions that meet the requirements `");
859861
msg.push_str(&dep.version_req().to_string());
860862
msg.push_str("` are: ");
861-
msg.push_str(&candidates
862-
.iter()
863-
.map(|v| v.summary.version())
864-
.map(|v| v.to_string())
865-
.collect::<Vec<_>>()
866-
.join(", "));
863+
msg.push_str(
864+
&candidates
865+
.iter()
866+
.map(|v| v.summary.version())
867+
.map(|v| v.to_string())
868+
.collect::<Vec<_>>()
869+
.join(", "),
870+
);
867871

868872
let mut conflicting_activations: Vec<_> = conflicting_activations.iter().collect();
869873
conflicting_activations.sort_unstable();
@@ -926,7 +930,7 @@ fn activation_error(
926930
// We didn't actually find any candidates, so we need to
927931
// give an error message that nothing was found.
928932
//
929-
// Maybe the user mistyped the ver_req? Like `dep="2"` when `dep=".2"`
933+
// Maybe the user mistyped the ver_req? Like `dep="2"` when `dep="0.2"`
930934
// was meant. So we re-query the registry with `deb="*"` so we can
931935
// list a few versions that were actually found.
932936
let all_req = semver::VersionReq::parse("*").unwrap();
@@ -981,26 +985,31 @@ fn activation_error(
981985
// was meant. So we try asking the registry for a `fuzzy` search for suggestions.
982986
let mut candidates = Vec::new();
983987
if let Err(e) = registry.query(&new_dep, &mut |s| candidates.push(s.name()), true) {
984-
return e
988+
return e;
985989
};
990+
candidates.sort_unstable();
991+
candidates.dedup();
992+
let mut candidates: Vec<_> = candidates
993+
.iter()
994+
.map(|n| (lev_distance(&*new_dep.name(), &*n), n))
995+
.filter(|&(d, _)| d < 4)
996+
.collect();
997+
candidates.sort_by_key(|o| o.0);
986998
let mut msg = format!(
987999
"no matching package named `{}` found\n\
9881000
location searched: {}\n",
9891001
dep.name(),
9901002
dep.source_id()
9911003
);
9921004
if !candidates.is_empty() {
993-
candidates.sort_unstable();
994-
candidates.dedup();
995-
candidates.sort_by_key(|o| lev_distance(&*new_dep.name(), &*o));
9961005
let mut names = candidates
9971006
.iter()
9981007
.take(3)
999-
.map(|c| c.to_string())
1008+
.map(|c| c.1.as_str())
10001009
.collect::<Vec<_>>();
10011010

10021011
if candidates.len() > 3 {
1003-
names.push("...".into());
1012+
names.push("...");
10041013
}
10051014

10061015
msg.push_str("did you mean: ");

tests/testsuite/build.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,6 @@ fn cargo_compile_with_dep_name_mismatch() {
10201020
execs().with_status(101).with_stderr(&format!(
10211021
r#"error: no matching package named `notquitebar` found
10221022
location searched: {proj_dir}/bar
1023-
did you mean: bar
10241023
required by package `foo v0.0.1 ({proj_dir})`
10251024
"#,
10261025
proj_dir = p.url()

0 commit comments

Comments
 (0)