Skip to content

Commit ee124d9

Browse files
committed
clippy_dev: Use the shared parsing logic in new_lint.
1 parent 35c7e63 commit ee124d9

9 files changed

Lines changed: 598 additions & 538 deletions

File tree

clippy_dev/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ annotate-snippets = { version = "0.12.10", features = ["simd"] }
99
anstream = "0.6.20"
1010
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
1111
clap = { version = "4.4", features = ["derive"] }
12-
indoc = "1.0"
1312
itertools = "0.12"
1413
memchr = "2.7.6"
1514
opener = "0.8"

clippy_dev/src/edit_lints.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn remove_lint_declaration(
180180
let delete_mod = if data.lints.iter().all(|(_, l)| l.name_sp.file != lint_file) {
181181
delete_file_if_exists(lint_file.path.get())
182182
} else {
183-
updater.update_file(lint_file.path.get(), &mut |_, src, dst| -> UpdateStatus {
183+
updater.change_file(lint_file.path.get(), |src, dst| {
184184
let mut start = &src[..lint_data.decl_range.start as usize];
185185
if start.ends_with("\n\n") {
186186
start = &start[..start.len() - 1];
@@ -191,7 +191,6 @@ fn remove_lint_declaration(
191191
}
192192
dst.push_str(start);
193193
dst.push_str(end);
194-
UpdateStatus::Changed
195194
});
196195
false
197196
};

clippy_dev/src/fmt.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ pub fn run(update_mode: UpdateMode) {
113113
cx.dcx.exit_on_err();
114114

115115
let mut updater = FileUpdater::default();
116+
let copy: &mut dyn FnMut(&str, &mut String) = &mut |src, dst| dst.push_str(src);
116117

117118
#[expect(clippy::mutable_key_type)]
118119
let mut lints = lint_data.lints.mk_by_file_map();
@@ -122,13 +123,13 @@ pub fn run(update_mode: UpdateMode) {
122123
let mut lints = lints.remove(file);
123124
let lints = lints.as_deref_mut().unwrap_or_default();
124125
updater.update_loaded_file_checked("cargo dev fmt", update_mode, file, &mut |_, src, dst| {
125-
gen_sorted_lints_file(src, dst, lints, passes, &mut ranges);
126+
gen_sorted_lints_file(src, dst, lints, passes, &mut ranges, copy);
126127
UpdateStatus::from_changed(src != dst)
127128
});
128129
}
129130
for (&file, lints) in &mut lints {
130131
updater.update_loaded_file_checked("cargo dev fmt", update_mode, file, &mut |_, src, dst| {
131-
gen_sorted_lints_file(src, dst, lints, &mut [], &mut ranges);
132+
gen_sorted_lints_file(src, dst, lints, &mut [], &mut ranges, copy);
132133
UpdateStatus::from_changed(src != dst)
133134
});
134135
}
@@ -138,9 +139,7 @@ pub fn run(update_mode: UpdateMode) {
138139
update_mode,
139140
conf_data.decl_sp.file,
140141
&mut |_, src, dst| {
141-
dst.push_str(&src[..conf_data.decl_sp.range.start as usize]);
142-
conf_data.gen_mac(src, dst);
143-
dst.push_str(&src[conf_data.decl_sp.range.end as usize..]);
142+
conf_data.gen_file(src, dst);
144143
UpdateStatus::from_changed(src != dst)
145144
},
146145
);

clippy_dev/src/generate.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ impl LintPass<'_> {
238238
}
239239

240240
impl ConfDef<'_> {
241+
pub fn gen_file(&mut self, src: &str, dst: &mut String) {
242+
dst.push_str(&src[..self.decl_sp.range.start as usize]);
243+
self.gen_mac(src, dst);
244+
dst.push_str(&src[self.decl_sp.range.end as usize..]);
245+
}
246+
241247
pub fn gen_mac(&mut self, src: &str, dst: &mut String) {
242248
self.opts.sort_unstable_by_key(|o| o.name);
243249
dst.push_str("define_Conf! {");
@@ -310,6 +316,7 @@ pub fn gen_sorted_lints_file(
310316
lints: &mut [ActiveLint<'_, '_>],
311317
passes: &mut [LintPass<'_>],
312318
ranges: &mut VecBuf<Range<u32>>,
319+
copy_src: &mut dyn FnMut(&str, &mut String),
313320
) {
314321
ranges.with(|ranges| {
315322
ranges.extend(lints.iter().map(|x| x.data.decl_range));
@@ -321,7 +328,7 @@ pub fn gen_sorted_lints_file(
321328

322329
let mut ranges = ranges.iter();
323330
let pos = if let Some(range) = ranges.next() {
324-
dst.push_str(&src[..range.start as usize]);
331+
copy_src(&src[..range.start as usize], dst);
325332
for lint in &*lints {
326333
lint.gen_mac(dst);
327334
dst.push_str("\n\n");
@@ -333,29 +340,31 @@ pub fn gen_sorted_lints_file(
333340
}
334341
range.end
335342
} else {
336-
dst.push_str(src);
343+
copy_src(src, dst);
337344
return;
338345
};
339346

340347
let pos = ranges.fold(pos, |start, range| {
341348
let s = &src[start as usize..range.start as usize];
342-
dst.push_str(if s.trim_start().is_empty() {
343-
// Only whitespace between this and the previous item. No need to keep that.
344-
""
345-
} else if src[..pos as usize].ends_with("\n\n")
346-
&& let Some(s) = s.strip_prefix("\n\n")
347-
{
348-
// Empty line before and after. Remove one of them.
349-
s
350-
} else {
351-
// Remove only full lines unless something is in the way.
352-
s.strip_prefix('\n').unwrap_or(s)
353-
});
349+
// Don't keep whitespace between declarations.
350+
if !s.trim_start().is_empty() {
351+
let s = if src[..pos as usize].ends_with("\n\n")
352+
&& let Some(s) = s.strip_prefix("\n\n")
353+
{
354+
// Empty line before and after. Remove one of them.
355+
s
356+
} else {
357+
// Remove the line end immediately proceeding a declaration.
358+
s.strip_prefix('\n').unwrap_or(s)
359+
};
360+
copy_src(s, dst);
361+
}
354362
range.end
355363
});
356364

357365
// Since we always generate an empty line at the end, make sure to always skip it.
358366
let s = &src[pos as usize..];
359-
dst.push_str(s.strip_prefix('\n').map_or(s, |s| s.strip_prefix('\n').unwrap_or(s)));
367+
let s = s.strip_prefix('\n').map_or(s, |s| s.strip_prefix('\n').unwrap_or(s));
368+
copy_src(s, dst);
360369
});
361370
}

clippy_dev/src/main.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,8 @@ fn main() {
3535
pass,
3636
name,
3737
category,
38-
r#type,
3938
msrv,
40-
} => {
41-
new_lint::create(clippy.version, pass, &name, &category, r#type.as_deref(), msrv);
42-
new_parse_cx(|cx| {
43-
let data = cx.parse_lint_decls();
44-
cx.dcx.exit_on_err();
45-
data.gen_decls(UpdateMode::Change);
46-
});
47-
},
39+
} => new_lint::create(clippy.version, &pass, &name, &category, msrv),
4840
DevCommand::Setup(SetupCommand { subcommand }) => match subcommand {
4941
SetupSubcommand::Intellij { remove, repo_path } => {
5042
if remove {
@@ -162,9 +154,9 @@ enum DevCommand {
162154
#[command(name = "new_lint")]
163155
/// Create a new lint and run `cargo dev update_lints`
164156
NewLint {
165-
#[arg(short, long, conflicts_with = "type", default_value = "late")]
157+
#[arg(short, long, default_value = "late")]
166158
/// Specify whether the lint runs during the early or late pass
167-
pass: new_lint::Pass,
159+
pass: String,
168160
#[arg(
169161
short,
170162
long,
@@ -191,9 +183,6 @@ enum DevCommand {
191183
/// What category the lint belongs to
192184
category: String,
193185
#[arg(long)]
194-
/// What directory the lint belongs in
195-
r#type: Option<String>,
196-
#[arg(long)]
197186
/// Add MSRV config code to the lint
198187
msrv: bool,
199188
},

0 commit comments

Comments
 (0)