rust-clippy/tests/compile-fail/regex.rs

77 lines
2.1 KiB
Rust
Raw Normal View History

2016-02-04 23:36:06 +00:00
#![feature(plugin)]
#![plugin(clippy)]
#![allow(unused)]
2016-02-05 22:10:48 +00:00
#![deny(invalid_regex, trivial_regex)]
2016-02-04 23:36:06 +00:00
extern crate regex;
use regex::Regex;
2016-02-05 15:48:35 +00:00
const OPENING_PAREN : &'static str = "(";
2016-02-05 22:10:48 +00:00
const NOT_A_REAL_REGEX : &'static 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("|");
2016-02-05 20:54:29 +00:00
//~^ERROR: regex syntax error: empty alternate
let wrong_char_ranice = Regex::new("[z-a]");
//~^ERROR: regex syntax error: invalid character class range
2016-02-05 15:48:35 +00:00
let some_regex = Regex::new(OPENING_PAREN);
2016-02-05 20:54:29 +00:00
//~^ERROR: regex syntax error on position 0: unclosed
2016-02-05 15:48:35 +00:00
let closing_paren = ")";
let not_linted = Regex::new(closing_paren);
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
//~|HELP consider using `==` on `str`s
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 17:06:39 +00:00
let trivial_backslash = Regex::new("a\\.b");
//~^ERROR: trivial regex
//~|HELP consider using `str::contains`
2016-02-05 22:10:48 +00:00
// unlikely corner cases
let trivial_empty = Regex::new("");
//~^ERROR: trivial regex
//~|HELP the regex is unlikely to be useful
2016-02-06 17:06:39 +00:00
let trivial_empty = Regex::new("^");
//~^ERROR: trivial regex
//~|HELP the regex is unlikely to be useful
2016-02-05 22:10:48 +00:00
let trivial_empty = Regex::new("^$");
//~^ERROR: trivial regex
//~|HELP consider using `str::is_empty`
// non-trivial regexes
2016-02-06 17:06:39 +00:00
let non_trivial_dot = Regex::new("a.b");
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");
}
fn main() {
syntax_error();
trivial_regex();
}