5 #![deny(invalid_regex, trivial_regex, regex_macro)]
9 use regex::{Regex, RegexSet, RegexBuilder};
10 use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet, RegexBuilder as BRegexBuilder};
12 const OPENING_PAREN : &'static str = "(";
13 const NOT_A_REAL_REGEX : &'static str = "foobar";
16 let pipe_in_wrong_position = Regex::new("|");
17 //~^ERROR: regex syntax error: empty alternate
18 let pipe_in_wrong_position_builder = RegexBuilder::new("|");
19 //~^ERROR: regex syntax error: empty alternate
20 let wrong_char_ranice = Regex::new("[z-a]");
21 //~^ERROR: regex syntax error: invalid character class range
22 let some_unicode = Regex::new("[é-è]");
23 //~^ERROR: regex syntax error: invalid character class range
25 let some_regex = Regex::new(OPENING_PAREN);
26 //~^ERROR: regex syntax error on position 0: unclosed
28 let binary_pipe_in_wrong_position = BRegex::new("|");
29 //~^ERROR: regex syntax error: empty alternate
30 let some_binary_regex = BRegex::new(OPENING_PAREN);
31 //~^ERROR: regex syntax error on position 0: unclosed
32 let some_binary_regex_builder = BRegexBuilder::new(OPENING_PAREN);
33 //~^ERROR: regex syntax error on position 0: unclosed
35 let closing_paren = ")";
36 let not_linted = Regex::new(closing_paren);
38 let set = RegexSet::new(&[
39 r"[a-z]+@[a-z]+\.(com|org|net)",
40 r"[a-z]+\.(com|org|net)",
42 let bset = BRegexSet::new(&[
43 r"[a-z]+@[a-z]+\.(com|org|net)",
44 r"[a-z]+\.(com|org|net)",
47 let set_error = RegexSet::new(&[
49 //~^ERROR: regex syntax error on position 0: unclosed
50 r"[a-z]+\.(com|org|net)",
52 let bset_error = BRegexSet::new(&[
54 //~^ERROR: regex syntax error on position 0: unclosed
55 r"[a-z]+\.(com|org|net)",
60 let trivial_eq = Regex::new("^foobar$");
61 //~^ERROR: trivial regex
62 //~|HELP consider using `==` on `str`s
64 let trivial_eq_builder = RegexBuilder::new("^foobar$");
65 //~^ERROR: trivial regex
66 //~|HELP consider using `==` on `str`s
68 let trivial_starts_with = Regex::new("^foobar");
69 //~^ERROR: trivial regex
70 //~|HELP consider using `str::starts_with`
72 let trivial_ends_with = Regex::new("foobar$");
73 //~^ERROR: trivial regex
74 //~|HELP consider using `str::ends_with`
76 let trivial_contains = Regex::new("foobar");
77 //~^ERROR: trivial regex
78 //~|HELP consider using `str::contains`
80 let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
81 //~^ERROR: trivial regex
82 //~|HELP consider using `str::contains`
84 let trivial_backslash = Regex::new("a\\.b");
85 //~^ERROR: trivial regex
86 //~|HELP consider using `str::contains`
88 // unlikely corner cases
89 let trivial_empty = Regex::new("");
90 //~^ERROR: trivial regex
91 //~|HELP the regex is unlikely to be useful
93 let trivial_empty = Regex::new("^");
94 //~^ERROR: trivial regex
95 //~|HELP the regex is unlikely to be useful
97 let trivial_empty = Regex::new("^$");
98 //~^ERROR: trivial regex
99 //~|HELP consider using `str::is_empty`
101 let binary_trivial_empty = BRegex::new("^$");
102 //~^ERROR: trivial regex
103 //~|HELP consider using `str::is_empty`
105 // non-trivial regexes
106 let non_trivial_dot = Regex::new("a.b");
107 let non_trivial_dot_builder = RegexBuilder::new("a.b");
108 let non_trivial_eq = Regex::new("^foo|bar$");
109 let non_trivial_starts_with = Regex::new("^foo|bar");
110 let non_trivial_ends_with = Regex::new("^foo|bar");
111 let non_trivial_ends_with = Regex::new("foo|bar");
112 let non_trivial_binary = BRegex::new("foo|bar");
113 let non_trivial_binary_builder = BRegexBuilder::new("foo|bar");