Skip to content

Commit 82d729c

Browse files
clippy_dev: Parsing revamp part 2/N (#15921)
Based on #15866 This adds a parsing context that can allocate from an arena. Ultimately this will also store a source map for better error reporting, but a few other changes are happening before that. The arena itself is unlikely to be needed from a perf standpoint (reading all the files should be the slow part), but having more things be copyable is nice. For reference the perf impact of this change is within the noise. changelog: none
2 parents 00f68d5 + bae625f commit 82d729c

File tree

8 files changed

+346
-244
lines changed

8 files changed

+346
-244
lines changed

clippy_dev/src/deprecate_lint.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::parse::{DeprecatedLint, Lint, find_lint_decls, read_deprecated_lints};
1+
use crate::parse::{DeprecatedLint, Lint, ParseCx};
22
use crate::update_lints::generate_lint_files;
33
use crate::utils::{UpdateMode, Version};
44
use std::ffi::OsStr;
@@ -14,17 +14,20 @@ use std::{fs, io};
1414
/// # Panics
1515
///
1616
/// If a file path could not read from or written to
17-
pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
18-
let mut lints = find_lint_decls();
19-
let (mut deprecated_lints, renamed_lints) = read_deprecated_lints();
17+
pub fn deprecate<'cx>(cx: ParseCx<'cx>, clippy_version: Version, name: &'cx str, reason: &'cx str) {
18+
let mut lints = cx.find_lint_decls();
19+
let (mut deprecated_lints, renamed_lints) = cx.read_deprecated_lints();
2020

2121
let Some(lint) = lints.iter().find(|l| l.name == name) else {
2222
eprintln!("error: failed to find lint `{name}`");
2323
return;
2424
};
2525

26-
let prefixed_name = String::from_iter(["clippy::", name]);
27-
match deprecated_lints.binary_search_by(|x| x.name.cmp(&prefixed_name)) {
26+
let prefixed_name = cx.str_buf.with(|buf| {
27+
buf.extend(["clippy::", name]);
28+
cx.arena.alloc_str(buf)
29+
});
30+
match deprecated_lints.binary_search_by(|x| x.name.cmp(prefixed_name)) {
2831
Ok(_) => {
2932
println!("`{name}` is already deprecated");
3033
return;
@@ -33,8 +36,8 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
3336
idx,
3437
DeprecatedLint {
3538
name: prefixed_name,
36-
reason: reason.into(),
37-
version: clippy_version.rust_display().to_string(),
39+
reason,
40+
version: cx.str_buf.alloc_display(cx.arena, clippy_version.rust_display()),
3841
},
3942
),
4043
}
@@ -58,8 +61,8 @@ pub fn deprecate(clippy_version: Version, name: &str, reason: &str) {
5861
}
5962
}
6063

61-
fn remove_lint_declaration(name: &str, path: &Path, lints: &mut Vec<Lint>) -> io::Result<bool> {
62-
fn remove_lint(name: &str, lints: &mut Vec<Lint>) {
64+
fn remove_lint_declaration(name: &str, path: &Path, lints: &mut Vec<Lint<'_>>) -> io::Result<bool> {
65+
fn remove_lint(name: &str, lints: &mut Vec<Lint<'_>>) {
6366
lints.iter().position(|l| l.name == name).map(|pos| lints.remove(pos));
6467
}
6568

clippy_dev/src/fmt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn run_rustfmt(update_mode: UpdateMode) {
268268
.expect("invalid rustfmt path");
269269
rustfmt_path.truncate(rustfmt_path.trim_end().len());
270270

271-
let args: Vec<_> = walk_dir_no_dot_or_target()
271+
let args: Vec<_> = walk_dir_no_dot_or_target(".")
272272
.filter_map(|e| {
273273
let e = expect_action(e, ErrAction::Read, ".");
274274
e.path()

clippy_dev/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
new_range_api,
66
os_str_slice,
77
os_string_truncate,
8+
pattern,
89
rustc_private,
910
slice_split_once
1011
)]
@@ -17,6 +18,7 @@
1718
)]
1819
#![allow(clippy::missing_panics_doc)]
1920

21+
extern crate rustc_arena;
2022
#[expect(unused_extern_crates, reason = "required to link to rustc crates")]
2123
extern crate rustc_driver;
2224
extern crate rustc_lexer;
@@ -34,7 +36,8 @@ pub mod setup;
3436
pub mod sync;
3537
pub mod update_lints;
3638

39+
mod parse;
3740
mod utils;
38-
pub use utils::{ClippyInfo, UpdateMode};
3941

40-
mod parse;
42+
pub use self::parse::{ParseCx, new_parse_cx};
43+
pub use self::utils::{ClippyInfo, UpdateMode};

clippy_dev/src/main.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use clap::{Args, Parser, Subcommand};
66
use clippy_dev::{
7-
ClippyInfo, UpdateMode, deprecate_lint, dogfood, fmt, lint, new_lint, release, rename_lint, serve, setup, sync,
8-
update_lints,
7+
ClippyInfo, UpdateMode, deprecate_lint, dogfood, fmt, lint, new_lint, new_parse_cx, release, rename_lint, serve,
8+
setup, sync, update_lints,
99
};
1010
use std::env;
1111

@@ -27,15 +27,15 @@ fn main() {
2727
allow_no_vcs,
2828
} => dogfood::dogfood(fix, allow_dirty, allow_staged, allow_no_vcs),
2929
DevCommand::Fmt { check } => fmt::run(UpdateMode::from_check(check)),
30-
DevCommand::UpdateLints { check } => update_lints::update(UpdateMode::from_check(check)),
30+
DevCommand::UpdateLints { check } => new_parse_cx(|cx| update_lints::update(cx, UpdateMode::from_check(check))),
3131
DevCommand::NewLint {
3232
pass,
3333
name,
3434
category,
3535
r#type,
3636
msrv,
3737
} => match new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv) {
38-
Ok(()) => update_lints::update(UpdateMode::Change),
38+
Ok(()) => new_parse_cx(|cx| update_lints::update(cx, UpdateMode::Change)),
3939
Err(e) => eprintln!("Unable to create lint: {e}"),
4040
},
4141
DevCommand::Setup(SetupCommand { subcommand }) => match subcommand {
@@ -78,13 +78,18 @@ fn main() {
7878
old_name,
7979
new_name,
8080
uplift,
81-
} => rename_lint::rename(
82-
clippy.version,
83-
&old_name,
84-
new_name.as_ref().unwrap_or(&old_name),
85-
uplift,
86-
),
87-
DevCommand::Deprecate { name, reason } => deprecate_lint::deprecate(clippy.version, &name, &reason),
81+
} => new_parse_cx(|cx| {
82+
rename_lint::rename(
83+
cx,
84+
clippy.version,
85+
&old_name,
86+
new_name.as_ref().unwrap_or(&old_name),
87+
uplift,
88+
);
89+
}),
90+
DevCommand::Deprecate { name, reason } => {
91+
new_parse_cx(|cx| deprecate_lint::deprecate(cx, clippy.version, &name, &reason));
92+
},
8893
DevCommand::Sync(SyncCommand { subcommand }) => match subcommand {
8994
SyncSubcommand::UpdateNightly => sync::update_nightly(),
9095
},

0 commit comments

Comments
 (0)