tidy/edition.rs
1//! Tidy check to ensure that crate `edition` is '2021' or '2024'.
2
3use std::path::Path;
4
5use crate::walk::{filter_dirs, walk};
6
7pub fn check(path: &Path, bad: &mut bool) {
8 walk(path, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
9 let file = entry.path();
10 let filename = file.file_name().unwrap();
11 if filename != "Cargo.toml" {
12 return;
13 }
14
15 let is_current_edition = contents
16 .lines()
17 .any(|line| line.trim() == "edition = \"2021\"" || line.trim() == "edition = \"2024\"");
18
19 let is_workspace = contents.lines().any(|line| line.trim() == "[workspace]");
20 let is_package = contents.lines().any(|line| line.trim() == "[package]");
21 assert!(is_workspace || is_package);
22
23 // Check that all packages use the 2021 edition. Virtual workspaces don't allow setting an
24 // edition, so these shouldn't be checked.
25 if is_package && !is_current_edition {
26 tidy_error!(
27 bad,
28 "{} doesn't have `edition = \"2021\"` or `edition = \"2024\"` on a separate line",
29 file.display()
30 );
31 }
32 });
33}