Skip to content

Commit 413713c

Browse files
committed
Add error info when cargo metadata fails to run
1 parent b16d101 commit 413713c

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed

clippy_lints/src/cargo_common_metadata.rs

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ declare_clippy_lint! {
3636
"common metadata is defined in `Cargo.toml`"
3737
}
3838

39-
fn warning(cx: &LateContext<'_, '_>, message: &str) {
40-
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, message);
41-
}
42-
4339
fn missing_warning(cx: &LateContext<'_, '_>, package: &cargo_metadata::Package, field: &str) {
4440
let message = format!("package `{}` is missing `{}` metadata", package.name, field);
45-
warning(cx, &message);
41+
span_lint(cx, CARGO_COMMON_METADATA, DUMMY_SP, &message);
4642
}
4743

4844
fn is_empty_str(value: &Option<String>) -> bool {
@@ -66,12 +62,7 @@ impl LateLintPass<'_, '_> for CargoCommonMetadata {
6662
return;
6763
}
6864

69-
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
70-
metadata
71-
} else {
72-
warning(cx, "could not read cargo metadata");
73-
return;
74-
};
65+
let metadata = unwrap_cargo_metadata!(cx, CARGO_COMMON_METADATA, false);
7566

7667
for package in metadata.packages {
7768
if is_empty_vec(&package.authors) {

clippy_lints/src/multiple_crate_versions.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::source_map::DUMMY_SP;
99

10-
use cargo_metadata::{DependencyKind, MetadataCommand, Node, Package, PackageId};
10+
use cargo_metadata::{DependencyKind, Node, Package, PackageId};
1111
use if_chain::if_chain;
1212
use itertools::Itertools;
1313

@@ -42,13 +42,7 @@ impl LateLintPass<'_, '_> for MultipleCrateVersions {
4242
return;
4343
}
4444

45-
let metadata = if let Ok(metadata) = MetadataCommand::new().exec() {
46-
metadata
47-
} else {
48-
span_lint(cx, MULTIPLE_CRATE_VERSIONS, DUMMY_SP, "could not read cargo metadata");
49-
return;
50-
};
51-
45+
let metadata = unwrap_cargo_metadata!(cx, MULTIPLE_CRATE_VERSIONS, true);
5246
let local_name = cx.tcx.crate_name(LOCAL_CRATE).as_str();
5347
let mut packages = metadata.packages;
5448
packages.sort_by(|a, b| a.name.cmp(&b.name));

clippy_lints/src/utils/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,24 @@ pub fn run_lints(cx: &LateContext<'_, '_>, lints: &[&'static Lint], id: HirId) -
14051405
})
14061406
}
14071407

1408+
#[macro_export]
1409+
macro_rules! unwrap_cargo_metadata {
1410+
($cx: ident, $lint: ident, $deps: expr) => {{
1411+
let mut command = cargo_metadata::MetadataCommand::new();
1412+
if !$deps {
1413+
command.no_deps();
1414+
}
1415+
1416+
match command.exec() {
1417+
Ok(metadata) => metadata,
1418+
Err(err) => {
1419+
span_lint($cx, $lint, DUMMY_SP, &format!("could not read cargo metadata: {}", err));
1420+
return;
1421+
},
1422+
}
1423+
}};
1424+
}
1425+
14081426
#[cfg(test)]
14091427
mod test {
14101428
use super::{trim_multiline, without_block_comments};

clippy_lints/src/wildcard_dependencies.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,7 @@ impl LateLintPass<'_, '_> for WildcardDependencies {
3434
return;
3535
}
3636

37-
let metadata = if let Ok(metadata) = cargo_metadata::MetadataCommand::new().no_deps().exec() {
38-
metadata
39-
} else {
40-
span_lint(cx, WILDCARD_DEPENDENCIES, DUMMY_SP, "could not read cargo metadata");
41-
return;
42-
};
37+
let metadata = unwrap_cargo_metadata!(cx, WILDCARD_DEPENDENCIES, false);
4338

4439
for dep in &metadata.packages[0].dependencies {
4540
// VersionReq::any() does not work

0 commit comments

Comments
 (0)