Skip to content

Commit 7b5111a

Browse files
committed
change TidyFlags to Option and add git context
1 parent 5bb1bc3 commit 7b5111a

31 files changed

+278
-243
lines changed

src/tools/features-status-dump/src/main.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::PathBuf;
55

66
use anyhow::{Context, Result};
77
use clap::Parser;
8-
use tidy::TidyFlags;
98
use tidy::diagnostics::RunningCheck;
109
use tidy::features::{Feature, collect_lang_features, collect_lib_features};
1110

@@ -32,7 +31,7 @@ fn main() -> Result<()> {
3231
let Cli { compiler_path, library_path, output_path } = Cli::parse();
3332

3433
let lang_features_status = collect_lang_features(&compiler_path, &mut RunningCheck::new_noop());
35-
let lib_features_status = collect_lib_features(&library_path, TidyFlags::default())
34+
let lib_features_status = collect_lib_features(&library_path, None)
3635
.into_iter()
3736
.filter(|&(ref name, _)| !lang_features_status.contains_key(name))
3837
.collect();

src/tools/replace-version-placeholder/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ fn main() {
1616
&root_path.join("src/doc/rustc"),
1717
&root_path.join("src/doc/rustdoc"),
1818
],
19+
None,
1920
|path, _is_dir| walk::filter_dirs(path),
2021
&mut |entry, contents| {
2122
if !contents.contains(VERSION_PLACEHOLDER) {

src/tools/tidy/src/alphabetical.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use std::fmt::Display;
2424
use std::iter::Peekable;
2525
use std::path::Path;
2626

27-
use crate::TidyFlags;
27+
use crate::TidyCtx;
2828
use crate::diagnostics::{CheckId, DiagCtx, RunningCheck};
2929
use crate::walk::{filter_dirs, walk};
3030

@@ -131,13 +131,13 @@ fn check_lines<'a>(
131131
}
132132
}
133133

134-
pub fn check(path: &Path, tidy_flags: TidyFlags, diag_ctx: DiagCtx) {
134+
pub fn check(path: &Path, tidy_ctx: Option<&TidyCtx>, diag_ctx: DiagCtx) {
135135
let mut check = diag_ctx.start_check(CheckId::new("alphabetical").path(path));
136136

137137
let skip =
138138
|path: &_, _is_dir| filter_dirs(path) || path.ends_with("tidy/src/alphabetical/tests.rs");
139139

140-
walk(path, tidy_flags, skip, &mut |entry, contents| {
140+
walk(path, tidy_ctx, skip, &mut |entry, contents| {
141141
let file = &entry.path().display();
142142
let lines = contents.lines().enumerate();
143143
check_lines(file, lines, &mut check)

src/tools/tidy/src/bins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ mod os_impl {
2929
use std::process::{Command, Stdio};
3030

3131
#[cfg(unix)]
32-
use crate::TidyFlags;
32+
use crate::TidyCtx;
3333
use crate::walk::{filter_dirs, walk_no_read};
3434

3535
enum FilesystemSupport {
@@ -112,7 +112,7 @@ mod os_impl {
112112
}
113113

114114
#[cfg(unix)]
115-
pub fn check(path: &Path, tidy_flags: TidyFlags, diag_ctx: DiagCtx) {
115+
pub fn check(path: &Path, tidy_ctx: Option<&TidyCtx>, diag_ctx: DiagCtx) {
116116
let mut check = diag_ctx.start_check("bins");
117117

118118
use std::ffi::OsStr;
@@ -129,7 +129,7 @@ mod os_impl {
129129
// (e.g. using `git ls-files`).
130130
walk_no_read(
131131
&[path],
132-
tidy_flags,
132+
tidy_ctx,
133133
|path, _is_dir| {
134134
filter_dirs(path)
135135
|| path.ends_with("src/etc")

src/tools/tidy/src/debug_artifacts.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22
33
use std::path::Path;
44

5-
use crate::TidyFlags;
5+
use crate::TidyCtx;
66
use crate::diagnostics::{CheckId, DiagCtx};
77
use crate::walk::{filter_dirs, filter_not_rust, walk};
88

99
const GRAPHVIZ_POSTFLOW_MSG: &str = "`borrowck_graphviz_postflow` attribute in test";
1010

11-
pub fn check(test_dir: &Path, tidy_flags: TidyFlags, diag_ctx: DiagCtx) {
11+
pub fn check(test_dir: &Path, tidy_ctx: Option<&TidyCtx>, diag_ctx: DiagCtx) {
1212
let mut check = diag_ctx.start_check(CheckId::new("debug_artifacts").path(test_dir));
1313

1414
walk(
1515
test_dir,
16-
tidy_flags,
16+
tidy_ctx,
1717
|path, _is_dir| filter_dirs(path) || filter_not_rust(path),
1818
&mut |entry, contents| {
1919
for (i, line) in contents.lines().enumerate() {

src/tools/tidy/src/deps.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use build_helper::ci::CiEnv;
99
use cargo_metadata::semver::Version;
1010
use cargo_metadata::{Metadata, Package, PackageId};
1111

12-
use crate::TidyFlags;
12+
use crate::TidyCtx;
1313
use crate::diagnostics::{DiagCtx, RunningCheck};
1414

1515
#[path = "../../../bootstrap/src/utils/proc_macro_deps.rs"]
@@ -667,12 +667,13 @@ const PERMITTED_CRANELIFT_DEPENDENCIES: &[&str] = &[
667667
///
668668
/// `root` is path to the directory with the root `Cargo.toml` (for the workspace). `cargo` is path
669669
/// to the cargo executable.
670-
pub fn check(root: &Path, cargo: &Path, tidy_flags: TidyFlags, diag_ctx: DiagCtx) {
670+
pub fn check(root: &Path, cargo: &Path, tidy_ctx: Option<&TidyCtx>, diag_ctx: DiagCtx) {
671671
let mut check = diag_ctx.start_check("deps");
672+
let bless = tidy_ctx.map(|flags| flags.bless).unwrap_or(false);
672673

673674
let mut checked_runtime_licenses = false;
674675

675-
check_proc_macro_dep_list(root, cargo, tidy_flags.bless, &mut check);
676+
check_proc_macro_dep_list(root, cargo, bless, &mut check);
676677

677678
for &WorkspaceInfo { path, exceptions, crates_and_deps, submodules } in WORKSPACES {
678679
if has_missing_submodule(root, submodules) {

src/tools/tidy/src/edition.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
33
use std::path::Path;
44

5-
use crate::TidyFlags;
5+
use crate::TidyCtx;
66
use crate::diagnostics::{CheckId, DiagCtx};
77
use crate::walk::{filter_dirs, walk};
88

9-
pub fn check(path: &Path, tidy_flags: TidyFlags, diag_ctx: DiagCtx) {
9+
pub fn check(path: &Path, tidy_ctx: Option<&TidyCtx>, diag_ctx: DiagCtx) {
1010
let mut check = diag_ctx.start_check(CheckId::new("edition").path(path));
11-
walk(path, tidy_flags, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
11+
walk(path, tidy_ctx, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
1212
let file = entry.path();
1313
let filename = file.file_name().unwrap();
1414
if filename != "Cargo.toml" {

src/tools/tidy/src/error_codes.rs

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::path::Path;
2222

2323
use regex::Regex;
2424

25-
use crate::TidyFlags;
25+
use crate::TidyCtx;
2626
use crate::diagnostics::{DiagCtx, RunningCheck};
2727
use crate::walk::{filter_dirs, walk, walk_many};
2828

@@ -41,7 +41,7 @@ pub fn check(
4141
root_path: &Path,
4242
search_paths: &[&Path],
4343
ci_info: &crate::CiInfo,
44-
tidy_flags: TidyFlags,
44+
tidy_ctx: Option<&TidyCtx>,
4545
diag_ctx: DiagCtx,
4646
) {
4747
let mut check = diag_ctx.start_check("error_codes");
@@ -55,13 +55,13 @@ pub fn check(
5555
check.verbose_msg(format!("Highest error code: `{}`", error_codes.iter().max().unwrap()));
5656

5757
// Stage 2: check list has docs
58-
let no_longer_emitted = check_error_codes_docs(root_path, &error_codes, &mut check, tidy_flags);
58+
let no_longer_emitted = check_error_codes_docs(root_path, &error_codes, &mut check, tidy_ctx);
5959

6060
// Stage 3: check list has UI tests
6161
check_error_codes_tests(root_path, &error_codes, &mut check, &no_longer_emitted);
6262

6363
// Stage 4: check list is emitted by compiler
64-
check_error_codes_used(search_paths, &error_codes, &mut check, &no_longer_emitted, tidy_flags);
64+
check_error_codes_used(search_paths, &error_codes, &mut check, &no_longer_emitted, tidy_ctx);
6565
}
6666

6767
fn check_removed_error_code_explanation(ci_info: &crate::CiInfo, check: &mut RunningCheck) {
@@ -161,13 +161,13 @@ fn check_error_codes_docs(
161161
root_path: &Path,
162162
error_codes: &[String],
163163
check: &mut RunningCheck,
164-
tidy_flags: TidyFlags,
164+
tidy_ctx: Option<&TidyCtx>,
165165
) -> Vec<String> {
166166
let docs_path = root_path.join(Path::new(ERROR_DOCS_PATH));
167167

168168
let mut no_longer_emitted_codes = Vec::new();
169169

170-
walk(&docs_path, tidy_flags, |_, _| false, &mut |entry, contents| {
170+
walk(&docs_path, tidy_ctx, |_, _| false, &mut |entry, contents| {
171171
let path = entry.path();
172172

173173
// Error if the file isn't markdown.
@@ -339,48 +339,43 @@ fn check_error_codes_used(
339339
error_codes: &[String],
340340
check: &mut RunningCheck,
341341
no_longer_emitted: &[String],
342-
tidy_flags: TidyFlags,
342+
tidy_ctx: Option<&TidyCtx>,
343343
) {
344344
// Search for error codes in the form `E0123`.
345345
let regex = Regex::new(r#"\bE\d{4}\b"#).unwrap();
346346

347347
let mut found_codes = Vec::new();
348348

349-
walk_many(
350-
search_paths,
351-
tidy_flags,
352-
|path, _is_dir| filter_dirs(path),
353-
&mut |entry, contents| {
354-
let path = entry.path();
355-
356-
// Return early if we aren't looking at a source file.
357-
if path.extension() != Some(OsStr::new("rs")) {
358-
return;
359-
}
349+
walk_many(search_paths, tidy_ctx, |path, _is_dir| filter_dirs(path), &mut |entry, contents| {
350+
let path = entry.path();
360351

361-
for line in contents.lines() {
362-
// We want to avoid parsing error codes in comments.
363-
if line.trim_start().starts_with("//") {
364-
continue;
365-
}
352+
// Return early if we aren't looking at a source file.
353+
if path.extension() != Some(OsStr::new("rs")) {
354+
return;
355+
}
366356

367-
for cap in regex.captures_iter(line) {
368-
if let Some(error_code) = cap.get(0) {
369-
let error_code = error_code.as_str().to_owned();
357+
for line in contents.lines() {
358+
// We want to avoid parsing error codes in comments.
359+
if line.trim_start().starts_with("//") {
360+
continue;
361+
}
370362

371-
if !error_codes.contains(&error_code) {
372-
// This error code isn't properly defined, we must error.
373-
check.error(format!("Error code `{error_code}` is used in the compiler but not defined and documented in `compiler/rustc_error_codes/src/lib.rs`."));
374-
continue;
375-
}
363+
for cap in regex.captures_iter(line) {
364+
if let Some(error_code) = cap.get(0) {
365+
let error_code = error_code.as_str().to_owned();
376366

377-
// This error code can now be marked as used.
378-
found_codes.push(error_code);
367+
if !error_codes.contains(&error_code) {
368+
// This error code isn't properly defined, we must error.
369+
check.error(format!("Error code `{error_code}` is used in the compiler but not defined and documented in `compiler/rustc_error_codes/src/lib.rs`."));
370+
continue;
379371
}
372+
373+
// This error code can now be marked as used.
374+
found_codes.push(error_code);
380375
}
381376
}
382-
},
383-
);
377+
}
378+
});
384379

385380
for code in error_codes {
386381
if !found_codes.contains(code) && !no_longer_emitted.contains(code) {

src/tools/tidy/src/extra_checks/mod.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use std::{fmt, fs, io};
2626
use build_helper::git::get_git_untracked_files;
2727

2828
use crate::diagnostics::DiagCtx;
29-
use crate::{CiInfo, TidyFlags};
29+
use crate::{CiInfo, TidyCtx};
3030

3131
mod rustdoc_js;
3232

@@ -56,7 +56,7 @@ pub fn check(
5656
cargo: &Path,
5757
extra_checks: Option<&str>,
5858
pos_args: &[String],
59-
tidy_flags: TidyFlags,
59+
tidy_ctx: Option<&TidyCtx>,
6060
diag_ctx: DiagCtx,
6161
) {
6262
let mut check = diag_ctx.start_check("extra_checks");
@@ -71,7 +71,7 @@ pub fn check(
7171
cargo,
7272
extra_checks,
7373
pos_args,
74-
tidy_flags,
74+
tidy_ctx,
7575
) {
7676
check.error(e);
7777
}
@@ -87,10 +87,11 @@ fn check_impl(
8787
cargo: &Path,
8888
extra_checks: Option<&str>,
8989
pos_args: &[String],
90-
tidy_flags: TidyFlags,
90+
tidy_ctx: Option<&TidyCtx>,
9191
) -> Result<(), Error> {
9292
let show_diff =
9393
std::env::var("TIDY_PRINT_DIFF").is_ok_and(|v| v.eq_ignore_ascii_case("true") || v == "1");
94+
let bless = tidy_ctx.map(|flags| flags.bless).unwrap_or(false);
9495

9596
// Split comma-separated args up
9697
let mut lint_args = match extra_checks {
@@ -130,7 +131,7 @@ fn check_impl(
130131
}
131132

132133
let rerun_with_bless = |mode: &str, action: &str| {
133-
if !tidy_flags.bless {
134+
if !bless {
134135
eprintln!("rerun tidy with `--extra-checks={mode} --bless` to {action}");
135136
}
136137
};
@@ -159,7 +160,7 @@ fn check_impl(
159160

160161
if python_lint {
161162
let py_path = py_path.as_ref().unwrap();
162-
let args: &[&OsStr] = if tidy_flags.bless {
163+
let args: &[&OsStr] = if bless {
163164
eprintln!("linting python files and applying suggestions");
164165
&["check".as_ref(), "--fix".as_ref()]
165166
} else {
@@ -169,7 +170,7 @@ fn check_impl(
169170

170171
let res = run_ruff(root_path, outdir, py_path, &cfg_args, &file_args, args);
171172

172-
if res.is_err() && show_diff && !tidy_flags.bless {
173+
if res.is_err() && show_diff && !bless {
173174
eprintln!("\npython linting failed! Printing diff suggestions:");
174175

175176
let diff_res = run_ruff(
@@ -191,7 +192,7 @@ fn check_impl(
191192

192193
if python_fmt {
193194
let mut args: Vec<&OsStr> = vec!["format".as_ref()];
194-
if tidy_flags.bless {
195+
if bless {
195196
eprintln!("formatting python files");
196197
} else {
197198
eprintln!("checking python file formatting");
@@ -201,7 +202,7 @@ fn check_impl(
201202
let py_path = py_path.as_ref().unwrap();
202203
let res = run_ruff(root_path, outdir, py_path, &cfg_args, &file_args, &args);
203204

204-
if res.is_err() && !tidy_flags.bless {
205+
if res.is_err() && !bless {
205206
if show_diff {
206207
eprintln!("\npython formatting does not match! Printing diff:");
207208

@@ -227,7 +228,7 @@ fn check_impl(
227228
let config_path = root_path.join(".clang-format");
228229
let config_file_arg = format!("file:{}", config_path.display());
229230
cfg_args_clang_format.extend(&["--style".as_ref(), config_file_arg.as_ref()]);
230-
if tidy_flags.bless {
231+
if bless {
231232
eprintln!("formatting C++ files");
232233
cfg_args_clang_format.push("-i".as_ref());
233234
} else {
@@ -247,7 +248,7 @@ fn check_impl(
247248
let args = merge_args(&cfg_args_clang_format, &file_args_clang_format);
248249
let res = py_runner(py_path.as_ref().unwrap(), false, None, "clang-format", &args);
249250

250-
if res.is_err() && show_diff && !tidy_flags.bless {
251+
if res.is_err() && show_diff && !bless {
251252
eprintln!("\nclang-format linting failed! Printing diff suggestions:");
252253

253254
let mut cfg_args_clang_format_diff = cfg_args.clone();
@@ -312,7 +313,7 @@ fn check_impl(
312313

313314
args.extend_from_slice(SPELLCHECK_DIRS);
314315

315-
if tidy_flags.bless {
316+
if bless {
316317
eprintln!("spellchecking files and fixing typos");
317318
args.push("--write-changes");
318319
} else {
@@ -330,17 +331,17 @@ fn check_impl(
330331
}
331332

332333
if js_lint {
333-
if tidy_flags.bless {
334+
if bless {
334335
eprintln!("linting javascript files");
335336
} else {
336337
eprintln!("linting javascript files and applying suggestions");
337338
}
338-
let res = rustdoc_js::lint(outdir, librustdoc_path, tools_path, tidy_flags);
339+
let res = rustdoc_js::lint(outdir, librustdoc_path, tools_path, tidy_ctx);
339340
if res.is_err() {
340341
rerun_with_bless("js:lint", "apply eslint suggestions");
341342
}
342343
res?;
343-
rustdoc_js::es_check(outdir, librustdoc_path, tidy_flags)?;
344+
rustdoc_js::es_check(outdir, librustdoc_path, tidy_ctx)?;
344345
}
345346

346347
if js_typecheck {

0 commit comments

Comments
 (0)