]> git.lizzy.rs Git - rust.git/blob - tests/compile-fail/regex.rs
Fix deploy.sh III
[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, RegexBuilder};
10 use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet, RegexBuilder as BRegexBuilder};
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 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
24
25     let some_regex = Regex::new(OPENING_PAREN);
26     //~^ERROR: regex syntax error on position 0: unclosed
27
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
34
35     let closing_paren = ")";
36     let not_linted = Regex::new(closing_paren);
37
38     let set = RegexSet::new(&[
39         r"[a-z]+@[a-z]+\.(com|org|net)",
40         r"[a-z]+\.(com|org|net)",
41     ]);
42     let bset = BRegexSet::new(&[
43         r"[a-z]+@[a-z]+\.(com|org|net)",
44         r"[a-z]+\.(com|org|net)",
45     ]);
46
47     let set_error = RegexSet::new(&[
48         OPENING_PAREN,
49         //~^ERROR: regex syntax error on position 0: unclosed
50         r"[a-z]+\.(com|org|net)",
51     ]);
52     let bset_error = BRegexSet::new(&[
53         OPENING_PAREN,
54         //~^ERROR: regex syntax error on position 0: unclosed
55         r"[a-z]+\.(com|org|net)",
56     ]);
57 }
58
59 fn trivial_regex() {
60     let trivial_eq = Regex::new("^foobar$");
61     //~^ERROR: trivial regex
62     //~|HELP consider using `==` on `str`s
63
64     let trivial_eq_builder = RegexBuilder::new("^foobar$");
65     //~^ERROR: trivial regex
66     //~|HELP consider using `==` on `str`s
67
68     let trivial_starts_with = Regex::new("^foobar");
69     //~^ERROR: trivial regex
70     //~|HELP consider using `str::starts_with`
71
72     let trivial_ends_with = Regex::new("foobar$");
73     //~^ERROR: trivial regex
74     //~|HELP consider using `str::ends_with`
75
76     let trivial_contains = Regex::new("foobar");
77     //~^ERROR: trivial regex
78     //~|HELP consider using `str::contains`
79
80     let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
81     //~^ERROR: trivial regex
82     //~|HELP consider using `str::contains`
83
84     let trivial_backslash = Regex::new("a\\.b");
85     //~^ERROR: trivial regex
86     //~|HELP consider using `str::contains`
87
88     // unlikely corner cases
89     let trivial_empty = Regex::new("");
90     //~^ERROR: trivial regex
91     //~|HELP the regex is unlikely to be useful
92
93     let trivial_empty = Regex::new("^");
94     //~^ERROR: trivial regex
95     //~|HELP the regex is unlikely to be useful
96
97     let trivial_empty = Regex::new("^$");
98     //~^ERROR: trivial regex
99     //~|HELP consider using `str::is_empty`
100
101     let binary_trivial_empty = BRegex::new("^$");
102     //~^ERROR: trivial regex
103     //~|HELP consider using `str::is_empty`
104
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");
114 }
115
116 fn main() {
117     syntax_error();
118     trivial_regex();
119 }