Skip to content

Use LazyLock for lint_message_convention regexes #9169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 32 additions & 28 deletions tests/lint_message_convention.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#![feature(once_cell)]
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
#![warn(rust_2018_idioms, unused_lifetimes)]

use std::ffi::OsStr;
use std::path::PathBuf;
use std::sync::LazyLock;

use regex::RegexSet;

Expand All @@ -14,43 +16,45 @@ struct Message {

impl Message {
fn new(path: PathBuf) -> Self {
let content: String = std::fs::read_to_string(&path).unwrap();
// we don't want the first letter after "error: ", "help: " ... to be capitalized
// also no punctuation (except for "?" ?) at the end of a line
let regex_set: RegexSet = RegexSet::new(&[
r"error: [A-Z]",
r"help: [A-Z]",
r"warning: [A-Z]",
r"note: [A-Z]",
r"try this: [A-Z]",
r"error: .*[.!]$",
r"help: .*[.!]$",
r"warning: .*[.!]$",
r"note: .*[.!]$",
r"try this: .*[.!]$",
])
.unwrap();
static REGEX_SET: LazyLock<RegexSet> = LazyLock::new(|| {
RegexSet::new(&[
r"error: [A-Z]",
r"help: [A-Z]",
r"warning: [A-Z]",
r"note: [A-Z]",
r"try this: [A-Z]",
r"error: .*[.!]$",
r"help: .*[.!]$",
r"warning: .*[.!]$",
r"note: .*[.!]$",
r"try this: .*[.!]$",
])
.unwrap()
});

// sometimes the first character is capitalized and it is legal (like in "C-like enum variants") or
// we want to ask a question ending in "?"
let exceptions_set: RegexSet = RegexSet::new(&[
r".*C-like enum variant discriminant is not portable to 32-bit targets",
r".*did you mean `unix`?",
r".*the arguments may be inverted...",
r".*Intel x86 assembly syntax used",
r".*AT&T x86 assembly syntax used",
r".*remove .*the return type...",
r"note: Clippy version: .*",
r"the compiler unexpectedly panicked. this is a bug.",
r"remove the `if let` statement in the for loop and then...",
])
.unwrap();
static EXCEPTIONS_SET: LazyLock<RegexSet> = LazyLock::new(|| {
RegexSet::new(&[
r"\.\.\.$",
r".*C-like enum variant discriminant is not portable to 32-bit targets",
r".*Intel x86 assembly syntax used",
r".*AT&T x86 assembly syntax used",
r"note: Clippy version: .*",
r"the compiler unexpectedly panicked. this is a bug.",
])
.unwrap()
});

let content: String = std::fs::read_to_string(&path).unwrap();

let bad_lines = content
.lines()
.filter(|line| regex_set.matches(line).matched_any())
.filter(|line| REGEX_SET.matches(line).matched_any())
// ignore exceptions
.filter(|line| !exceptions_set.matches(line).matched_any())
.filter(|line| !EXCEPTIONS_SET.matches(line).matched_any())
.map(ToOwned::to_owned)
.collect::<Vec<String>>();

Expand Down