Skip to content

Commit 97421c9

Browse files
committed
fix(manifest): Point out when a key belongs to config
Inspired by #16251
1 parent 54b68f2 commit 97421c9

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
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: 12 additions & 1 deletion
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,8 +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));
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();
2901+
}
2902+
warnings.push(message);
28922903
}
28932904
}
28942905

tests/testsuite/bad_config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,26 +853,43 @@ quiet = false
853853
p.cargo("check")
854854
.with_stderr_data(str![[r#"
855855
[WARNING] unused manifest key: alias
856+
[HELP] alias is a valid .cargo/config.toml key
856857
[WARNING] unused manifest key: build
858+
[HELP] build is a valid .cargo/config.toml key
857859
[WARNING] unused manifest key: cache
860+
[HELP] cache is a valid .cargo/config.toml key
858861
[WARNING] unused manifest key: cargo-new
862+
[HELP] cargo-new is a valid .cargo/config.toml key
859863
[WARNING] unused manifest key: credential-alias
864+
[HELP] credential-alias is a valid .cargo/config.toml key
860865
[WARNING] unused manifest key: doc
866+
[HELP] doc is a valid .cargo/config.toml key
861867
[WARNING] unused manifest key: env
868+
[HELP] env is a valid .cargo/config.toml key
862869
[WARNING] unused manifest key: future-incompat-report
870+
[HELP] future-incompat-report is a valid .cargo/config.toml key
863871
[WARNING] unused manifest key: http
872+
[HELP] http is a valid .cargo/config.toml key
864873
[WARNING] unused manifest key: install
874+
[HELP] install is a valid .cargo/config.toml key
865875
[WARNING] unused manifest key: lib.build
866876
[WARNING] unused manifest key: net
877+
[HELP] net is a valid .cargo/config.toml key
867878
[WARNING] unused manifest key: package.unused
868879
[WARNING] unused manifest key: paths
880+
[HELP] paths is a valid .cargo/config.toml key
869881
[WARNING] unused manifest key: registries
882+
[HELP] registries is a valid .cargo/config.toml key
870883
[WARNING] unused manifest key: registry
884+
[HELP] registry is a valid .cargo/config.toml key
871885
[WARNING] unused manifest key: resolver
886+
[HELP] resolver is a valid .cargo/config.toml key
872887
[WARNING] unused manifest key: source
888+
[HELP] source is a valid .cargo/config.toml key
873889
[WARNING] unused manifest key: target.cfg(unix).linker
874890
[WARNING] unused manifest key: target.foo.bar
875891
[WARNING] unused manifest key: term
892+
[HELP] term is a valid .cargo/config.toml key
876893
[CHECKING] foo v0.1.0 ([ROOT]/foo)
877894
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
878895

0 commit comments

Comments
 (0)