2023-06-08 06:55:47 -05:00
|
|
|
#![allow(
|
|
|
|
unused,
|
2023-06-15 14:15:10 -05:00
|
|
|
clippy::needless_raw_strings,
|
2023-06-08 06:55:47 -05:00
|
|
|
clippy::needless_raw_string_hashes,
|
2023-09-15 06:07:34 -05:00
|
|
|
clippy::needless_borrow,
|
|
|
|
clippy::needless_borrows_for_generic_args
|
2023-06-08 06:55:47 -05:00
|
|
|
)]
|
2024-09-21 06:46:08 -05:00
|
|
|
#![warn(clippy::invalid_regex, clippy::trivial_regex, clippy::regex_creation_in_loops)]
|
2016-02-04 17:36:06 -06:00
|
|
|
|
|
|
|
extern crate regex;
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
use regex::bytes::{Regex as BRegex, RegexBuilder as BRegexBuilder, RegexSet as BRegexSet};
|
|
|
|
use regex::{Regex, RegexBuilder, RegexSet};
|
2016-02-04 17:36:06 -06:00
|
|
|
|
2017-10-20 08:51:35 -05:00
|
|
|
const OPENING_PAREN: &str = "(";
|
|
|
|
const NOT_A_REAL_REGEX: &str = "foobar";
|
2016-02-05 09:48:35 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
fn syntax_error() {
|
2016-02-04 17:36:06 -06:00
|
|
|
let pipe_in_wrong_position = Regex::new("|");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2016-05-25 14:36:51 -05:00
|
|
|
let pipe_in_wrong_position_builder = RegexBuilder::new("|");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2016-02-05 14:54:29 -06:00
|
|
|
let wrong_char_ranice = Regex::new("[z-a]");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: regex syntax error: invalid character class range, the start must be <= th
|
|
|
|
//~| NOTE: `-D clippy::invalid-regex` implied by `-D warnings`
|
2016-05-07 17:56:23 -05:00
|
|
|
let some_unicode = Regex::new("[é-è]");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: regex syntax error: invalid character class range, the start must be <= th
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 09:48:35 -06:00
|
|
|
let some_regex = Regex::new(OPENING_PAREN);
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-05-25 10:15:19 -05:00
|
|
|
let binary_pipe_in_wrong_position = BRegex::new("|");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2016-05-25 10:15:19 -05:00
|
|
|
let some_binary_regex = BRegex::new(OPENING_PAREN);
|
2016-05-25 14:36:51 -05:00
|
|
|
let some_binary_regex_builder = BRegexBuilder::new(OPENING_PAREN);
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 09:48:35 -06:00
|
|
|
let closing_paren = ")";
|
|
|
|
let not_linted = Regex::new(closing_paren);
|
2016-05-25 10:15:19 -05:00
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
let set = RegexSet::new(&[r"[a-z]+@[a-z]+\.(com|org|net)", r"[a-z]+\.(com|org|net)"]);
|
2016-05-25 10:15:19 -05:00
|
|
|
let bset = BRegexSet::new(&[
|
|
|
|
r"[a-z]+@[a-z]+\.(com|org|net)",
|
|
|
|
r"[a-z]+\.(com|org|net)",
|
2018-04-07 15:18:51 -05:00
|
|
|
r".", // regression test
|
2016-05-25 10:15:19 -05:00
|
|
|
]);
|
|
|
|
|
2018-12-09 16:26:16 -06:00
|
|
|
let set_error = RegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]);
|
|
|
|
let bset_error = BRegexSet::new(&[OPENING_PAREN, r"[a-z]+\.(com|org|net)"]);
|
2018-01-23 09:52:14 -06:00
|
|
|
|
2023-04-21 22:17:10 -05:00
|
|
|
// These following three cases are considering valid since regex-1.8.0
|
2018-01-23 09:52:14 -06:00
|
|
|
let raw_string_error = Regex::new(r"[...\/...]");
|
|
|
|
let raw_string_error = Regex::new(r#"[...\/...]"#);
|
2023-04-21 22:17:10 -05:00
|
|
|
let _ = Regex::new(r"(?<hi>hi)").unwrap();
|
2023-01-26 09:30:44 -06:00
|
|
|
|
|
|
|
let escaped_string_span = Regex::new("\\b\\c");
|
|
|
|
|
|
|
|
let aux_span = Regex::new("(?ixi)");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: regex syntax error: duplicate flag
|
2023-05-28 06:53:03 -05:00
|
|
|
|
|
|
|
let should_not_lint = Regex::new("(?u).");
|
|
|
|
let should_not_lint = BRegex::new("(?u).");
|
|
|
|
let invalid_utf8_should_not_lint = BRegex::new("(?-u).");
|
|
|
|
let invalid_utf8_should_lint = Regex::new("(?-u).");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: regex syntax error: pattern can match invalid UTF-8
|
2016-02-04 17:36:06 -06:00
|
|
|
}
|
2016-02-05 16:10:48 -06:00
|
|
|
|
|
|
|
fn trivial_regex() {
|
|
|
|
let trivial_eq = Regex::new("^foobar$");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-05-25 14:36:51 -05:00
|
|
|
let trivial_eq_builder = RegexBuilder::new("^foobar$");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
let trivial_starts_with = Regex::new("^foobar");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
let trivial_ends_with = Regex::new("foobar$");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
let trivial_contains = Regex::new("foobar");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-06 11:06:39 -06:00
|
|
|
let trivial_backslash = Regex::new("a\\.b");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
// unlikely corner cases
|
|
|
|
let trivial_empty = Regex::new("");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-06 11:06:39 -06:00
|
|
|
let trivial_empty = Regex::new("^");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
let trivial_empty = Regex::new("^$");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-05-25 10:15:19 -05:00
|
|
|
let binary_trivial_empty = BRegex::new("^$");
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: trivial regex
|
2017-02-08 07:58:07 -06:00
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
// non-trivial regexes
|
2016-02-06 11:06:39 -06:00
|
|
|
let non_trivial_dot = Regex::new("a.b");
|
2016-05-25 14:36:51 -05:00
|
|
|
let non_trivial_dot_builder = RegexBuilder::new("a.b");
|
2023-05-28 06:53:03 -05:00
|
|
|
let non_trivial_dot = Regex::new(".");
|
|
|
|
let non_trivial_dot = BRegex::new(".");
|
2016-02-05 16:10:48 -06:00
|
|
|
let non_trivial_eq = Regex::new("^foo|bar$");
|
|
|
|
let non_trivial_starts_with = Regex::new("^foo|bar");
|
|
|
|
let non_trivial_ends_with = Regex::new("^foo|bar");
|
|
|
|
let non_trivial_ends_with = Regex::new("foo|bar");
|
2016-05-25 10:15:19 -05:00
|
|
|
let non_trivial_binary = BRegex::new("foo|bar");
|
2016-05-25 14:36:51 -05:00
|
|
|
let non_trivial_binary_builder = BRegexBuilder::new("foo|bar");
|
2020-10-07 18:07:00 -05:00
|
|
|
|
|
|
|
// #6005: unicode classes in bytes::Regex
|
|
|
|
let a_byte_of_unicode = BRegex::new(r"\p{C}");
|
2023-12-02 07:04:39 -06:00
|
|
|
|
2024-01-02 18:21:51 -06:00
|
|
|
// start and end word boundary, introduced in regex 0.10
|
2023-12-02 07:04:39 -06:00
|
|
|
let _ = BRegex::new(r"\<word\>");
|
|
|
|
let _ = BRegex::new(r"\b{start}word\b{end}");
|
2016-02-05 16:10:48 -06:00
|
|
|
}
|
|
|
|
|
2024-09-21 06:46:08 -05:00
|
|
|
fn regex_creation_in_loops() {
|
|
|
|
loop {
|
2024-10-05 11:08:33 -05:00
|
|
|
static STATIC_REGEX: std::sync::LazyLock<Regex> = std::sync::LazyLock::new(|| Regex::new("a.b").unwrap());
|
|
|
|
|
2024-09-21 06:46:08 -05:00
|
|
|
let regex = Regex::new("a.b");
|
|
|
|
//~^ ERROR: compiling a regex in a loop
|
|
|
|
let regex = BRegex::new("a.b");
|
|
|
|
//~^ ERROR: compiling a regex in a loop
|
|
|
|
#[allow(clippy::regex_creation_in_loops)]
|
|
|
|
let allowed_regex = Regex::new("a.b");
|
|
|
|
|
|
|
|
if true {
|
|
|
|
let regex = Regex::new("a.b");
|
|
|
|
//~^ ERROR: compiling a regex in a loop
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..10 {
|
|
|
|
let nested_regex = Regex::new("a.b");
|
|
|
|
//~^ ERROR: compiling a regex in a loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i in 0..10 {
|
|
|
|
let dependant_regex = Regex::new(&format!("{i}"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:10:48 -06:00
|
|
|
fn main() {
|
|
|
|
syntax_error();
|
|
|
|
trivial_regex();
|
2024-09-21 06:46:08 -05:00
|
|
|
regex_creation_in_loops();
|
2016-02-05 16:10:48 -06:00
|
|
|
}
|