Skip to content

Commit 1292915

Browse files
committed
Auto merge of #7575 - orium:fix-unused-warnings, r=alexcrichton
Fix unused configuration key warning for a few keys under `build`. Recently cargo started to warn about configuration keys that he doesn't know about. However, there are a few keys under `build` that were used in a dynamic way (`rustc`, `rustdoc`, and `rustc_wrapper`) by `Config::maybe_get_tool()`. Since these keys are not known to exist when `Config` is deserialized, cargo was emitting unused warnings. This commit makes those config keys explicit. Note that by doing so there is a small breaking change: before it was possible to have `build.rustc_wrapper` in the configuration file (even though the documented key uses kebak-case), and now that key will be ignored. (Good thing we have warnings for unrecognized keys!)
2 parents f612284 + e509851 commit 1292915

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed

src/cargo/ops/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
//! to print at the same time).
1616
//!
1717
//! Cargo begins a normal `cargo check` operation with itself set as a proxy
18-
//! for rustc by setting `rustc_wrapper` in the build config. When
18+
//! for rustc by setting `primary_unit_rustc` in the build config. When
1919
//! cargo launches rustc to check a crate, it is actually launching itself.
2020
//! The `FIX_ENV` environment variable is set so that cargo knows it is in
2121
//! fix-proxy-mode.

src/cargo/util/config/mod.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl Config {
216216
/// Gets the path to the `rustdoc` executable.
217217
pub fn rustdoc(&self) -> CargoResult<&Path> {
218218
self.rustdoc
219-
.try_borrow_with(|| self.get_tool("rustdoc"))
219+
.try_borrow_with(|| Ok(self.get_tool("rustdoc", &self.build_config()?.rustdoc)))
220220
.map(AsRef::as_ref)
221221
}
222222

@@ -227,9 +227,9 @@ impl Config {
227227
.join(".rustc_info.json")
228228
.into_path_unlocked()
229229
});
230-
let wrapper = self.maybe_get_tool("rustc_wrapper")?;
230+
let wrapper = self.maybe_get_tool("rustc_wrapper", &self.build_config()?.rustc_wrapper);
231231
Rustc::new(
232-
self.get_tool("rustc")?,
232+
self.get_tool("rustc", &self.build_config()?.rustc),
233233
wrapper,
234234
&self
235235
.home()
@@ -853,47 +853,34 @@ impl Config {
853853
Ok(())
854854
}
855855

856-
/// Looks for a path for `tool` in an environment variable or config path, and returns `None`
857-
/// if it's not present.
858-
fn maybe_get_tool(&self, tool: &str) -> CargoResult<Option<PathBuf>> {
859-
let var = tool
860-
.chars()
861-
.flat_map(|c| c.to_uppercase())
862-
.collect::<String>();
863-
if let Some(tool_path) = env::var_os(&var) {
864-
let maybe_relative = match tool_path.to_str() {
865-
Some(s) => s.contains('/') || s.contains('\\'),
866-
None => false,
867-
};
868-
let path = if maybe_relative {
869-
self.cwd.join(tool_path)
870-
} else {
871-
PathBuf::from(tool_path)
872-
};
873-
return Ok(Some(path));
874-
}
875-
876-
// For backwards compatibility we allow both snake_case config paths as well as the
877-
// idiomatic kebab-case paths.
878-
let config_paths = [
879-
format!("build.{}", tool),
880-
format!("build.{}", tool.replace('_', "-")),
881-
];
856+
/// Looks for a path for `tool` in an environment variable or the given config, and returns
857+
/// `None` if it's not present.
858+
fn maybe_get_tool(&self, tool: &str, from_config: &Option<PathBuf>) -> Option<PathBuf> {
859+
let var = tool.to_uppercase();
882860

883-
for config_path in &config_paths {
884-
if let Some(tool_path) = self.get_path(&config_path)? {
885-
return Ok(Some(tool_path.val));
861+
match env::var_os(&var) {
862+
Some(tool_path) => {
863+
let maybe_relative = match tool_path.to_str() {
864+
Some(s) => s.contains('/') || s.contains('\\'),
865+
None => false,
866+
};
867+
let path = if maybe_relative {
868+
self.cwd.join(tool_path)
869+
} else {
870+
PathBuf::from(tool_path)
871+
};
872+
Some(path)
886873
}
887-
}
888874

889-
Ok(None)
875+
None => from_config.clone(),
876+
}
890877
}
891878

892879
/// Looks for a path for `tool` in an environment variable or config path, defaulting to `tool`
893880
/// as a path.
894-
pub fn get_tool(&self, tool: &str) -> CargoResult<PathBuf> {
895-
self.maybe_get_tool(tool)
896-
.map(|t| t.unwrap_or_else(|| PathBuf::from(tool)))
881+
fn get_tool(&self, tool: &str, from_config: &Option<PathBuf>) -> PathBuf {
882+
self.maybe_get_tool(tool, from_config)
883+
.unwrap_or_else(|| PathBuf::from(tool))
897884
}
898885

899886
pub fn jobserver_from_env(&self) -> Option<&jobserver::Client> {
@@ -1473,6 +1460,9 @@ pub struct CargoBuildConfig {
14731460
pub jobs: Option<u32>,
14741461
pub rustflags: Option<StringList>,
14751462
pub rustdocflags: Option<StringList>,
1463+
pub rustc_wrapper: Option<PathBuf>,
1464+
pub rustc: Option<PathBuf>,
1465+
pub rustdoc: Option<PathBuf>,
14761466
}
14771467

14781468
/// A type to deserialize a list of strings from a toml file.

0 commit comments

Comments
 (0)