rust/tests/compile-fail/regex.rs

120 lines
3.8 KiB
Rust
Raw Normal View History

2016-02-04 17:36:06 -06:00
#![feature(plugin)]
#![plugin(clippy)]
2016-02-04 17:36:06 -06:00
#![allow(unused)]
2016-02-07 15:50:54 -06:00
#![deny(invalid_regex, trivial_regex, regex_macro)]
2016-02-04 17:36:06 -06:00
extern crate regex;
2016-05-25 14:36:51 -05:00
use regex::{Regex, RegexSet, RegexBuilder};
use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet, RegexBuilder as BRegexBuilder};
2016-02-04 17:36:06 -06:00
2016-02-05 09:48:35 -06:00
const OPENING_PAREN : &'static str = "(";
2016-02-05 16:10:48 -06:00
const NOT_A_REAL_REGEX : &'static 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("|");
2016-02-05 14:54:29 -06:00
//~^ERROR: regex syntax error: empty alternate
2016-05-25 14:36:51 -05:00
let pipe_in_wrong_position_builder = RegexBuilder::new("|");
//~^ERROR: regex syntax error: empty alternate
2016-02-05 14:54:29 -06:00
let wrong_char_ranice = Regex::new("[z-a]");
//~^ERROR: regex syntax error: invalid character class range
2016-05-07 17:56:23 -05:00
let some_unicode = Regex::new("[é-è]");
//~^ERROR: regex syntax error: invalid character class range
2016-02-05 14:54:29 -06:00
2016-02-05 09:48:35 -06:00
let some_regex = Regex::new(OPENING_PAREN);
2016-02-05 14:54:29 -06:00
//~^ERROR: regex syntax error on position 0: unclosed
2016-02-05 09:48:35 -06:00
2016-05-25 10:15:19 -05:00
let binary_pipe_in_wrong_position = BRegex::new("|");
//~^ERROR: regex syntax error: empty alternate
let some_binary_regex = BRegex::new(OPENING_PAREN);
//~^ERROR: regex syntax error on position 0: unclosed
2016-05-25 14:36:51 -05:00
let some_binary_regex_builder = BRegexBuilder::new(OPENING_PAREN);
//~^ERROR: regex syntax error on position 0: unclosed
2016-05-25 10:15:19 -05: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
let set = RegexSet::new(&[
r"[a-z]+@[a-z]+\.(com|org|net)",
r"[a-z]+\.(com|org|net)",
]);
let bset = BRegexSet::new(&[
r"[a-z]+@[a-z]+\.(com|org|net)",
r"[a-z]+\.(com|org|net)",
]);
let set_error = RegexSet::new(&[
OPENING_PAREN,
//~^ERROR: regex syntax error on position 0: unclosed
r"[a-z]+\.(com|org|net)",
]);
let bset_error = BRegexSet::new(&[
OPENING_PAREN,
//~^ERROR: regex syntax error on position 0: unclosed
r"[a-z]+\.(com|org|net)",
]);
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$");
//~^ERROR: trivial regex
//~|HELP consider using `==` on `str`s
2016-05-25 14:36:51 -05:00
let trivial_eq_builder = RegexBuilder::new("^foobar$");
//~^ERROR: trivial regex
//~|HELP consider using `==` on `str`s
2016-02-05 16:10:48 -06:00
let trivial_starts_with = Regex::new("^foobar");
//~^ERROR: trivial regex
//~|HELP consider using `str::starts_with`
let trivial_ends_with = Regex::new("foobar$");
//~^ERROR: trivial regex
//~|HELP consider using `str::ends_with`
let trivial_contains = Regex::new("foobar");
//~^ERROR: trivial regex
//~|HELP consider using `str::contains`
let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
//~^ERROR: trivial regex
//~|HELP consider using `str::contains`
2016-02-06 11:06:39 -06:00
let trivial_backslash = Regex::new("a\\.b");
//~^ERROR: trivial regex
//~|HELP consider using `str::contains`
2016-02-05 16:10:48 -06:00
// unlikely corner cases
let trivial_empty = Regex::new("");
//~^ERROR: trivial regex
//~|HELP the regex is unlikely to be useful
2016-02-06 11:06:39 -06:00
let trivial_empty = Regex::new("^");
//~^ERROR: trivial regex
//~|HELP the regex is unlikely to be useful
2016-02-05 16:10:48 -06:00
let trivial_empty = Regex::new("^$");
//~^ERROR: trivial regex
//~|HELP consider using `str::is_empty`
2016-05-25 10:15:19 -05:00
let binary_trivial_empty = BRegex::new("^$");
//~^ERROR: trivial regex
//~|HELP consider using `str::is_empty`
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");
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");
2016-02-05 16:10:48 -06:00
}
fn main() {
syntax_error();
trivial_regex();
}