rust-clippy/tests/ui/regex.rs

121 lines
4 KiB
Rust
Raw Normal View History

#![allow(
unused,
clippy::needless_raw_strings,
clippy::needless_raw_string_hashes,
clippy::needless_borrow,
clippy::needless_borrows_for_generic_args
)]
2020-07-01 10:36:36 +00:00
#![warn(clippy::invalid_regex, clippy::trivial_regex)]
2016-02-04 23:36:06 +00:00
extern crate regex;
2018-12-09 22:26:16 +00:00
use regex::bytes::{Regex as BRegex, RegexBuilder as BRegexBuilder, RegexSet as BRegexSet};
use regex::{Regex, RegexBuilder, RegexSet};
2016-02-04 23:36:06 +00:00
const OPENING_PAREN: &str = "(";
const NOT_A_REAL_REGEX: &str = "foobar";
2016-02-05 15:48:35 +00:00
2016-02-05 22:10:48 +00:00
fn syntax_error() {
2016-02-04 23:36:06 +00:00
let pipe_in_wrong_position = Regex::new("|");
//~^ ERROR: trivial regex
2016-05-25 19:36:51 +00:00
let pipe_in_wrong_position_builder = RegexBuilder::new("|");
//~^ ERROR: trivial regex
2016-02-05 20:54:29 +00:00
let wrong_char_ranice = Regex::new("[z-a]");
//~^ 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 22:56:23 +00:00
let some_unicode = Regex::new("[é-è]");
//~^ ERROR: regex syntax error: invalid character class range, the start must be <= th
2017-02-08 13:58:07 +00:00
2016-02-05 15:48:35 +00:00
let some_regex = Regex::new(OPENING_PAREN);
2017-02-08 13:58:07 +00:00
2016-05-25 15:15:19 +00:00
let binary_pipe_in_wrong_position = BRegex::new("|");
//~^ ERROR: trivial regex
2016-05-25 15:15:19 +00:00
let some_binary_regex = BRegex::new(OPENING_PAREN);
2016-05-25 19:36:51 +00:00
let some_binary_regex_builder = BRegexBuilder::new(OPENING_PAREN);
2017-02-08 13:58:07 +00:00
2016-02-05 15:48:35 +00:00
let closing_paren = ")";
let not_linted = Regex::new(closing_paren);
2016-05-25 15:15:19 +00:00
2018-12-09 22:26:16 +00:00
let set = RegexSet::new(&[r"[a-z]+@[a-z]+\.(com|org|net)", r"[a-z]+\.(com|org|net)"]);
2016-05-25 15:15:19 +00:00
let bset = BRegexSet::new(&[
r"[a-z]+@[a-z]+\.(com|org|net)",
r"[a-z]+\.(com|org|net)",
2018-04-07 20:18:51 +00:00
r".", // regression test
2016-05-25 15:15:19 +00:00
]);
2018-12-09 22:26:16 +00: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)"]);
// These following three cases are considering valid since regex-1.8.0
let raw_string_error = Regex::new(r"[...\/...]");
let raw_string_error = Regex::new(r#"[...\/...]"#);
let _ = Regex::new(r"(?<hi>hi)").unwrap();
let escaped_string_span = Regex::new("\\b\\c");
let aux_span = Regex::new("(?ixi)");
//~^ ERROR: regex syntax error: duplicate flag
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).");
//~^ ERROR: regex syntax error: pattern can match invalid UTF-8
2016-02-04 23:36:06 +00:00
}
2016-02-05 22:10:48 +00:00
fn trivial_regex() {
let trivial_eq = Regex::new("^foobar$");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-05-25 19:36:51 +00:00
let trivial_eq_builder = RegexBuilder::new("^foobar$");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
let trivial_starts_with = Regex::new("^foobar");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
let trivial_ends_with = Regex::new("foobar$");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
let trivial_contains = Regex::new("foobar");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-06 17:06:39 +00:00
let trivial_backslash = Regex::new("a\\.b");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
// unlikely corner cases
let trivial_empty = Regex::new("");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-06 17:06:39 +00:00
let trivial_empty = Regex::new("^");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
let trivial_empty = Regex::new("^$");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-05-25 15:15:19 +00:00
let binary_trivial_empty = BRegex::new("^$");
//~^ ERROR: trivial regex
2017-02-08 13:58:07 +00:00
2016-02-05 22:10:48 +00:00
// non-trivial regexes
2016-02-06 17:06:39 +00:00
let non_trivial_dot = Regex::new("a.b");
2016-05-25 19:36:51 +00:00
let non_trivial_dot_builder = RegexBuilder::new("a.b");
let non_trivial_dot = Regex::new(".");
let non_trivial_dot = BRegex::new(".");
2016-02-05 22:10:48 +00: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 15:15:19 +00:00
let non_trivial_binary = BRegex::new("foo|bar");
2016-05-25 19:36:51 +00:00
let non_trivial_binary_builder = BRegexBuilder::new("foo|bar");
// #6005: unicode classes in bytes::Regex
let a_byte_of_unicode = BRegex::new(r"\p{C}");
2016-02-05 22:10:48 +00:00
}
fn main() {
syntax_error();
trivial_regex();
}