refactor(rust): upgrade workspace crates to edition 2024 (#4424) Workspace Rust crates and toolchains targeted Edition 2021 (or omitted an explicit edition), leaving them misaligned with the project's Rust 2024 standard, preventing `let`-chains, and running `rustfmt` under the older style edition. Set `edition = "2024"` on the default and release Rust toolchains and across workspace Rust library, binary, and test targets. Flatten nested conditionals into `let`-chains and reformat sources under the 2024 style edition.
diff --git a/MODULE.bazel b/MODULE.bazel index ab53d60..985bf35 100644 --- a/MODULE.bazel +++ b/MODULE.bazel
@@ -193,14 +193,16 @@ bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True) bazel_dep(name = "rules_pkg", version = "1.2.0", dev_dependency = True) -# Rust toolchains to build the release artifacts for Linux from Linux x86_64; -# see dev/release_artifacts. They target musl, which Rust can link statically -# with its bundled rust-lld, so no C cross toolchain is needed. rust = use_extension( "@rules_rust//rust:extensions.bzl", "rust", dev_dependency = True, ) +rust.toolchain(edition = "2024") + +# Rust toolchains to build the release artifacts for Linux from Linux x86_64; +# see dev/release_artifacts. They target musl, which Rust can link statically +# with its bundled rust-lld, so no C cross toolchain is needed. rust.repository_set( # rules_rust also registers an x86_64 to x86_64 toolchain from this set, in # name order with its default ones, so the name must sort after @@ -209,7 +211,7 @@ # The default allocator library needs a C toolchain for the target platform, # but there isn't one. This one is what rules_rust uses for e.g. Windows. allocator_library = "@rules_rust//rust/private/cc:empty", - edition = "2021", + edition = "2024", exec_triple = "x86_64-unknown-linux-gnu", target_compatible_with = [ "@platforms//cpu:aarch64", @@ -302,7 +304,7 @@ # On Windows, rules_rust names toolchains after a hash of the full name: # rw-1886791612 for this one, after rust_windows_x86_64's rw-1704990954. name = "rust_release_windows", - edition = "2021", + edition = "2024", exec_triple = "x86_64-pc-windows-msvc", target_compatible_with = [ "@platforms//cpu:x86_32",
diff --git a/crates/exe_zip_maker/BUILD.bazel b/crates/exe_zip_maker/BUILD.bazel index bf307f4..de9036e 100644 --- a/crates/exe_zip_maker/BUILD.bazel +++ b/crates/exe_zip_maker/BUILD.bazel
@@ -8,7 +8,7 @@ rust_library( name = "exe_zip_maker_lib", srcs = ["src/lib.rs"], - edition = "2021", + edition = "2024", deps = [ "@crates//:lexopt", "@crates//:sha2",
diff --git a/crates/exe_zip_maker/src/lib.rs b/crates/exe_zip_maker/src/lib.rs index 17fe2c4..b7fc19e 100644 --- a/crates/exe_zip_maker/src/lib.rs +++ b/crates/exe_zip_maker/src/lib.rs
@@ -51,10 +51,10 @@ /// Creates a self-executable zip archive by prepending a preamble to a zip archive /// and substituting `%ZIP_HASH%` with the SHA-256 hash of the zip archive. pub fn create_exe_zip(preamble_path: &Path, zip_path: &Path, output_path: &Path) -> io::Result<()> { - if let Some(parent) = output_path.parent() { - if !parent.as_os_str().is_empty() { - fs::create_dir_all(parent)?; - } + if let Some(parent) = output_path.parent() + && !parent.as_os_str().is_empty() + { + fs::create_dir_all(parent)?; } let zip_hash = compute_file_sha256_hex(zip_path)?;
diff --git a/crates/shrubbery/BUILD.bazel b/crates/shrubbery/BUILD.bazel index 9d3dd1e..eba84f8 100644 --- a/crates/shrubbery/BUILD.bazel +++ b/crates/shrubbery/BUILD.bazel
@@ -8,7 +8,7 @@ rust_library( name = "shrubbery_lib", srcs = ["src/lib.rs"], - edition = "2021", + edition = "2024", deps = [ "//crates/exe_zip_maker:exe_zip_maker_lib", "//crates/zipper:zipper_lib", @@ -18,7 +18,7 @@ rust_binary( name = "shrubbery", srcs = ["src/main.rs"], - edition = "2021", + edition = "2024", visibility = ["//visibility:public"], deps = [":shrubbery_lib"], )
diff --git a/crates/zipper/BUILD.bazel b/crates/zipper/BUILD.bazel index 1c925c8..344ee2f 100644 --- a/crates/zipper/BUILD.bazel +++ b/crates/zipper/BUILD.bazel
@@ -8,7 +8,7 @@ rust_library( name = "zipper_lib", srcs = ["src/lib.rs"], - edition = "2021", + edition = "2024", deps = [ "@crates//:anyhow", "@crates//:lexopt",
diff --git a/crates/zipper/src/lib.rs b/crates/zipper/src/lib.rs index 05e0dd3..7fd5828 100644 --- a/crates/zipper/src/lib.rs +++ b/crates/zipper/src/lib.rs
@@ -9,9 +9,9 @@ use std::io::{self, BufWriter, Seek, Write}; use std::path::{Path, PathBuf}; -use anyhow::{bail, Context, Result}; +use anyhow::{Context, Result, bail}; use zip::write::SimpleFileOptions; -use zip::{CompressionMethod, DateTime, ZipWriter, ZIP64_BYTES_THR}; +use zip::{CompressionMethod, DateTime, ZIP64_BYTES_THR, ZipWriter}; /// `BufWriter` capacity for the output zip. /// @@ -51,10 +51,8 @@ legacy_external_runfiles: bool, runfiles_dir: &str, ) -> String { - if legacy_external_runfiles { - if let Some(rest) = path.strip_prefix("external/") { - return unix_join(&[runfiles_dir, rest]); - } + if legacy_external_runfiles && let Some(rest) = path.strip_prefix("external/") { + return unix_join(&[runfiles_dir, rest]); } if let Some(rest) = path.strip_prefix("../") { return unix_join(&[runfiles_dir, rest]); @@ -75,11 +73,11 @@ continue; } if part == ".." { - if let Some(&last) = out.last() { - if last != ".." { - out.pop(); - continue; - } + if let Some(&last) = out.last() + && last != ".." + { + out.pop(); + continue; } out.push(".."); } else { @@ -430,12 +428,12 @@ } let entries = read_manifest(options)?; - if let Some(parent) = options.output_zip.parent() { - // TODO: Use `Path::is_empty()` once it is stable (Rust 1.98). - if !parent.as_os_str().is_empty() { - fs::create_dir_all(parent) - .with_context(|| format!("creating directory {}", parent.display()))?; - } + // TODO: Use `Path::is_empty()` once it is stable (Rust 1.98). + if let Some(parent) = options.output_zip.parent() + && !parent.as_os_str().is_empty() + { + fs::create_dir_all(parent) + .with_context(|| format!("creating directory {}", parent.display()))?; } let out_file = File::create(&options.output_zip)
diff --git a/tests/exe_zip_maker/BUILD.bazel b/tests/exe_zip_maker/BUILD.bazel index 4a9190d..7453d25 100644 --- a/tests/exe_zip_maker/BUILD.bazel +++ b/tests/exe_zip_maker/BUILD.bazel
@@ -8,6 +8,7 @@ name = "exe_zip_maker_test", size = "small", srcs = ["exe_zip_maker_test.rs"], + edition = "2024", deps = [ "//crates/exe_zip_maker:exe_zip_maker_lib", ],
diff --git a/tests/exe_zip_maker/exe_zip_maker_test.rs b/tests/exe_zip_maker/exe_zip_maker_test.rs index 5f891dd..141b444 100644 --- a/tests/exe_zip_maker/exe_zip_maker_test.rs +++ b/tests/exe_zip_maker/exe_zip_maker_test.rs
@@ -3,8 +3,8 @@ use std::path::PathBuf; use exe_zip_maker_lib::{ - compute_file_sha256_hex, create_exe_zip, parse_args, replace_bytes, run_cli, Options, - PLACEHOLDER, + Options, PLACEHOLDER, compute_file_sha256_hex, create_exe_zip, parse_args, replace_bytes, + run_cli, }; #[test]
diff --git a/tests/shrubbery/BUILD.bazel b/tests/shrubbery/BUILD.bazel index de800e5..181a607 100644 --- a/tests/shrubbery/BUILD.bazel +++ b/tests/shrubbery/BUILD.bazel
@@ -11,6 +11,7 @@ data = [ "//crates/shrubbery", ], + edition = "2024", env = { "SHRUBBERY_BIN": "$(rootpath //crates/shrubbery)", },
diff --git a/tests/shrubbery/shrubbery_test.rs b/tests/shrubbery/shrubbery_test.rs index 2519b35..8d94aab 100644 --- a/tests/shrubbery/shrubbery_test.rs +++ b/tests/shrubbery/shrubbery_test.rs
@@ -3,7 +3,7 @@ use std::path::PathBuf; use std::process::Command; -use shrubbery_lib::{run_cli, SUBCOMMANDS}; +use shrubbery_lib::{SUBCOMMANDS, run_cli}; fn temp_test_dir(prefix: &str) -> PathBuf { let base = env::var_os("TEST_TMPDIR")
diff --git a/tests/zipper/BUILD.bazel b/tests/zipper/BUILD.bazel index 8c409e0..3eba80c 100644 --- a/tests/zipper/BUILD.bazel +++ b/tests/zipper/BUILD.bazel
@@ -8,6 +8,7 @@ name = "zipper_test", size = "small", srcs = ["zipper_test.rs"], + edition = "2024", deps = [ "//crates/zipper:zipper_lib", "@crates//:zip",
diff --git a/tests/zipper/zipper_test.rs b/tests/zipper/zipper_test.rs index c779cb6..755ac9e 100644 --- a/tests/zipper/zipper_test.rs +++ b/tests/zipper/zipper_test.rs
@@ -6,7 +6,7 @@ use std::sync::atomic::{AtomicUsize, Ordering}; use zip::{CompressionMethod, DateTime, ZipArchive}; -use zipper_lib::{create_zip, parse_args, relative_path, Cli, ZipOptions}; +use zipper_lib::{Cli, ZipOptions, create_zip, parse_args, relative_path}; static TEST_COUNTER: AtomicUsize = AtomicUsize::new(0); @@ -674,10 +674,12 @@ } let link_path = extract_dir.join("runfiles/my_ws/path/to/link"); - assert!(fs::symlink_metadata(&link_path) - .expect("link_path should exist") - .file_type() - .is_symlink()); + assert!( + fs::symlink_metadata(&link_path) + .expect("link_path should exist") + .file_type() + .is_symlink() + ); assert_eq!( fs::read_link(&link_path).expect("link_path should be a symlink"), PathBuf::from("../../target/path") @@ -688,10 +690,12 @@ ); let link2_path = extract_dir.join("runfiles/my_ws/same_dir_link"); - assert!(fs::symlink_metadata(&link2_path) - .expect("link2_path should exist") - .file_type() - .is_symlink()); + assert!( + fs::symlink_metadata(&link2_path) + .expect("link2_path should exist") + .file_type() + .is_symlink() + ); assert_eq!( fs::read_link(&link2_path).expect("link2_path should be a symlink"), PathBuf::from("same_dir_target")