Generate renamed lint test

This commit is contained in:
Jason Newcomb 2022-04-03 08:51:17 -04:00
parent 4227411513
commit d5ef542d37
6 changed files with 136 additions and 78 deletions

View file

@ -1,7 +1,7 @@
use core::fmt::Write;
use itertools::Itertools;
use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind};
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::fs;
use std::path::Path;
@ -32,7 +32,7 @@ pub enum UpdateMode {
/// Panics if a file path could not read from or then written to
#[allow(clippy::too_many_lines)]
pub fn run(update_mode: UpdateMode) {
let (lints, deprecated_lints) = gather_all();
let (lints, deprecated_lints, renamed_lints) = gather_all();
let internal_lints = Lint::internal_lints(&lints);
let usable_lints = Lint::usable_lints(&lints);
@ -110,10 +110,13 @@ pub fn run(update_mode: UpdateMode) {
let content = gen_deprecated_lints_test(&deprecated_lints);
process_file("tests/ui/deprecated.rs", update_mode, &content);
let content = gen_renamed_lints_test(&renamed_lints);
process_file("tests/ui/rename.rs", update_mode, &content);
}
pub fn print_lints() {
let (lint_list, _) = gather_all();
let (lint_list, _, _) = gather_all();
let usable_lints = Lint::usable_lints(&lint_list);
let usable_lint_count = usable_lints.len();
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
@ -213,6 +216,19 @@ impl DeprecatedLint {
}
}
struct RenamedLint {
old_name: String,
new_name: String,
}
impl RenamedLint {
fn new(old_name: &str, new_name: &str) -> Self {
Self {
old_name: remove_line_splices(old_name),
new_name: remove_line_splices(new_name),
}
}
}
/// Generates the code for registering a group
fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator<Item = &'a Lint>) -> String {
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 {
res
}
fn gen_renamed_lints_test(lints: &[RenamedLint]) -> String {
let mut seen_lints = HashSet::new();
let mut res: String = GENERATED_FILE_COMMENT.into();
res.push_str("// run-rustfix\n\n");
for lint in lints {
if seen_lints.insert(&lint.new_name) {
writeln!(res, "#![allow({})]", lint.new_name).unwrap();
}
}
seen_lints.clear();
for lint in lints {
if seen_lints.insert(&lint.old_name) {
writeln!(res, "#![warn({})]", lint.old_name).unwrap();
}
}
res.push_str("\nfn main() {}\n");
res
}
/// Gathers all lints defined in `clippy_lints/src`
fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>, Vec<RenamedLint>) {
let mut lints = Vec::with_capacity(1000);
let mut deprecated_lints = Vec::with_capacity(50);
let mut renamed_lints = Vec::with_capacity(50);
let root_path = clippy_project_root().join("clippy_lints/src");
for (rel_path, file) in WalkDir::new(&root_path)
@ -317,13 +353,13 @@ fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
module.strip_suffix(".rs").unwrap_or(&module)
};
if module == "deprecated_lints" {
parse_deprecated_contents(&contents, &mut deprecated_lints);
} else {
parse_contents(&contents, module, &mut lints);
match module {
"deprecated_lints" => parse_deprecated_contents(&contents, &mut deprecated_lints),
"renamed_lints" => parse_renamed_contents(&contents, &mut renamed_lints),
_ => parse_contents(&contents, module, &mut lints),
}
}
(lints, deprecated_lints)
(lints, deprecated_lints, renamed_lints)
}
macro_rules! match_tokens {
@ -406,6 +442,25 @@ fn parse_deprecated_contents(contents: &str, lints: &mut Vec<DeprecatedLint>) {
}
}
fn parse_renamed_contents(contents: &str, lints: &mut Vec<RenamedLint>) {
for line in contents.lines() {
let mut offset = 0usize;
let mut iter = tokenize(line).map(|t| {
let range = offset..offset + t.len;
offset = range.end;
(t.kind, &line[range])
});
let (old_name, new_name) = match_tokens!(
iter,
// ("old_name",
Whitespace OpenParen Literal{kind: LiteralKind::Str{..},..}(old_name) Comma
// "new_name"),
Whitespace Literal{kind: LiteralKind::Str{..},..}(new_name) CloseParen Comma
);
lints.push(RenamedLint::new(old_name, new_name));
}
}
/// Removes the line splices and surrounding quotes from a string literal
fn remove_line_splices(s: &str) -> String {
let s = s

View file

@ -162,6 +162,8 @@ mod deprecated_lints;
#[cfg_attr(feature = "internal", allow(clippy::missing_clippy_version_attribute))]
mod utils;
mod renamed_lints;
// begin lints modules, do not remove this comment, its used in `update_lints`
mod absurd_extreme_comparisons;
mod approx_const;
@ -920,43 +922,9 @@ fn register_removed_non_tool_lints(store: &mut rustc_lint::LintStore) {
///
/// Used in `./src/driver.rs`.
pub fn register_renamed(ls: &mut rustc_lint::LintStore) {
// NOTE: when renaming a lint, add a corresponding test to tests/ui/rename.rs
ls.register_renamed("clippy::stutter", "clippy::module_name_repetitions");
ls.register_renamed("clippy::new_without_default_derive", "clippy::new_without_default");
ls.register_renamed("clippy::cyclomatic_complexity", "clippy::cognitive_complexity");
ls.register_renamed("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes");
ls.register_renamed("clippy::option_and_then_some", "clippy::bind_instead_of_map");
ls.register_renamed("clippy::box_vec", "clippy::box_collection");
ls.register_renamed("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions");
ls.register_renamed("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions");
ls.register_renamed("clippy::option_map_unwrap_or", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or");
ls.register_renamed("clippy::option_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::result_unwrap_used", "clippy::unwrap_used");
ls.register_renamed("clippy::option_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::result_expect_used", "clippy::expect_used");
ls.register_renamed("clippy::for_loop_over_option", "clippy::for_loops_over_fallibles");
ls.register_renamed("clippy::for_loop_over_result", "clippy::for_loops_over_fallibles");
ls.register_renamed("clippy::identity_conversion", "clippy::useless_conversion");
ls.register_renamed("clippy::zero_width_space", "clippy::invisible_characters");
ls.register_renamed("clippy::single_char_push_str", "clippy::single_char_add_str");
ls.register_renamed("clippy::if_let_some_result", "clippy::match_result_ok");
ls.register_renamed("clippy::disallowed_type", "clippy::disallowed_types");
ls.register_renamed("clippy::disallowed_method", "clippy::disallowed_methods");
ls.register_renamed("clippy::ref_in_deref", "clippy::needless_borrow");
ls.register_renamed("clippy::to_string_in_display", "clippy::recursive_format_impl");
// uplifted lints
ls.register_renamed("clippy::invalid_ref", "invalid_value");
ls.register_renamed("clippy::into_iter_on_array", "array_into_iter");
ls.register_renamed("clippy::unused_label", "unused_labels");
ls.register_renamed("clippy::drop_bounds", "drop_bounds");
ls.register_renamed("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr");
ls.register_renamed("clippy::panic_params", "non_fmt_panics");
ls.register_renamed("clippy::unknown_clippy_lints", "unknown_lints");
ls.register_renamed("clippy::invalid_atomic_ordering", "invalid_atomic_ordering");
ls.register_renamed("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums");
for (old_name, new_name) in renamed_lints::RENAMED_LINTS {
ls.register_renamed(old_name, new_name);
}
}
// only exists to let the dogfood integration test works.

View file

@ -0,0 +1,37 @@
pub static RENAMED_LINTS: &[(&str, &str)] = &[
("clippy::stutter", "clippy::module_name_repetitions"),
("clippy::new_without_default_derive", "clippy::new_without_default"),
("clippy::cyclomatic_complexity", "clippy::cognitive_complexity"),
("clippy::const_static_lifetime", "clippy::redundant_static_lifetimes"),
("clippy::option_and_then_some", "clippy::bind_instead_of_map"),
("clippy::box_vec", "clippy::box_collection"),
("clippy::block_in_if_condition_expr", "clippy::blocks_in_if_conditions"),
("clippy::block_in_if_condition_stmt", "clippy::blocks_in_if_conditions"),
("clippy::option_map_unwrap_or", "clippy::map_unwrap_or"),
("clippy::option_map_unwrap_or_else", "clippy::map_unwrap_or"),
("clippy::result_map_unwrap_or_else", "clippy::map_unwrap_or"),
("clippy::option_unwrap_used", "clippy::unwrap_used"),
("clippy::result_unwrap_used", "clippy::unwrap_used"),
("clippy::option_expect_used", "clippy::expect_used"),
("clippy::result_expect_used", "clippy::expect_used"),
("clippy::for_loop_over_option", "clippy::for_loops_over_fallibles"),
("clippy::for_loop_over_result", "clippy::for_loops_over_fallibles"),
("clippy::identity_conversion", "clippy::useless_conversion"),
("clippy::zero_width_space", "clippy::invisible_characters"),
("clippy::single_char_push_str", "clippy::single_char_add_str"),
("clippy::if_let_some_result", "clippy::match_result_ok"),
("clippy::disallowed_type", "clippy::disallowed_types"),
("clippy::disallowed_method", "clippy::disallowed_methods"),
("clippy::ref_in_deref", "clippy::needless_borrow"),
("clippy::to_string_in_display", "clippy::recursive_format_impl"),
// uplifted lints
("clippy::invalid_ref", "invalid_value"),
("clippy::into_iter_on_array", "array_into_iter"),
("clippy::unused_label", "unused_labels"),
("clippy::drop_bounds", "drop_bounds"),
("clippy::temporary_cstring_as_ptr", "temporary_cstring_as_ptr"),
("clippy::panic_params", "non_fmt_panics"),
("clippy::unknown_clippy_lints", "unknown_lints"),
("clippy::invalid_atomic_ordering", "invalid_atomic_ordering"),
("clippy::mem_discriminant_non_enum", "enum_intrinsics_non_enums"),
];

View file

@ -1,12 +1,13 @@
//! Test for Clippy lint renames.
// This file was generated by `cargo dev update_lints`.
// Use that command to update this file and do not edit by hand.
// Manual edits will be overwritten.
// run-rustfix
#![allow(dead_code)]
// allow the new lint name here, to test if the new name works
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::new_without_default)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::bind_instead_of_map)]
#![allow(clippy::box_collection)]
#![allow(clippy::blocks_in_if_conditions)]
@ -20,8 +21,8 @@
#![allow(clippy::match_result_ok)]
#![allow(clippy::disallowed_types)]
#![allow(clippy::disallowed_methods)]
#![allow(clippy::needless_borrow)]
#![allow(clippy::recursive_format_impl)]
// uplifted lints
#![allow(invalid_value)]
#![allow(array_into_iter)]
#![allow(unused_labels)]
@ -31,11 +32,10 @@
#![allow(unknown_lints)]
#![allow(invalid_atomic_ordering)]
#![allow(enum_intrinsics_non_enums)]
// warn for the old lint name here, to test if the renaming worked
#![warn(clippy::module_name_repetitions)]
#![warn(clippy::new_without_default)]
#![warn(clippy::redundant_static_lifetimes)]
#![warn(clippy::cognitive_complexity)]
#![warn(clippy::redundant_static_lifetimes)]
#![warn(clippy::bind_instead_of_map)]
#![warn(clippy::box_collection)]
#![warn(clippy::blocks_in_if_conditions)]
@ -57,7 +57,6 @@
#![warn(clippy::disallowed_methods)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::recursive_format_impl)]
// uplifted lints
#![warn(invalid_value)]
#![warn(array_into_iter)]
#![warn(unused_labels)]

