Skip to content

Commit dcd80e5

Browse files
committed
Support Regex::with_size_limit as well as new
1 parent c669046 commit dcd80e5

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

src/regex.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,52 +96,58 @@ impl LateLintPass for RegexPass {
9696
fn check_expr(&mut self, cx: &LateContext, expr: &Expr) {
9797
if_let_chain!{[
9898
let ExprCall(ref fun, ref args) = expr.node,
99-
args.len() == 1
99+
args.len() >= 1,
100+
let Some(def) = cx.tcx.def_map.borrow().get(&fun.id)
100101
], {
101-
let def_id = cx.tcx.def_map.borrow()[&fun.id].def_id();
102+
let (utf8_path, bytes_path, re_idx) = match args.len() {
103+
1 => (paths::REGEX_NEW, paths::REGEX_BYTES_NEW, 0),
104+
2 => (paths::REGEX_WSL, paths::REGEX_BYTES_WSL, 1),
105+
_ => return,
106+
};
102107

103-
let utf8 = if match_def_path(cx, def_id, &paths::REGEX_NEW) {
108+
let def_id = def.def_id();
109+
let utf8 = if match_def_path(cx, def_id, utf8_path) {
104110
true
105-
} else if match_def_path(cx, def_id, &paths::REGEX_BYTES_NEW) {
111+
} else if match_def_path(cx, def_id, bytes_path) {
106112
false
107113
} else {
108114
return;
109115
};
110116

111117
let builder = regex_syntax::ExprBuilder::new().unicode(utf8);
112118

113-
if let ExprLit(ref lit) = args[0].node {
119+
if let ExprLit(ref lit) = args[re_idx].node {
114120
if let LitKind::Str(ref r, _) = lit.node {
115121
match builder.parse(r) {
116122
Ok(r) => {
117123
if let Some(repl) = is_trivial_regex(&r) {
118-
span_help_and_lint(cx, TRIVIAL_REGEX, args[0].span,
124+
span_help_and_lint(cx, TRIVIAL_REGEX, args[re_idx].span,
119125
"trivial regex",
120126
&format!("consider using {}", repl));
121127
}
122128
}
123129
Err(e) => {
124130
span_lint(cx,
125131
INVALID_REGEX,
126-
str_span(args[0].span, &r, e.position()),
132+
str_span(args[re_idx].span, &r, e.position()),
127133
&format!("regex syntax error: {}",
128134
e.description()));
129135
}
130136
}
131137
}
132-
} else if let Some(r) = const_str(cx, &*args[0]) {
138+
} else if let Some(r) = const_str(cx, &*args[re_idx]) {
133139
match builder.parse(&r) {
134140
Ok(r) => {
135141
if let Some(repl) = is_trivial_regex(&r) {
136-
span_help_and_lint(cx, TRIVIAL_REGEX, args[0].span,
142+
span_help_and_lint(cx, TRIVIAL_REGEX, args[re_idx].span,
137143
"trivial regex",
138144
&format!("consider using {}", repl));
139145
}
140146
}
141147
Err(e) => {
142148
span_lint(cx,
143149
INVALID_REGEX,
144-
args[0].span,
150+
args[re_idx].span,
145151
&format!("regex syntax error on position {}: {}",
146152
e.position(),
147153
e.description()));

src/utils/paths.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ pub const RANGE_TO_INCLUSIVE: [&'static str; 3] = ["core", "ops", "RangeToInclus
4646
pub const RANGE_TO_INCLUSIVE_STD: [&'static str; 3] = ["std", "ops", "RangeToInclusive"];
4747
pub const RANGE_TO_STD: [&'static str; 3] = ["std", "ops", "RangeTo"];
4848
pub const REGEX: [&'static str; 3] = ["regex", "re_unicode", "Regex"];
49-
pub const REGEX_BYTES: [&'static str; 3] = ["regex", "re_bytes", "Regex"];
50-
pub const REGEX_BYTES_NEW: [&'static str; 4] = ["regex", "re_bytes", "Regex", "new"];
51-
pub const REGEX_NEW: [&'static str; 4] = ["regex", "re_unicode", "Regex", "new"];
49+
pub const REGEX_BYTES: &'static [&'static str] = &["regex", "re_bytes", "Regex"];
50+
pub const REGEX_BYTES_NEW: &'static [&'static str] = &["regex", "re_bytes", "Regex", "new"];
51+
pub const REGEX_BYTES_WSL: &'static [&'static str] = &["regex", "re_bytes", "Regex", "with_size_limit"];
52+
pub const REGEX_NEW: &'static [&'static str] = &["regex", "re_unicode", "Regex", "new"];
53+
pub const REGEX_WSL: &'static [&'static str] = &["regex", "re_unicode", "Regex", "with_size_limit"];
5254
pub const RESULT: [&'static str; 3] = ["core", "result", "Result"];
5355
pub const STRING: [&'static str; 3] = ["collections", "string", "String"];
5456
pub const TRANSMUTE: [&'static str; 4] = ["core", "intrinsics", "", "transmute"];

tests/compile-fail/regex.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ const NOT_A_REAL_REGEX : &'static str = "foobar";
1515
fn syntax_error() {
1616
let pipe_in_wrong_position = Regex::new("|");
1717
//~^ERROR: regex syntax error: empty alternate
18+
let pipe_in_wrong_position2 = Regex::with_size_limit(42, "|");
19+
//~^ERROR: regex syntax error: empty alternate
1820
let wrong_char_ranice = Regex::new("[z-a]");
1921
//~^ERROR: regex syntax error: invalid character class range
22+
let wrong_char_ranice2 = Regex::with_size_limit(42, "[z-a]");
23+
//~^ERROR: regex syntax error: invalid character class range
2024
let some_unicode = Regex::new("[é-è]");
2125
//~^ERROR: regex syntax error: invalid character class range
2226

@@ -25,18 +29,28 @@ fn syntax_error() {
2529

2630
let binary_pipe_in_wrong_position = BRegex::new("|");
2731
//~^ERROR: regex syntax error: empty alternate
32+
let binary_pipe_in_wrong_position = BRegex::with_size_limit(42, "|");
33+
//~^ERROR: regex syntax error: empty alternate
34+
2835
let some_binary_regex = BRegex::new(OPENING_PAREN);
2936
//~^ERROR: regex syntax error on position 0: unclosed
37+
let some_binary_regex2 = BRegex::with_size_limit(42, OPENING_PAREN);
38+
//~^ERROR: regex syntax error on position 0: unclosed
3039

3140
let closing_paren = ")";
3241
let not_linted = Regex::new(closing_paren);
42+
let not_linted2 = Regex::with_size_limit(42, closing_paren);
3343
}
3444

3545
fn trivial_regex() {
3646
let trivial_eq = Regex::new("^foobar$");
3747
//~^ERROR: trivial regex
3848
//~|HELP consider using `==` on `str`s
3949

50+
let trivial_eq2 = Regex::with_size_limit(42, "^foobar$");
51+
//~^ERROR: trivial regex
52+
//~|HELP consider using `==` on `str`s
53+
4054
let trivial_starts_with = Regex::new("^foobar");
4155
//~^ERROR: trivial regex
4256
//~|HELP consider using `str::starts_with`
@@ -73,6 +87,9 @@ fn trivial_regex() {
7387
let binary_trivial_empty = BRegex::new("^$");
7488
//~^ERROR: trivial regex
7589
//~|HELP consider using `str::is_empty`
90+
let binary_trivial_empty2 = BRegex::with_size_limit(42, "^$");
91+
//~^ERROR: trivial regex
92+
//~|HELP consider using `str::is_empty`
7693

7794
// non-trivial regexes
7895
let non_trivial_dot = Regex::new("a.b");
@@ -81,6 +98,7 @@ fn trivial_regex() {
8198
let non_trivial_ends_with = Regex::new("^foo|bar");
8299
let non_trivial_ends_with = Regex::new("foo|bar");
83100
let non_trivial_binary = BRegex::new("foo|bar");
101+
let non_trivial_binary2 = BRegex::with_size_limit(42, "foo|bar");
84102
}
85103

86104
fn main() {

0 commit comments

Comments
 (0)