Skip to content

Commit

Permalink
Use warn_on_deprecated
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Mar 9, 2022
1 parent b295e2f commit 8b895cc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 54 deletions.
62 changes: 26 additions & 36 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ pub fn parse_document(
.map_err(|e| anyhow::Error::from(e).context("could not parse input as TOML"))
}

/// Warn about paths that have been deprecated and may conflict.
fn warn_on_deprecated(new_path: &str, name: &str, kind: &str, warnings: &mut Vec<String>) {
let old_path = new_path.replace("-", "_");
warnings.push(format!(
"conflicting between `{new_path}` and `{old_path}` in the `{name}` {kind}.\n
`{old_path}` is ignored and not recommended for use in the future"
))
}

type TomlLibTarget = TomlTarget;
type TomlBinTarget = TomlTarget;
type TomlExampleTarget = TomlTarget;
Expand Down Expand Up @@ -1265,23 +1274,15 @@ impl TomlManifest {
// Collect the dependencies.
process_dependencies(&mut cx, me.dependencies.as_ref(), None)?;
if me.dev_dependencies.is_some() && me.dev_dependencies2.is_some() {
cx.warnings.push(format!(
"found both `dev-dependencies` and `dev_dependencies` are set \
in the `{}` package",
package_name
));
warn_on_deprecated("dev-dependencies", package_name, "package", cx.warnings);
}
let dev_deps = me
.dev_dependencies
.as_ref()
.or_else(|| me.dev_dependencies2.as_ref());
process_dependencies(&mut cx, dev_deps, Some(DepKind::Development))?;
if me.build_dependencies.is_some() && me.build_dependencies2.is_some() {
cx.warnings.push(format!(
"found both `build-dependencies` and `build_dependencies` are set \
in the `{}` package",
package_name
));
warn_on_deprecated("build-dependencies", package_name, "package", cx.warnings);
}
let build_deps = me
.build_dependencies
Expand All @@ -1297,23 +1298,15 @@ impl TomlManifest {
};
process_dependencies(&mut cx, platform.dependencies.as_ref(), None)?;
if platform.build_dependencies.is_some() && platform.build_dependencies2.is_some() {
cx.warnings.push(format!(
"found both `build-dependencies` and `build_dependencies` are set \
in the `{}` platform target",
name
));
warn_on_deprecated("build-dependencies", name, "platform target", cx.warnings);
}
let build_deps = platform
.build_dependencies
.as_ref()
.or_else(|| platform.build_dependencies2.as_ref());
process_dependencies(&mut cx, build_deps, Some(DepKind::Build))?;
if platform.dev_dependencies.is_some() && platform.dev_dependencies2.is_some() {
cx.warnings.push(format!(
"found both `dev-dependencies` and `dev_dependencies` are set \
in the `{}` platform target",
name
));
warn_on_deprecated("dev-dependencies", name, "platform target", cx.warnings);
}
let dev_deps = platform
.dev_dependencies
Expand Down Expand Up @@ -1937,11 +1930,7 @@ impl<P: ResolveToPath> DetailedTomlDependency<P> {
let version = self.version.as_deref();
let mut dep = Dependency::parse(pkg_name, version, new_source_id)?;
if self.default_features.is_some() && self.default_features2.is_some() {
cx.warnings.push(format!(
"found both `default-features` and `default_features` are set \
in the `{}` dependency",
name_in_toml
));
warn_on_deprecated("default-features", name_in_toml, "dependency", cx.warnings);
}
dep.set_features(self.features.iter().flatten())
.set_default_features(
Expand Down Expand Up @@ -2094,11 +2083,12 @@ impl TomlTarget {

fn validate_proc_macro(&self, warnings: &mut Vec<String>) {
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 target",
self.name()
));
warn_on_deprecated(
"proc-macro",
self.name().as_str(),
"library target",
warnings,
);
}
}

Expand All @@ -2115,12 +2105,12 @@ 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
));
warn_on_deprecated(
"crate-type",
self.name().as_str(),
format!("{target_kind_human} target").as_str(),
warnings,
);
}
}

