Skip to content

Commit 81c233b

Browse files
committed
target_path_not_found_error_message: simplify
1 parent 56602aa commit 81c233b

File tree

1 file changed

+20
-36
lines changed

1 file changed

+20
-36
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -958,18 +958,12 @@ fn target_path_not_found_error_message(
958958
match (kind, commonly_wrong) {
959959
// commonly wrong paths
960960
("test" | "bench" | "example", true) => target_path.push(kind),
961-
("bin", true) => {
962-
target_path.push("src");
963-
target_path.push("bins");
964-
}
961+
("bin", true) => target_path.extend(["src", "bins"]),
965962
// default inferred paths
966963
("test", false) => target_path.push(DEFAULT_TEST_DIR_NAME),
967964
("bench", false) => target_path.push(DEFAULT_BENCH_DIR_NAME),
968965
("example", false) => target_path.push(DEFAULT_EXAMPLE_DIR_NAME),
969-
("bin", false) => {
970-
target_path.push("src");
971-
target_path.push("bin");
972-
}
966+
("bin", false) => target_path.extend(["src", "bin"]),
973967
_ => unreachable!("invalid target kind: {}", kind),
974968
}
975969

@@ -988,36 +982,26 @@ fn target_path_not_found_error_message(
988982
let target_name = name_or_panic(target);
989983
let commonly_wrong_paths = possible_target_paths(&target_name, target_kind, true);
990984
let possible_paths = possible_target_paths(&target_name, target_kind, false);
991-
let existing_wrong_path_index = match (
992-
package_root.join(&commonly_wrong_paths[0]).exists(),
993-
package_root.join(&commonly_wrong_paths[1]).exists(),
994-
) {
995-
(true, _) => Some(0),
996-
(_, true) => Some(1),
997-
_ => None,
998-
};
999985

1000-
if let Some(i) = existing_wrong_path_index {
1001-
return format!(
1002-
"\
1003-
can't find `{name}` {kind} at default paths, but found a file at `{wrong_path}`.
1004-
Perhaps rename the file to `{possible_path}` for target auto-discovery, \
1005-
or specify {kind}.path if you want to use a non-default path.",
1006-
name = target_name,
1007-
kind = target_kind,
1008-
wrong_path = commonly_wrong_paths[i].display(),
1009-
possible_path = possible_paths[i].display(),
1010-
);
986+
if let Some((wrong_path, possible_path)) = commonly_wrong_paths
987+
.iter()
988+
.zip(possible_paths.iter())
989+
.filter(|(wp, _)| package_root.join(wp).exists())
990+
.next()
991+
{
992+
let [wrong_path, possible_path] = [wrong_path, possible_path].map(|p| p.display());
993+
format!(
994+
"can't find `{target_name}` {target_kind} at default paths, but found a file at `{wrong_path}`.\n\
995+
Perhaps rename the file to `{possible_path}` for target auto-discovery, \
996+
or specify {target_kind}.path if you want to use a non-default path."
997+
)
998+
} else {
999+
let [path_file, path_dir] = possible_paths.each_ref().map(|p| p.display());
1000+
format!(
1001+
"can't find `{target_name}` {target_kind} at `{path_file}` or `{path_dir}`. \
1002+
Please specify {target_kind}.path if you want to use a non-default path."
1003+
)
10111004
}
1012-
1013-
format!(
1014-
"can't find `{name}` {kind} at `{path_file}` or `{path_dir}`. \
1015-
Please specify {kind}.path if you want to use a non-default path.",
1016-
name = target_name,
1017-
kind = target_kind,
1018-
path_file = possible_paths[0].display(),
1019-
path_dir = possible_paths[1].display(),
1020-
)
10211005
}
10221006

10231007
fn target_path(

0 commit comments

Comments
 (0)