]> git.lizzy.rs Git - rust.git/blob - src/tools/tidy/src/edition.rs
Auto merge of #90934 - JohnTitor:rollup-5soqo0j, r=JohnTitor
[rust.git] / src / tools / tidy / src / edition.rs
1 //! Tidy check to ensure that crate `edition` is '2018' or '2021'.
2
3 use std::path::Path;
4
5 fn is_edition_2018(mut line: &str) -> bool {
6     line = line.trim();
7     line == "edition = \"2018\""
8 }
9
10 fn is_edition_2021(mut line: &str) -> bool {
11     line = line.trim();
12     line == "edition = \"2021\""
13 }
14
15 pub fn check(path: &Path, bad: &mut bool) {
16     super::walk(
17         path,
18         &mut |path| super::filter_dirs(path) || path.ends_with("src/test"),
19         &mut |entry, contents| {
20             let file = entry.path();
21             let filename = file.file_name().unwrap();
22             if filename != "Cargo.toml" {
23                 return;
24             }
25
26             // Library crates are not yet ready to migrate to 2021.
27             if path.components().any(|c| c.as_os_str() == "library") {
28                 let has = contents.lines().any(is_edition_2018);
29                 if !has {
30                     tidy_error!(
31                         bad,
32                         "{} doesn't have `edition = \"2018\"` on a separate line",
33                         file.display()
34                     );
35                 }
36             } else {
37                 let is_2021 = contents.lines().any(is_edition_2021);
38                 if !is_2021 {
39                     tidy_error!(
40                         bad,
41                         "{} doesn't have `edition = \"2021\"` on a separate line",
42                         file.display()
43                     );
44                 }
45             }
46         },
47     );
48 }