Expand Down
37 changes: 22 additions & 15 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,9 +1705,10 @@ fn dev_dependencies_conflicting_warning() {
.file("a/src/lib.rs", "")
.build();
p.cargo("build")
.with_stderr_contains(
"[WARNING] found both `dev-dependencies` and `dev_dependencies` are set in the `foo` package",
)
.with_stderr_contains(
"[WARNING] conflicting between `dev-dependencies` and `dev_dependencies` in the `foo` package.\n
`dev_dependencies` is ignored and not recommended for use in the future"
)
.run();
}

Expand Down Expand Up @@ -1740,9 +1741,10 @@ fn build_dependencies_conflicting_warning() {
.file("a/src/lib.rs", "")
.build();
p.cargo("build")
.with_stderr_contains(
"[WARNING] found both `build-dependencies` and `build_dependencies` are set in the `foo` package",
)
.with_stderr_contains(
"[WARNING] conflicting between `build-dependencies` and `build_dependencies` in the `foo` package.\n
`build_dependencies` is ignored and not recommended for use in the future"
)
.run();
}

Expand All @@ -1766,9 +1768,10 @@ fn lib_crate_types_conflicting_warning() {
.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",
)
.with_stderr_contains(
"[WARNING] conflicting between `crate-type` and `crate_type` in the `foo` library target.\n
`crate_type` is ignored and not recommended for use in the future",
)
.run();
}

Expand Down Expand Up @@ -1812,8 +1815,10 @@ fn examples_crate_types_conflicting_warning() {
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",
[WARNING] conflicting between `crate-type` and `crate_type` in the `ex` example target.\n
`crate_type` is ignored and not recommended for use in the future
[WARNING] conflicting between `crate-type` and `crate_type` in the `goodbye` example target.\n
`crate_type` is ignored and not recommended for use in the future",
)
.run();
}
Expand Down Expand Up @@ -3016,9 +3021,10 @@ fn cargo_platform_specific_dependency_build_dependencies_conflicting_warning() {

p.cargo("build")
.with_stderr_contains(
format!("[WARNING] found both `build-dependencies` and `build_dependencies` are set in the `{}` platform target", host),
format!("[WARNING] conflicting between `build-dependencies` and `build_dependencies` in the `{}` platform target.\n
`build_dependencies` is ignored and not recommended for use in the future", host)
)
.run();
.run();

assert!(p.bin("foo").is_file());
}
Expand Down Expand Up @@ -3055,9 +3061,10 @@ fn cargo_platform_specific_dependency_dev_dependencies_conflicting_warning() {

p.cargo("build")
.with_stderr_contains(
format!("[WARNING] found both `dev-dependencies` and `dev_dependencies` are set in the `{}` platform target", host),
format!("[WARNING] conflicting between `dev-dependencies` and `dev_dependencies` in the `{}` platform target.\n
`dev_dependencies` is ignored and not recommended for use in the future", host)
)
.run();
.run();

assert!(p.bin("foo").is_file());
p.cargo("test").run();
Expand Down
5 changes: 3 additions & 2 deletions tests/testsuite/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,8 @@ fn default_features_conflicting_warning() {

p.cargo("build")
.with_stderr_contains(
"[WARNING] found both `default-features` and `default_features` are set in the `a` dependency",
"[WARNING] conflicting between `default-features` and `default_features` in the `a` dependency.\n
`default_features` is ignored and not recommended for use in the future"
)
.run();
.run();
}
3 changes: 2 additions & 1 deletion tests/testsuite/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,8 @@ 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 target",
"[WARNING] conflicting between `proc-macro` and `proc_macro` in the `foo` library target.\n
`proc_macro` is ignored and not recommended for use in the future",
)
.run();
}
Expand Down

0 comments on commit 8b895cc

Please sign in to comment.