View file

@ -1,12 +1,13 @@
//! Test for Clippy lint renames.
// This file was generated by `cargo dev update_lints`.
// Use that command to update this file and do not edit by hand.
// Manual edits will be overwritten.
// run-rustfix
#![allow(dead_code)]
// allow the new lint name here, to test if the new name works
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::new_without_default)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::redundant_static_lifetimes)]
#![allow(clippy::bind_instead_of_map)]
#![allow(clippy::box_collection)]
#![allow(clippy::blocks_in_if_conditions)]
@ -20,8 +21,8 @@
#![allow(clippy::match_result_ok)]
#![allow(clippy::disallowed_types)]
#![allow(clippy::disallowed_methods)]
#![allow(clippy::needless_borrow)]
#![allow(clippy::recursive_format_impl)]
// uplifted lints
#![allow(invalid_value)]
#![allow(array_into_iter)]
#![allow(unused_labels)]
@ -31,11 +32,10 @@
#![allow(unknown_lints)]
#![allow(invalid_atomic_ordering)]
#![allow(enum_intrinsics_non_enums)]
// warn for the old lint name here, to test if the renaming worked
#![warn(clippy::stutter)]
#![warn(clippy::new_without_default_derive)]
#![warn(clippy::const_static_lifetime)]
#![warn(clippy::cyclomatic_complexity)]
#![warn(clippy::const_static_lifetime)]
#![warn(clippy::option_and_then_some)]
#![warn(clippy::box_vec)]
#![warn(clippy::block_in_if_condition_expr)]
@ -57,7 +57,6 @@
#![warn(clippy::disallowed_method)]
#![warn(clippy::ref_in_deref)]
#![warn(clippy::to_string_in_display)]
// uplifted lints
#![warn(clippy::invalid_ref)]
#![warn(clippy::into_iter_on_array)]
#![warn(clippy::unused_label)]

