Skip to content

Commit d5ef542

Browse files
committed
Generate renamed lint test
1 parent 4227411 commit d5ef542

File tree

6 files changed

+135
-77
lines changed

6 files changed

+135
-77
lines changed

clippy_dev/src/update_lints.rs

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use core::fmt::Write;
22
use itertools::Itertools;
33
use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind};
4-
use std::collections::HashMap;
4+
use std::collections::{HashMap, HashSet};
55
use std::ffi::OsStr;
66
use std::fs;
77
use std::path::Path;
@@ -32,7 +32,7 @@ pub enum UpdateMode {
3232
/// Panics if a file path could not read from or then written to
3333
#[allow(clippy::too_many_lines)]
3434
pub fn run(update_mode: UpdateMode) {
35-
let (lints, deprecated_lints) = gather_all();
35+
let (lints, deprecated_lints, renamed_lints) = gather_all();
3636

3737
let internal_lints = Lint::internal_lints(&lints);
3838
let usable_lints = Lint::usable_lints(&lints);
@@ -110,10 +110,13 @@ pub fn run(update_mode: UpdateMode) {
110110

111111
let content = gen_deprecated_lints_test(&deprecated_lints);
112112
process_file("tests/ui/deprecated.rs", update_mode, &content);
113+
114+
let content = gen_renamed_lints_test(&renamed_lints);
115+
process_file("tests/ui/rename.rs", update_mode, &content);
113116
}
114117

115118
pub fn print_lints() {
116-
let (lint_list, _) = gather_all();
119+
let (lint_list, _, _) = gather_all();
117120
let usable_lints = Lint::usable_lints(&lint_list);
118121
let usable_lint_count = usable_lints.len();
119122
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
@@ -213,6 +216,19 @@ impl DeprecatedLint {
213216
}
214217
}
215218

219+
struct RenamedLint {
220+
old_name: String,
221+
new_name: String,
222+
}
223+
impl RenamedLint {
224+
fn new(old_name: &str, new_name: &str) -> Self {
225+
Self {
226+
old_name: remove_line_splices(old_name),
227+
new_name: remove_line_splices(new_name),
228+
}
229+
}
230+
}
231+
216232
/// Generates the code for registering a group
217233
fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator<Item = &'a Lint>) -> String {
218234
let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect();
@@ -288,10 +304,30 @@ fn gen_deprecated_lints_test(lints: &[DeprecatedLint]) -> String {
288304
res
289305
}
290306

307+
fn gen_renamed_lints_test(lints: &[RenamedLint]) -> String {
308+
let mut seen_lints = HashSet::new();
309+
let mut res: String = GENERATED_FILE_COMMENT.into();
310+
res.push_str("// run-rustfix\n\n");
311+
for lint in lints {
312+
if seen_lints.insert(&lint.new_name) {
313+
writeln!(res, "#![allow({})]", lint.new_name).unwrap();
314+
}
315+
}
316+
seen_lints.clear();
317+
for lint in lints {
318+
if seen_lints.insert(&lint.old_name) {
319+
writeln!(res, "#![warn({})]", lint.old_name).unwrap();
320+
}
321+
}
322+
res.push_str("\nfn main() {}\n");
323+
res
324+
}
325+
291326
/// Gathers all lints defined in `clippy_lints/src`
292-
fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
327+
fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>, Vec<RenamedLint>) {
293328
let mut lints = Vec::with_capacity(1000);
294329
let mut deprecated_lints = Vec::with_capacity(50);
330+
let mut renamed_lints = Vec::with_capacity(50);
295331
let root_path = clippy_project_root().join("clippy_lints/src");
296332

297333
for (rel_path, file) in WalkDir::new(&root_path)
@@ -317,13 +353,13 @@ fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
317353
module.strip_suffix(".rs").unwrap_or(&module)
318354
};
319355

320-
if module == "deprecated_lints" {
321-
parse_deprecated_contents(&contents, &mut deprecated_lints);
322-
} else {
323-
parse_contents(&contents, module, &mut lints);
356+
match module {
357+
"deprecated_lints" => parse_deprecated_contents(&contents, &mut deprecated_lints),
358+
"renamed_lints" => parse_renamed_contents(&contents, &mut renamed_lints),
359+
_ => parse_contents(&contents, module, &mut lints),
324360
}
325361
}
326-
(lints, deprecated_lints)
362+
(lints, deprecated_lints, renamed_lints)
327363
}
328364

