Skip to content

Commit 3885b78

Browse files
authored
fix(manifest): Point out when a key belongs to config (#16256)
### What does this PR try to resolve? Inspired by #16251 ### How to test and review this PR?
2 parents c24e1c1 + 97421c9 commit 3885b78

File tree

3 files changed

+139
-57
lines changed

3 files changed

+139
-57
lines changed

src/cargo/util/context/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,29 @@ macro_rules! get_value_typed {
158158
};
159159
}
160160

161+
pub const TOP_LEVEL_CONFIG_KEYS: &[&str] = &[
162+
"paths",
163+
"alias",
164+
"build",
165+
"credential-alias",
166+
"doc",
167+
"env",
168+
"future-incompat-report",
169+
"cache",
170+
"cargo-new",
171+
"http",
172+
"install",
173+
"net",
174+
"patch",
175+
"profile",
176+
"resolver",
177+
"registries",
178+
"registry",
179+
"source",
180+
"target",
181+
"term",
182+
];
183+
161184
/// Indicates why a config value is being loaded.
162185
#[derive(Clone, Copy, Debug)]
163186
enum WhyLoad {

src/cargo/util/toml/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::util::interning::InternedString;
3535
use crate::util::lints::{get_key_value_span, rel_cwd_manifest_path};
3636
use crate::util::{
3737
self, GlobalContext, IntoUrl, OnceExt, OptVersionReq, context::ConfigRelativePath,
38+
context::TOP_LEVEL_CONFIG_KEYS,
3839
};
3940

4041
mod embedded;
@@ -2887,11 +2888,18 @@ fn deprecated_underscore<T>(
28872888
}
28882889

28892890
fn warn_on_unused(unused: &BTreeSet<String>, warnings: &mut Vec<String>) {
2891+
use std::fmt::Write as _;
2892+
28902893
for key in unused {
2891-
warnings.push(format!("unused manifest key: {}", key));
2892-
if key == "profiles.debug" {
2893-
warnings.push("use `[profile.dev]` to configure debug builds".to_string());
2894+
let mut message = format!("unused manifest key: {}", key);
2895+
if TOP_LEVEL_CONFIG_KEYS.iter().any(|c| c == key) {
2896+
write!(
2897+
&mut message,
2898+
"\nhelp: {key} is a valid .cargo/config.toml key"
2899+
)
2900+
.unwrap();
28942901
}
2902+
warnings.push(message);
28952903
}
28962904
}
28972905

tests/testsuite/bad_config.rs

Lines changed: 105 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -772,74 +772,125 @@ fn unused_keys() {
772772
.file(
773773
"Cargo.toml",
774774
r#"
775-
[package]
776-
name = "foo"
777-
version = "0.1.0"
778-
edition = "2015"
779-
authors = []
775+
paths = ["/path/to/override"]
780776
781-
[target.foo]
782-
bar = "3"
783-
"#,
784-
)
785-
.file("src/lib.rs", "")
786-
.build();
777+
[package]
778+
name = "foo"
779+
version = "0.1.0"
780+
edition = "2015"
781+
authors = []
782+
unused = "foo"
787783
788-
p.cargo("check")
789-
.with_stderr_data(str![[r#"
790-
[WARNING] unused manifest key: target.foo.bar
791-
[CHECKING] foo v0.1.0 ([ROOT]/foo)
792-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
784+
[target.foo]
785+
bar = "3"
793786
794-
"#]])
795-
.run();
787+
[lib]
788+
build = "foo"
796789
797-
let p = project()
798-
.file(
799-
"Cargo.toml",
800-
r#"
801-
[package]
790+
## Config fields
802791
803-
name = "foo"
804-
version = "0.5.0"
805-
edition = "2015"
806-
authors = ["[email protected]"]
807-
unused = "foo"
808-
"#,
809-
)
810-
.file("src/lib.rs", "pub fn foo() {}")
811-
.build();
812-
p.cargo("check")
813-
.with_stderr_data(str![[r#"
814-
[WARNING] unused manifest key: package.unused
815-
[CHECKING] foo v0.5.0 ([ROOT]/foo)
816-
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
792+
[alias]
793+
b = "build"
817794
818-
"#]])
819-
.run();
795+
[build]
796+
jobs = 1
820797
821-
let p = project()
822-
.at("bar")
823-
.file(
824-
"Cargo.toml",
825-
r#"
826-
[package]
798+
[credential-alias]
799+
my-alias = ["/usr/bin/cargo-credential-example", "--argument", "value", "--flag"]
827800
828-
name = "foo"
829-
version = "0.5.0"
830-
edition = "2015"
831-
authors = ["[email protected]"]
801+
[doc]
802+
browser = "chromium"
832803
833-
[lib]
834-
build = "foo"
835-
"#,
804+
[env]
805+
ENV_VAR_NAME = "value"
806+
807+
[future-incompat-report]
808+
frequency = 'always'
809+
810+
[cache]
811+
auto-clean-frequency = "1 day"
812+
813+
[cargo-new]
814+
vcs = "none"
815+
816+
[http]
817+
debug = false
818+
819+
[install]
820+
root = "/some/path"
821+
822+
[net]
823+
retry = 3
824+
825+
[net.ssh]
826+
known-hosts = ["..."]
827+
828+
[resolver]
829+
incompatible-rust-versions = "allow"
830+
831+
[registries.alternative]
832+
index = "…"
833+
834+
[registries.crates-io]
835+
protocol = "sparse"
836+
837+
[registry]
838+
default = "…"
839+
840+
[source.alternative]
841+
replace-with = "…"
842+
843+
[target.'cfg(unix)']
844+
linker = "…"
845+
846+
[term]
847+
quiet = false
848+
"#,
836849
)
837-
.file("src/lib.rs", "pub fn foo() {}")
850+
.file("src/lib.rs", "")
838851
.build();
852+
839853
p.cargo("check")
840854
.with_stderr_data(str![[r#"
855+
[WARNING] unused manifest key: alias
856+
[HELP] alias is a valid .cargo/config.toml key
857+
[WARNING] unused manifest key: build
858+
[HELP] build is a valid .cargo/config.toml key
859+
[WARNING] unused manifest key: cache
860+
[HELP] cache is a valid .cargo/config.toml key
861+
[WARNING] unused manifest key: cargo-new
862+
[HELP] cargo-new is a valid .cargo/config.toml key
863+
[WARNING] unused manifest key: credential-alias
864+
[HELP] credential-alias is a valid .cargo/config.toml key
865+
[WARNING] unused manifest key: doc
866+
[HELP] doc is a valid .cargo/config.toml key
867+
[WARNING] unused manifest key: env
868+
[HELP] env is a valid .cargo/config.toml key
869+
[WARNING] unused manifest key: future-incompat-report
870+
[HELP] future-incompat-report is a valid .cargo/config.toml key
871+
[WARNING] unused manifest key: http
872+
[HELP] http is a valid .cargo/config.toml key
873+
[WARNING] unused manifest key: install
874+
[HELP] install is a valid .cargo/config.toml key
841875
[WARNING] unused manifest key: lib.build
842-
[CHECKING] foo v0.5.0 ([ROOT]/bar)
876+
[WARNING] unused manifest key: net
877+
[HELP] net is a valid .cargo/config.toml key
878+
[WARNING] unused manifest key: package.unused
879+
[WARNING] unused manifest key: paths
880+
[HELP] paths is a valid .cargo/config.toml key
881+
[WARNING] unused manifest key: registries
882+
[HELP] registries is a valid .cargo/config.toml key
883+
[WARNING] unused manifest key: registry
884+
[HELP] registry is a valid .cargo/config.toml key
885+
[WARNING] unused manifest key: resolver
886+
[HELP] resolver is a valid .cargo/config.toml key
887+
[WARNING] unused manifest key: source
888+
[HELP] source is a valid .cargo/config.toml key
889+
[WARNING] unused manifest key: target.cfg(unix).linker
890+
[WARNING] unused manifest key: target.foo.bar
891+
[WARNING] unused manifest key: term
892+
[HELP] term is a valid .cargo/config.toml key
893+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
843894
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
844895
845896
"#]])

0 commit comments

Comments
 (0)