You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: clippy_lints/src/regex.rs
+61-1Lines changed: 61 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -55,6 +55,44 @@ declare_clippy_lint! {
55
55
"trivial regular expressions"
56
56
}
57
57
58
+
declare_clippy_lint!{
59
+
/// ### What it does
60
+
///
61
+
/// Checks for [regex](https://crates.io/crates/regex) compilation inside a loop with a literal.
62
+
///
63
+
/// ### Why is this bad?
64
+
///
65
+
/// Compiling a regex is a much more expensive operation than using one, and a compiled regex can be used multiple times.
66
+
/// This is documented as an antipattern [on the regex documentation](https://docs.rs/regex/latest/regex/#avoid-re-compiling-regexes-especially-in-a-loop)
67
+
///
68
+
/// ### Example
69
+
/// ```no_run
70
+
/// # let haystacks = [""];
71
+
/// # const MY_REGEX: &str = "a.b";
72
+
/// for haystack in haystacks {
73
+
/// let regex = regex::Regex::new(MY_REGEX).unwrap();
74
+
/// if regex.is_match(haystack) {
75
+
/// // Perform operation
76
+
/// }
77
+
/// }
78
+
/// ```
79
+
/// can be replaced with
80
+
/// ```no_run
81
+
/// # let haystacks = [""];
82
+
/// # const MY_REGEX: &str = "a.b";
83
+
/// let regex = regex::Regex::new(MY_REGEX).unwrap();
84
+
/// for haystack in haystacks {
85
+
/// if regex.is_match(haystack) {
86
+
/// // Perform operation
87
+
/// }
88
+
/// }
89
+
/// ```
90
+
#[clippy::version = "1.83.0"]
91
+
pubREGEX_CREATION_IN_LOOPS,
92
+
perf,
93
+
"regular expression compilation performed in a loop"
0 commit comments