329365
macro_rules! match_tokens {
@@ -406,6 +442,25 @@ fn parse_deprecated_contents(contents: &str, lints: &mut Vec<DeprecatedLint>) {
406442
}
407443
}
408444

445+
fn parse_renamed_contents(contents: &str, lints: &mut Vec<RenamedLint>) {
446+
for line in contents.lines() {
447+
let mut offset = 0usize;
448+
let mut iter = tokenize(line).map(|t| {
449+
let range = offset..offset + t.len;
450+
offset = range.end;
451+
(t.kind, &line[range])
452+
});
453+
let (old_name, new_name) = match_tokens!(
454+
iter,
455+
// ("old_name",
456+
Whitespace OpenParen Literal{kind: LiteralKind::Str{..},..}(old_name) Comma
457+
// "new_name"),
458+
Whitespace Literal{kind: LiteralKind::Str{..},..}(new_name) CloseParen Comma
459+
);
460+
lints.push(RenamedLint::new(old_name, new_name));
461+
}
462+
}
463+
409464
/// Removes the line splices and surrounding quotes from a string literal
410465
fn remove_line_splices(s: &str) -> String {
411466
let s = s

clippy_lints/src/lib.rs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ mod deprecated_lints;
162162
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
163163
mod utils;
164164

165+
mod renamed_lints;
166+
165167
// begin lints modules, do not remove this comment, it’s used in `update_lints`
166168
mod absurd_extreme_comparisons;
167169
mod approx_const;
@@ -920,43 +922,9 @@ fn register_removed_non_tool_lints(store: &mut rustc_lint::LintStore) {
920922
///
921923
/// Used in `./src/driver.rs`.
922924
pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
923-
// NOTE: when renaming a lint, add a corresponding test to tests/ui/rename.rs
924-
ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions");
925-
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
926-
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
927-
ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes");
928-
ls.register_renamed("clippy::option_and_then_some", "clippy::bind_instead_of_map");
929-
ls.register_renamed("clippy::box_vec", "clippy::box_collection");
930-
ls.register_renamed("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions");
931-
ls.register_renamed("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions");
932-
ls.register_renamed("clippy::option_map_unwrap_or", "clippy::map_unwrap_or");
933-
ls.register_renamed("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or");
934-
ls.register_renamed("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or");
935-
ls.register_renamed("clippy::option_unwrap_used", "clippy::unwrap_used");
936-
ls.register_renamed("clippy::result_unwrap_used", "clippy::unwrap_used");
937-
ls.register_renamed("clippy::option_expect_used", "clippy::expect_used");
938-
ls.register_renamed("clippy::result_expect_used", "clippy::expect_used");
939-
ls.register_renamed("clippy::for_loop_over_option", "clippy::for_loops_over_fallibles");
940-
ls.register_renamed("clippy::for_loop_over_result", "clippy::for_loops_over_fallibles");
941-
ls.register_renamed("clippy::identity_conversion", "clippy::useless_conversion");
942-
ls.register_renamed("clippy::zero_width_space", "clippy::invisible_characters");
943-
ls.register_renamed("clippy::single_char_push_str", "clippy::single_char_add_str");
944-
ls.register_renamed("clippy::if_let_some_result", "clippy::match_result_ok");
945-
ls.register_renamed("clippy::disallowed_type", "clippy::disallowed_types");
946-
ls.register_renamed("clippy::disallowed_method", "clippy::disallowed_methods");
947-
ls.register_renamed("clippy::ref_in_deref", "clippy::needless_borrow");
948-
ls.register_renamed("clippy::to_string_in_display", "clippy::recursive_format_impl");
949-
950-
// uplifted lints
951-
ls.register_renamed("clippy::invalid_ref", "invalid_value");
952-
ls.register_renamed("clippy::into_iter_on_array", "array_into_iter");
953-
ls.register_renamed("clippy::unused_label", "unused_labels");
954-
ls.register_renamed("clippy::drop_bounds", "drop_bounds");
955-
ls.register_renamed("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr");
956-
ls.register_renamed("clippy::panic_params", "non_fmt_panics");
957-
ls.register_renamed("clippy::unknown_clippy_lints", "unknown_lints");
958-
ls.register_renamed("clippy::invalid_atomic_ordering", "invalid_atomic_ordering");
959-
ls.register_renamed("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums");
925+
for (old_name, new_name) in renamed_lints::RENAMED_LINTS {
926+
ls.register_renamed(old_name, new_name);
927+
}
960928
}
961929

962930
// only exists to let the dogfood integration test works.

clippy_lints/src/renamed_lints.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
pub static RENAMED_LINTS: &[(&str, &str)] = &[
2+
("clippy::stutter", "clippy::module_name_repetitions"),
3+
("clippy::new_without_default_derive", "clippy::new_without_default"),
4+
("clippy::cyclomatic_complexity", "clippy::cognitive_complexity"),
5+
("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes"),
6+
("clippy::option_and_then_some", "clippy::bind_instead_of_map"),
7+
("clippy::box_vec", "clippy::box_collection"),
8+
("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"),
9+
("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"),
10+
("clippy::option_map_unwrap_or", "clippy::map_unwrap_or"),
11+
("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or"),
12+
("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or"),
13+
("clippy::option_unwrap_used", "clippy::unwrap_used"),
14+
("clippy::result_unwrap_used", "clippy::unwrap_used"),
15+
("clippy::option_expect_used", "clippy::expect_used"),
16+
("clippy::result_expect_used", "clippy::expect_used"),
17+
("clippy::for_loop_over_option", "clippy::for_loops_over_fallibles"),
18+
("clippy::for_loop_over_result", "clippy::for_loops_over_fallibles"),
19+
("clippy::identity_conversion", "clippy::useless_conversion"),
20+
("clippy::zero_width_space", "clippy::invisible_characters"),
21+
("clippy::single_char_push_str", "clippy::single_char_add_str"),
22+
("clippy::if_let_some_result", "clippy::match_result_ok"),
23+
("clippy::disallowed_type", "clippy::disallowed_types"),
24+
("clippy::disallowed_method", "clippy::disallowed_methods"),
25+
("clippy::ref_in_deref", "clippy::needless_borrow"),
26+
("clippy::to_string_in_display", "clippy::recursive_format_impl"),
27+
// uplifted lints
28+
("clippy::invalid_ref", "invalid_value"),
29+
("clippy::into_iter_on_array", "array_into_iter"),
30+
("clippy::unused_label", "unused_labels"),
31+
("clippy::drop_bounds", "drop_bounds"),
32+
("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr"),
33+
("clippy::panic_params", "non_fmt_panics"),
34+
("clippy::unknown_clippy_lints", "unknown_lints"),
35+
("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"),
36+
("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums"),
37+
];

tests/ui/rename.fixed

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
//! Test for Clippy lint renames.
1+
// This file was generated by `cargo dev update_lints`.
2+
// Use that command to update this file and do not edit by hand.
3+
// Manual edits will be overwritten.
4+
25
// run-rustfix
36

4-
#![allow(dead_code)]
5-
// allow the new lint name here, to test if the new name works
67
#![allow(clippy::module_name_repetitions)]
78
#![allow(clippy::new_without_default)]
8-
#![allow(clippy::redundant_static_lifetimes)]
99
#![allow(clippy::cognitive_complexity)]
10+
#![allow(clippy::redundant_static_lifetimes)]
1011
#![allow(clippy::bind_instead_of_map)]
1112
#![allow(clippy::box_collection)]
1213
#![allow(clippy::blocks_in_if_conditions)]
@@ -20,8 +21,8 @@
2021
#![allow(clippy::match_result_ok)]
2122
#![allow(clippy::disallowed_types)]
2223
#![allow(clippy::disallowed_methods)]
24+
#![allow(clippy::needless_borrow)]
2325
#![allow(clippy::recursive_format_impl)]
24-
// uplifted lints
2526
#![allow(invalid_value)]
2627
#![allow(array_into_iter)]
2728
#![allow(unused_labels)]
@@ -31,11 +32,10 @@
3132
#![allow(unknown_lints)]
3233
#![allow(invalid_atomic_ordering)]
3334
#![allow(enum_intrinsics_non_enums)]
34-
// warn for the old lint name here, to test if the renaming worked
3535
#![warn(clippy::module_name_repetitions)]
3636
#![warn(clippy::new_without_default)]
37-
#![warn(clippy::redundant_static_lifetimes)]
3837
#![warn(clippy::cognitive_complexity)]
38+
#![warn(clippy::redundant_static_lifetimes)]
3939
#![warn(clippy::bind_instead_of_map)]
4040
#![warn(clippy::box_collection)]
4141
#![warn(clippy::blocks_in_if_conditions)]
@@ -57,7 +57,6 @@
5757
#![warn(clippy::disallowed_methods)]
5858
#![warn(clippy::needless_borrow)]
5959
#![warn(clippy::recursive_format_impl)]
60-
// uplifted lints
6160
#![warn(invalid_value)]
6261
#![warn(array_into_iter)]
6362
#![warn(unused_labels)]

tests/ui/rename.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
//! Test for Clippy lint renames.
1+
// This file was generated by `cargo dev update_lints`.
2+
// Use that command to update this file and do not edit by hand.
3+
// Manual edits will be overwritten.
4+
25
// run-rustfix
36

4-
#![allow(dead_code)]
5-
// allow the new lint name here, to test if the new name works
67
#![allow(clippy::module_name_repetitions)]
78
#![allow(clippy::new_without_default)]
8-
#![allow(clippy::redundant_static_lifetimes)]
99
#![allow(clippy::cognitive_complexity)]
10+
#![allow(clippy::redundant_static_lifetimes)]
1011
#![allow(clippy::bind_instead_of_map)]
1112
#![allow(clippy::box_collection)]
1213
#![allow(clippy::blocks_in_if_conditions)]
@@ -20,8 +21,8 @@
2021
#![allow(clippy::match_result_ok)]
2122
#![allow(clippy::disallowed_types)]
2223
#![allow(clippy::disallowed_methods)]
24+
#![allow(clippy::needless_borrow)]
2325
#![allow(clippy::recursive_format_impl)]
24-
// uplifted lints
2526
#![allow(invalid_value)]
2627
#![allow(array_into_iter)]
2728
#![allow(unused_labels)]
@@ -31,11 +32,10 @@
3132
#![allow(unknown_lints)]
3233
#![allow(invalid_atomic_ordering)]
3334
#![allow(enum_intrinsics_non_enums)]
34-
// warn for the old lint name here, to test if the renaming worked
3535
#![warn(clippy::stutter)]
3636
#![warn(clippy::new_without_default_derive)]
37-
#![warn(clippy::const_static_lifetime)]
3837
#![warn(clippy::cyclomatic_complexity)]
38+
#![warn(clippy::const_static_lifetime)]
3939
#![warn(clippy::option_and_then_some)]
4040
#![warn(clippy::box_vec)]
4141
#![warn(clippy::block_in_if_condition_expr)]
@@ -57,7 +57,6 @@
5757
#![warn(clippy::disallowed_method)]
5858
#![warn(clippy::ref_in_deref)]
5959
#![warn(clippy::to_string_in_display)]
60-
// uplifted lints
6160
#![warn(clippy::invalid_ref)]
6261
#![warn(clippy::into_iter_on_array)]
6362
#![warn(clippy::unused_label)]

0 commit comments

Comments
 (0)