Warning on conflicting crate_types keys

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
This commit is contained in:
hi-rustin 2022-01-23 17:20:39 +08:00
parent 375ccd2f4a
commit a317aff9b8
4 changed files with 87 additions and 2 deletions

View File

@ -2061,7 +2061,7 @@ impl TomlTarget {
if self.proc_macro_raw.is_some() && self.proc_macro_raw2.is_some() {
warnings.push(format!(
"found both `proc-macro` and `proc_macro` are set \
in the `{}` library",
in the `{}` library target",
self.name()
));
}
@ -2078,6 +2078,17 @@ impl TomlTarget {
})
}
fn validate_crate_types(&self, target_kind_human: &str, warnings: &mut Vec<String>) {
if self.crate_type.is_some() && self.crate_type2.is_some() {
warnings.push(format!(
"found both `crate-type` and `crate_type` are set \
in the `{}` {} target",
self.name(),
target_kind_human
));
}
}
fn crate_types(&self) -> Option<&Vec<String>> {
self.crate_type
.as_ref()

View File

@ -175,6 +175,7 @@ fn clean_lib(
None => return Ok(None),
};
lib.validate_proc_macro(warnings);
lib.validate_crate_types("library", warnings);
validate_target_name(lib, "library", "lib", warnings)?;
@ -399,6 +400,7 @@ fn clean_examples(
let mut result = Vec::new();
for (path, toml) in targets {
toml.validate_crate_types("example", warnings);
let crate_types = match toml.crate_types() {
Some(kinds) => kinds.iter().map(|s| s.into()).collect(),
None => Vec::new(),

View File

@ -1676,6 +1676,78 @@ Caused by:
.run();
}
#[cargo_test]
fn lib_crate_types_conflicting_warning() {
let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[lib]
name = "foo"
crate-type = ["rlib", "dylib"]
crate_type = ["staticlib", "dylib"]
"#,
)
.file("src/lib.rs", "pub fn foo() {}")
.build();
p.cargo("build")
.with_stderr_contains(
"[WARNING] found both `crate-type` and `crate_type` are set in the `foo` library target",
)
.run();
}
#[cargo_test]
fn examples_crate_types_conflicting_warning() {
let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.5.0"
authors = ["wycats@example.com"]
[[example]]
name = "ex"
path = "examples/ex.rs"
crate-type = ["rlib", "dylib"]
crate_type = ["proc_macro"]
[[example]]
name = "goodbye"
path = "examples/ex-goodbye.rs"
crate-type = ["rlib", "dylib"]
crate_type = ["rlib", "staticlib"]
"#,
)
.file("src/lib.rs", "")
.file(
"examples/ex.rs",
r#"
fn main() { println!("ex"); }
"#,
)
.file(
"examples/ex-goodbye.rs",
r#"
fn main() { println!("goodbye"); }
"#,
)
.build();
p.cargo("build")
.with_stderr_contains(
"\
[WARNING] found both `crate-type` and `crate_type` are set in the `ex` example target
[WARNING] found both `crate-type` and `crate_type` are set in the `goodbye` example target",
)
.run();
}
#[cargo_test]
fn self_dependency() {
let p = project()

View File

@ -406,7 +406,7 @@ fn proc_macro_conflicting_warning() {
foo.cargo("build")
.with_stderr_contains(
"[WARNING] found both `proc-macro` and `proc_macro` are set in the `foo` library",
"[WARNING] found both `proc-macro` and `proc_macro` are set in the `foo` library target",
)
.run();
}