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