]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/regex.rs
d9f262fb045a0639f169108fcb1d692b5eefe3ca
[rust.git] / tests / compile-fail / regex.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![allow(unused)]
5 #![deny(invalid_regex, trivial_regex, regex_macro)]
6
7 extern crate regex;
8
9 use regex::{Regex, RegexSet};
10 use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet};
11
12 const OPENING_PAREN : &'static str = "(";
13 const NOT_A_REAL_REGEX : &'static str = "foobar";
14
15 fn syntax_error() {
16     let pipe_in_wrong_position = Regex::new("|");
17     //~^ERROR: regex syntax error: empty alternate
18     let wrong_char_ranice = Regex::new("[z-a]");
19     //~^ERROR: regex syntax error: invalid character class range
20     let some_unicode = Regex::new("[é-è]");
21     //~^ERROR: regex syntax error: invalid character class range
22
23     let some_regex = Regex::new(OPENING_PAREN);
24     //~^ERROR: regex syntax error on position 0: unclosed
25
26     let binary_pipe_in_wrong_position = BRegex::new("|");
27     //~^ERROR: regex syntax error: empty alternate
28     let some_binary_regex = BRegex::new(OPENING_PAREN);
29     //~^ERROR: regex syntax error on position 0: unclosed
30
31     let closing_paren = ")";
32     let not_linted = Regex::new(closing_paren);
33
34     let set = RegexSet::new(&[
35         r"[a-z]+@[a-z]+\.(com|org|net)",
36         r"[a-z]+\.(com|org|net)",
37     ]);
38     let bset = BRegexSet::new(&[
39         r"[a-z]+@[a-z]+\.(com|org|net)",
40         r"[a-z]+\.(com|org|net)",
41     ]);
42
43     let set_error = RegexSet::new(&[
44         OPENING_PAREN,
45         //~^ERROR: regex syntax error on position 0: unclosed
46         r"[a-z]+\.(com|org|net)",
47     ]);
48     let bset_error = BRegexSet::new(&[
49         OPENING_PAREN,
50         //~^ERROR: regex syntax error on position 0: unclosed
51         r"[a-z]+\.(com|org|net)",
52     ]);
53 }
54
55 fn trivial_regex() {
56     let trivial_eq = Regex::new("^foobar$");
57     //~^ERROR: trivial regex
58     //~|HELP consider using `==` on `str`s
59
60     let trivial_starts_with = Regex::new("^foobar");
61     //~^ERROR: trivial regex
62     //~|HELP consider using `str::starts_with`
63
64     let trivial_ends_with = Regex::new("foobar$");
65     //~^ERROR: trivial regex
66     //~|HELP consider using `str::ends_with`
67
68     let trivial_contains = Regex::new("foobar");
69     //~^ERROR: trivial regex
70     //~|HELP consider using `str::contains`
71
72     let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
73     //~^ERROR: trivial regex
74     //~|HELP consider using `str::contains`
75
76     let trivial_backslash = Regex::new("a\\.b");
77     //~^ERROR: trivial regex
78     //~|HELP consider using `str::contains`
79
80     // unlikely corner cases
81     let trivial_empty = Regex::new("");
82     //~^ERROR: trivial regex
83     //~|HELP the regex is unlikely to be useful
84
85     let trivial_empty = Regex::new("^");
86     //~^ERROR: trivial regex
87     //~|HELP the regex is unlikely to be useful
88
89     let trivial_empty = Regex::new("^$");
90     //~^ERROR: trivial regex
91     //~|HELP consider using `str::is_empty`
92
93     let binary_trivial_empty = BRegex::new("^$");
94     //~^ERROR: trivial regex
95     //~|HELP consider using `str::is_empty`
96
97     // non-trivial regexes
98     let non_trivial_dot = Regex::new("a.b");
99     let non_trivial_eq = Regex::new("^foo|bar$");
100     let non_trivial_starts_with = Regex::new("^foo|bar");
101     let non_trivial_ends_with = Regex::new("^foo|bar");
102     let non_trivial_ends_with = Regex::new("foo|bar");
103     let non_trivial_binary = BRegex::new("foo|bar");
104 }
105
106 fn main() {
107     syntax_error();
108     trivial_regex();
109 }