View file

@ -12,18 +12,18 @@ error: lint `clippy::new_without_default_derive` has been renamed to `clippy::ne
LL | #![warn(clippy::new_without_default_derive)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::new_without_default`
error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes`
--> $DIR/rename.rs:37:9
|
LL | #![warn(clippy::const_static_lifetime)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes`
error: lint `clippy::cyclomatic_complexity` has been renamed to `clippy::cognitive_complexity`
--> $DIR/rename.rs:38:9
--> $DIR/rename.rs:37:9
|
LL | #![warn(clippy::cyclomatic_complexity)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::cognitive_complexity`
error: lint `clippy::const_static_lifetime` has been renamed to `clippy::redundant_static_lifetimes`
--> $DIR/rename.rs:38:9
|
LL | #![warn(clippy::const_static_lifetime)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::redundant_static_lifetimes`
error: lint `clippy::option_and_then_some` has been renamed to `clippy::bind_instead_of_map`
--> $DIR/rename.rs:39:9
|
@ -151,55 +151,55 @@ LL | #![warn(clippy::to_string_in_display)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `clippy::recursive_format_impl`
error: lint `clippy::invalid_ref` has been renamed to `invalid_value`
--> $DIR/rename.rs:61:9
--> $DIR/rename.rs:60:9
|
LL | #![warn(clippy::invalid_ref)]
| ^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_value`
error: lint `clippy::into_iter_on_array` has been renamed to `array_into_iter`
--> $DIR/rename.rs:62:9
--> $DIR/rename.rs:61:9
|
LL | #![warn(clippy::into_iter_on_array)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `array_into_iter`
error: lint `clippy::unused_label` has been renamed to `unused_labels`
--> $DIR/rename.rs:63:9
--> $DIR/rename.rs:62:9
|
LL | #![warn(clippy::unused_label)]
| ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unused_labels`
error: lint `clippy::drop_bounds` has been renamed to `drop_bounds`
--> $DIR/rename.rs:64:9
--> $DIR/rename.rs:63:9
|
LL | #![warn(clippy::drop_bounds)]
| ^^^^^^^^^^^^^^^^^^^ help: use the new name: `drop_bounds`
error: lint `clippy::temporary_cstring_as_ptr` has been renamed to `temporary_cstring_as_ptr`
--> $DIR/rename.rs:65:9
--> $DIR/rename.rs:64:9
|
LL | #![warn(clippy::temporary_cstring_as_ptr)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `temporary_cstring_as_ptr`
error: lint `clippy::panic_params` has been renamed to `non_fmt_panics`
--> $DIR/rename.rs:66:9
--> $DIR/rename.rs:65:9
|
LL | #![warn(clippy::panic_params)]
| ^^^^^^^^^^^^^^^^^^^^ help: use the new name: `non_fmt_panics`
error: lint `clippy::unknown_clippy_lints` has been renamed to `unknown_lints`
--> $DIR/rename.rs:67:9
--> $DIR/rename.rs:66:9
|
LL | #![warn(clippy::unknown_clippy_lints)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `unknown_lints`
error: lint `clippy::invalid_atomic_ordering` has been renamed to `invalid_atomic_ordering`
--> $DIR/rename.rs:68:9
--> $DIR/rename.rs:67:9
|
LL | #![warn(clippy::invalid_atomic_ordering)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `invalid_atomic_ordering`
error: lint `clippy::mem_discriminant_non_enum` has been renamed to `enum_intrinsics_non_enums`
--> $DIR/rename.rs:69:9
--> $DIR/rename.rs:68:9
|
LL | #![warn(clippy::mem_discriminant_non_enum)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use the new name: `enum_intrinsics_non_enums`