From 51d166f17aa2dd73226152ff7ca6c69f2db5947c Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 25 May 2016 21:36:51 +0200 Subject: [PATCH] Support `RegexBuilder` --- CHANGELOG.md | 4 ++-- src/regex.rs | 4 ++++ src/utils/paths.rs | 2 ++ tests/compile-fail/regex.rs | 14 ++++++++++++-- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f17b1dd0837..928fac7412e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ All notable changes to this project will be documented in this file. ## 0.0.70 — TBD -* [`invalid_regex`] and [`trivial_regex`] can now warn on `RegexSet::new` and - byte regexes +* [`invalid_regex`] and [`trivial_regex`] can now warn on `RegexSet::new`, + `RegexBuilder::new` and byte regexes ## 0.0.69 — 2016-05-20 * Rustup to *rustc 1.10.0-nightly (476fe6eef 2016-05-21)* diff --git a/src/regex.rs b/src/regex.rs index 8876a649e5d..b335bf993b5 100644 --- a/src/regex.rs +++ b/src/regex.rs @@ -104,6 +104,10 @@ fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { check_regex(cx, &args[0], true); } else if match_def_path(cx, def_id, &paths::REGEX_BYTES_NEW) { check_regex(cx, &args[0], false); + } else if match_def_path(cx, def_id, &paths::REGEX_BUILDER_NEW) { + check_regex(cx, &args[0], true); + } else if match_def_path(cx, def_id, &paths::REGEX_BYTES_BUILDER_NEW) { + check_regex(cx, &args[0], false); } else if match_def_path(cx, def_id, &paths::REGEX_SET_NEW) { check_set(cx, &args[0], true); } else if match_def_path(cx, def_id, &paths::REGEX_BYTES_SET_NEW) { diff --git a/src/utils/paths.rs b/src/utils/paths.rs index b0ce8b4a233..3c91578abd0 100644 --- a/src/utils/paths.rs +++ b/src/utils/paths.rs @@ -46,7 +46,9 @@ pub const RANGE_TO_INCLUSIVE_STD: [&'static str; 3] = ["std", "ops", "RangeToInclusive"]; pub const RANGE_TO_STD: [&'static str; 3] = ["std", "ops", "RangeTo"]; pub const REGEX: [&'static str; 3] = ["regex", "re_unicode", "Regex"]; +pub const REGEX_BUILDER_NEW: [&'static str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"]; pub const REGEX_BYTES: [&'static str; 3] = ["regex", "re_bytes", "Regex"]; +pub const REGEX_BYTES_BUILDER_NEW: [&'static str; 5] = ["regex", "re_builder", "bytes", "RegexBuilder", "new"]; pub const REGEX_BYTES_NEW: [&'static str; 4] = ["regex", "re_bytes", "Regex", "new"]; pub const REGEX_BYTES_SET_NEW: [&'static str; 5] = ["regex", "re_set", "bytes", "RegexSet", "new"]; pub const REGEX_NEW: [&'static str; 4] = ["regex", "re_unicode", "Regex", "new"]; diff --git a/tests/compile-fail/regex.rs b/tests/compile-fail/regex.rs index d9f262fb045..d6287b881a1 100644 --- a/tests/compile-fail/regex.rs +++ b/tests/compile-fail/regex.rs @@ -6,8 +6,8 @@ extern crate regex; -use regex::{Regex, RegexSet}; -use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet}; +use regex::{Regex, RegexSet, RegexBuilder}; +use regex::bytes::{Regex as BRegex, RegexSet as BRegexSet, RegexBuilder as BRegexBuilder}; const OPENING_PAREN : &'static str = "("; const NOT_A_REAL_REGEX : &'static str = "foobar"; @@ -15,6 +15,8 @@ fn syntax_error() { let pipe_in_wrong_position = Regex::new("|"); //~^ERROR: regex syntax error: empty alternate + let pipe_in_wrong_position_builder = RegexBuilder::new("|"); + //~^ERROR: regex syntax error: empty alternate let wrong_char_ranice = Regex::new("[z-a]"); //~^ERROR: regex syntax error: invalid character class range let some_unicode = Regex::new("[é-è]"); @@ -27,6 +29,8 @@ fn syntax_error() { //~^ERROR: regex syntax error: empty alternate let some_binary_regex = BRegex::new(OPENING_PAREN); //~^ERROR: regex syntax error on position 0: unclosed + let some_binary_regex_builder = BRegexBuilder::new(OPENING_PAREN); + //~^ERROR: regex syntax error on position 0: unclosed let closing_paren = ")"; let not_linted = Regex::new(closing_paren); @@ -57,6 +61,10 @@ fn trivial_regex() { //~^ERROR: trivial regex //~|HELP consider using `==` on `str`s + let trivial_eq_builder = RegexBuilder::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` @@ -96,11 +104,13 @@ fn trivial_regex() { // non-trivial regexes let non_trivial_dot = Regex::new("a.b"); + let non_trivial_dot_builder = RegexBuilder::new("a.b"); 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"); let non_trivial_binary = BRegex::new("foo|bar"); + let non_trivial_binary_builder = BRegexBuilder::new("foo|bar"); } fn main() { -- 2.44.0