Skip to content

Commit f72abee

Browse files
committed
Auto merge of #12168 - epage:lints2, r=weihanglo
fix(lints): Switch to -Zlints so stable projects can experiment ### What does this PR try to resolve? In #12115, we explored how we can let stable projects experiment with `[lints]` to provide feedback. What we settled on is switching from the `cargo-features` manifest key to the `-Z` flag as `cargo-features` always requires nightly while `-Z` only requires it when being passed in. This means a project can have a `[lints]` table and have CI / contributors run `cargo +nightly check -Zlints` when they care about warnings. ### How should we test and review this PR? Demonstrate how you test this change and guide reviewers through your PR. With a smooth review process, a pull request usually gets reviewed quicker. If you don't know how to write and run your tests, please read the guide: https://doc.crates.io/contrib/tests ### Additional information I considered reworking the code to show the user the errors they would encounter once the feature is stable but held off. I wasn't quite sure what language to use and most likely a user would have something doing error reporting, like CI, so it should be fine.
2 parents 5a39627 + 9f71a99 commit f72abee

File tree

3 files changed

+43
-81
lines changed

3 files changed

+43
-81
lines changed

src/cargo/core/features.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,6 @@ features! {
483483

484484
// Allow specifying rustflags directly in a profile
485485
(stable, workspace_inheritance, "1.64", "reference/unstable.html#workspace-inheritance"),
486-
487-
// Allow specifying rustflags directly in a profile
488-
(unstable, lints, "", "reference/unstable.html#lints"),
489486
}
490487

491488
pub struct Feature {
@@ -734,6 +731,7 @@ unstable_cli_options!(
734731
skip_rustdoc_fingerprint: bool = (HIDDEN),
735732
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
736733
msrv_policy: bool = ("Enable rust-version aware policy within cargo"),
734+
lints: bool = ("Pass `[lints]` to the linting tools"),
737735
);
738736

739737
const STABILIZED_COMPILE_PROGRESS: &str = "The progress bar is now always \
@@ -1097,6 +1095,7 @@ impl CliUnstable {
10971095
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
10981096
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
10991097
"msrv-policy" => self.msrv_policy = parse_empty(k, v)?,
1098+
"lints" => self.lints = parse_empty(k, v)?,
11001099
_ => bail!("unknown `-Z` flag specified: {}", k),
11011100
}
11021101

src/cargo/util/toml/mod.rs

+16-28
Original file line numberDiff line numberDiff line change
@@ -2045,12 +2045,7 @@ impl TomlManifest {
20452045
let mut inheritable = toml_config.package.clone().unwrap_or_default();
20462046
inheritable.update_ws_path(package_root.to_path_buf());
20472047
inheritable.update_deps(toml_config.dependencies.clone());
2048-
let lints = parse_unstable_lints(
2049-
toml_config.lints.clone(),
2050-
&features,
2051-
config,
2052-
&mut warnings,
2053-
)?;
2048+
let lints = parse_unstable_lints(toml_config.lints.clone(), config, &mut warnings)?;
20542049
let lints = verify_lints(lints)?;
20552050
inheritable.update_lints(lints);
20562051
if let Some(ws_deps) = &inheritable.dependencies {
@@ -2316,14 +2311,10 @@ impl TomlManifest {
23162311
&inherit_cell,
23172312
)?;
23182313

2319-
let lints = parse_unstable_lints::<MaybeWorkspaceLints>(
2320-
me.lints.clone(),
2321-
&features,
2322-
config,
2323-
cx.warnings,
2324-
)?
2325-
.map(|mw| mw.resolve("lints", || inherit()?.lints()))
2326-
.transpose()?;
2314+
let lints =
2315+
parse_unstable_lints::<MaybeWorkspaceLints>(me.lints.clone(), config, cx.warnings)?
2316+
.map(|mw| mw.resolve("lints", || inherit()?.lints()))
2317+
.transpose()?;
23272318
let lints = verify_lints(lints)?;
23282319
let default = TomlLints::default();
23292320
let rustflags = lints_to_rustflags(lints.as_ref().unwrap_or(&default));
@@ -2757,12 +2748,7 @@ impl TomlManifest {
27572748
let mut inheritable = toml_config.package.clone().unwrap_or_default();
27582749
inheritable.update_ws_path(root.to_path_buf());
27592750
inheritable.update_deps(toml_config.dependencies.clone());
2760-
let lints = parse_unstable_lints(
2761-
toml_config.lints.clone(),
2762-
&features,
2763-
config,
2764-
&mut warnings,
2765-
)?;
2751+
let lints = parse_unstable_lints(toml_config.lints.clone(), config, &mut warnings)?;
27662752
let lints = verify_lints(lints)?;
27672753
inheritable.update_lints(lints);
27682754
let ws_root_config = WorkspaceRootConfig::new(
@@ -2911,44 +2897,46 @@ impl TomlManifest {
29112897

29122898
fn parse_unstable_lints<T: Deserialize<'static>>(
29132899
lints: Option<toml::Value>,
2914-
features: &Features,
29152900
config: &Config,
29162901
warnings: &mut Vec<String>,
29172902
) -> CargoResult<Option<T>> {
29182903
let Some(lints) = lints else { return Ok(None); };
29192904

2920-
if !features.is_enabled(Feature::lints()) {
2921-
warn_for_feature("lints", config, warnings);
2905+
if !config.cli_unstable().lints {
2906+
warn_for_lint_feature(config, warnings);
29222907
return Ok(None);
29232908
}
29242909

29252910
lints.try_into().map(Some).map_err(|err| err.into())
29262911
}
29272912

2928-
fn warn_for_feature(name: &str, config: &Config, warnings: &mut Vec<String>) {
2913+
fn warn_for_lint_feature(config: &Config, warnings: &mut Vec<String>) {
29292914
use std::fmt::Write as _;
29302915

2916+
let key_name = "lints";
2917+
let feature_name = "lints";
2918+
29312919
let mut message = String::new();
29322920

29332921
let _ = write!(
29342922
message,
2935-
"feature `{name}` is not supported on this version of Cargo and will be ignored"
2923+
"unused manifest key `{key_name}` (may be supported in a future version)"
29362924
);
29372925
if config.nightly_features_allowed {
29382926
let _ = write!(
29392927
message,
29402928
"
29412929
2942-
consider adding `cargo-features = [\"{name}\"]` to the manifest"
2930+
consider passing `-Z{feature_name}` to enable this feature."
29432931
);
29442932
} else {
29452933
let _ = write!(
29462934
message,
29472935
"
29482936
29492937
this Cargo does not support nightly features, but if you
2950-
switch to nightly channel you can add
2951-
`cargo-features = [\"{name}\"]` to enable this feature",
2938+
switch to nightly channel you can pass
2939+
`-Z{feature_name}` to enable this feature.",
29522940
);
29532941
}
29542942
warnings.push(message);

0 commit comments

Comments
 (0)