Skip to content

Commit ea7066a

Browse files
committed
Auto merge of #5688 - ebroto:fix_cargo_tests_in_rustc, r=flip1995
Fix cargo tests when running inside the rustlang/rust repo It seems we hit rust-lang/cargo#5418, so I've applied the suggested solution. Also added some more info when cargo-metadata fails to execute. (there was no open issue for this) changelog: none
2 parents b16d101 + c325c12 commit ea7066a

File tree

13 files changed

+39
-29
lines changed

13 files changed

+39
-29
lines changed

clippy_dev/src/new_lint.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
147147
name = "{}"
148148
version = "0.1.0"
149149
publish = false
150+
151+
[workspace]
150152
"#,
151153
hint, lint_name
152154
)

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

tests/compile-test.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,6 @@ fn run_ui_cargo(config: &mut compiletest::Config) {
220220
Ok(result)
221221
}
222222

223-
if cargo::is_rustc_test_suite() {
224-
return;
225-
}
226-
227223
config.mode = TestMode::Ui;
228224
config.src_base = Path::new("tests").join("ui-cargo").canonicalize().unwrap();
229225

tests/ui-cargo/cargo_common_metadata/fail/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
name = "cargo_common_metadata"
33
version = "0.1.0"
44
publish = false
5+
6+
[workspace]

tests/ui-cargo/cargo_common_metadata/pass/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ readme = "README.md"
99
license = "MIT OR Apache-2.0"
1010
keywords = ["metadata", "lint", "clippy"]
1111
categories = ["development-tools::testing"]
12+
13+
[workspace]

tests/ui-cargo/multiple_crate_versions/5041_allow_dev_build/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ name = "multiple_crate_versions"
55
version = "0.1.0"
66
publish = false
77

8+
[workspace]
9+
810
# One of the versions of winapi is only a dev dependency: allowed
911
[dependencies]
1012
ctrlc = "=3.1.0"

tests/ui-cargo/multiple_crate_versions/fail/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name = "multiple_crate_versions"
33
version = "0.1.0"
44
publish = false
55

6+
[workspace]
7+
68
[dependencies]
79
ctrlc = "=3.1.0"
810
ansi_term = "=0.11.0"

tests/ui-cargo/multiple_crate_versions/pass/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name = "cargo_common_metadata"
33
version = "0.1.0"
44
publish = false
55

6+
[workspace]
7+
68
[dependencies]
79
regex = "1.3.7"
810
serde = "1.0.110"

tests/ui-cargo/wildcard_dependencies/fail/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ name = "wildcard_dependencies"
33
version = "0.1.0"
44
publish = false
55

6+
[workspace]
7+
68
[dependencies]
79
regex = "*"

tests/ui-cargo/wildcard_dependencies/pass/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ name = "wildcard_dependencies"
33
version = "0.1.0"
44
publish = false
55

6+
[workspace]
7+
68
[dependencies]
79
regex = "1"

0 commit comments

Comments
 (0)