From 8608a3f637871c157b030f5661231f8261ca9797 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 26 Apr 2025 14:08:36 -0700 Subject: [PATCH 1/2] lint-docs: Don't hard-code the valid editions This removes the hard-coded list of edition support in the lint-docs tool, and instead just assumes the edition attribute is something valid. There isn't a real reason to have this, as rustc will error if given a wrong number. This should be easier to maintain going forward. --- src/tools/lint-docs/src/lib.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/tools/lint-docs/src/lib.rs b/src/tools/lint-docs/src/lib.rs index 9fd33e23204..7e00697a0bd 100644 --- a/src/tools/lint-docs/src/lib.rs +++ b/src/tools/lint-docs/src/lib.rs @@ -444,21 +444,15 @@ impl<'a> LintExtractor<'a> { fs::write(&tempfile, source) .map_err(|e| format!("failed to write {}: {}", tempfile.display(), e))?; let mut cmd = Command::new(self.rustc_path); - if options.contains(&"edition2024") { - cmd.arg("--edition=2024"); - cmd.arg("-Zunstable-options"); - } else if options.contains(&"edition2021") { - cmd.arg("--edition=2021"); - } else if options.contains(&"edition2018") { - cmd.arg("--edition=2018"); - } else if options.contains(&"edition2015") { - cmd.arg("--edition=2015"); - } else if options.contains(&"edition") { - panic!("lint-docs: unknown edition"); - } else { + let edition = options + .iter() + .filter_map(|opt| opt.strip_prefix("edition")) + .next() // defaults to latest edition - cmd.arg("--edition=2021"); - } + .unwrap_or("2021"); + cmd.arg(format!("--edition={edition}")); + // Just in case this is an unstable edition. + cmd.arg("-Zunstable-options"); cmd.arg("--error-format=json"); cmd.arg("--target").arg(self.rustc_target); if let Some(target_linker) = self.rustc_linker { From 1f108fe08a906b49026cd0aa324b3f9381c052c1 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sat, 26 Apr 2025 14:08:58 -0700 Subject: [PATCH 2/2] Update lint-docs to default to Rust 2024 This updates the lint-docs tool to default to the 2024 edition. The lint docs are supposed to illustrate the code with the latest edition, and I just forgot to update this in https://github.com/rust-lang/rust/pull/133349. Some docs needed to add the `edition` attribute since they were assuming a particular edition, but were missing the explicit annotation. --- compiler/rustc_lint/src/builtin.rs | 2 +- compiler/rustc_lint/src/impl_trait_overcaptures.rs | 2 +- compiler/rustc_lint_defs/src/builtin.rs | 4 ++-- src/tools/lint-docs/src/lib.rs | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_lint/src/builtin.rs b/compiler/rustc_lint/src/builtin.rs index e65f4beab24..41b43f64798 100644 --- a/compiler/rustc_lint/src/builtin.rs +++ b/compiler/rustc_lint/src/builtin.rs @@ -948,7 +948,7 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + /// ```rust,compile_fail,edition2021 /// #[no_mangle] /// const FOO: i32 = 5; /// ``` diff --git a/compiler/rustc_lint/src/impl_trait_overcaptures.rs b/compiler/rustc_lint/src/impl_trait_overcaptures.rs index f1dc420aa3c..7f4789ad0d9 100644 --- a/compiler/rustc_lint/src/impl_trait_overcaptures.rs +++ b/compiler/rustc_lint/src/impl_trait_overcaptures.rs @@ -41,7 +41,7 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + /// ```rust,compile_fail,edition2021 /// # #![deny(impl_trait_overcaptures)] /// # use std::fmt::Display; /// let mut x = vec![]; diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 17d501c5730..03b8112938c 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -1424,7 +1424,7 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + /// ```rust,compile_fail,edition2021 /// macro_rules! foo { /// () => {}; /// ($name) => { }; @@ -4128,7 +4128,7 @@ declare_lint! { /// /// ### Example /// - /// ```rust,compile_fail + /// ```rust,compile_fail,edition2021 /// #![deny(dependency_on_unit_never_type_fallback)] /// fn main() { /// if true { diff --git a/src/tools/lint-docs/src/lib.rs b/src/tools/lint-docs/src/lib.rs index 7e00697a0bd..d6c69d39e17 100644 --- a/src/tools/lint-docs/src/lib.rs +++ b/src/tools/lint-docs/src/lib.rs @@ -449,7 +449,7 @@ impl<'a> LintExtractor<'a> { .filter_map(|opt| opt.strip_prefix("edition")) .next() // defaults to latest edition - .unwrap_or("2021"); + .unwrap_or("2024"); cmd.arg(format!("--edition={edition}")); // Just in case this is an unstable edition. cmd.arg("-Zunstable-options");