Skip to content

Commit d247d9c

Browse files
authored
Merge pull request #2645 from TimNN/regex-bytes-utf8
Allow invalid UTF-8 in bytes Regexes
2 parents a1486af + fad826f commit d247d9c

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

clippy_lints/src/regex.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ fn check_set<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: bool)
187187
}
188188

189189
fn check_regex<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr, utf8: bool) {
190-
let mut parser = regex_syntax::ParserBuilder::new().unicode(utf8).build();
190+
let mut parser = regex_syntax::ParserBuilder::new()
191+
.unicode(utf8)
192+
.allow_invalid_utf8(!utf8)
193+
.build();
191194

192195
if let ExprLit(ref lit) = expr.node {
193196
if let LitKind::Str(ref r, style) = lit.node {

tests/ui/regex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ fn syntax_error() {
3434
let bset = BRegexSet::new(&[
3535
r"[a-z]+@[a-z]+\.(com|org|net)",
3636
r"[a-z]+\.(com|org|net)",
37+
r".", // regression test
3738
]);
3839

3940
let set_error = RegexSet::new(&[

tests/ui/regex.stderr

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -56,113 +56,113 @@ error: regex syntax error on position 0: unclosed group
5656
| ^^^^^^^^^^^^^
5757

5858
error: regex syntax error on position 0: unclosed group
59-
--> $DIR/regex.rs:40:9
59+
--> $DIR/regex.rs:41:9
6060
|
61-
40 | OPENING_PAREN,
61+
41 | OPENING_PAREN,
6262
| ^^^^^^^^^^^^^
6363

6464
error: regex syntax error on position 0: unclosed group
65-
--> $DIR/regex.rs:44:9
65+
--> $DIR/regex.rs:45:9
6666
|
67-
44 | OPENING_PAREN,
67+
45 | OPENING_PAREN,
6868
| ^^^^^^^^^^^^^
6969

7070
error: regex syntax error: unrecognized escape sequence
71-
--> $DIR/regex.rs:48:45
71+
--> $DIR/regex.rs:49:45
7272
|
73-
48 | let raw_string_error = Regex::new(r"[...//...]");
73+
49 | let raw_string_error = Regex::new(r"[...//...]");
7474
| ^^
7575

7676
error: regex syntax error: unrecognized escape sequence
77-
--> $DIR/regex.rs:49:46
77+
--> $DIR/regex.rs:50:46
7878
|
79-
49 | let raw_string_error = Regex::new(r#"[...//...]"#);
79+
50 | let raw_string_error = Regex::new(r#"[...//...]"#);
8080
| ^^
8181

8282
error: trivial regex
83-
--> $DIR/regex.rs:53:33
83+
--> $DIR/regex.rs:54:33
8484
|
85-
53 | let trivial_eq = Regex::new("^foobar$");
85+
54 | let trivial_eq = Regex::new("^foobar$");
8686
| ^^^^^^^^^^
8787
|
8888
= help: consider using `==` on `str`s
8989

9090
error: trivial regex
91-
--> $DIR/regex.rs:55:48
91+
--> $DIR/regex.rs:56:48
9292
|
93-
55 | let trivial_eq_builder = RegexBuilder::new("^foobar$");
93+
56 | let trivial_eq_builder = RegexBuilder::new("^foobar$");
9494
| ^^^^^^^^^^
9595
|
9696
= help: consider using `==` on `str`s
9797

9898
error: trivial regex
99-
--> $DIR/regex.rs:57:42
99+
--> $DIR/regex.rs:58:42
100100
|
101-
57 | let trivial_starts_with = Regex::new("^foobar");
101+
58 | let trivial_starts_with = Regex::new("^foobar");
102102
| ^^^^^^^^^
103103
|
104104
= help: consider using `str::starts_with`
105105

106106
error: trivial regex
107-
--> $DIR/regex.rs:59:40
107+
--> $DIR/regex.rs:60:40
108108
|
109-
59 | let trivial_ends_with = Regex::new("foobar$");
109+
60 | let trivial_ends_with = Regex::new("foobar$");
110110
| ^^^^^^^^^
111111
|
112112
= help: consider using `str::ends_with`
113113

114114
error: trivial regex
115-
--> $DIR/regex.rs:61:39
115+
--> $DIR/regex.rs:62:39
116116
|
117-
61 | let trivial_contains = Regex::new("foobar");
117+
62 | let trivial_contains = Regex::new("foobar");
118118
| ^^^^^^^^
119119
|
120120
= help: consider using `str::contains`
121121

122122
error: trivial regex
123-
--> $DIR/regex.rs:63:39
123+
--> $DIR/regex.rs:64:39
124124
|
125-
63 | let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
125+
64 | let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
126126
| ^^^^^^^^^^^^^^^^
127127
|
128128
= help: consider using `str::contains`
129129

130130
error: trivial regex
131-
--> $DIR/regex.rs:65:40
131+
--> $DIR/regex.rs:66:40
132132
|
133-
65 | let trivial_backslash = Regex::new("a/.b");
133+
66 | let trivial_backslash = Regex::new("a/.b");
134134
| ^^^^^^^
135135
|
136136
= help: consider using `str::contains`
137137

138138
error: trivial regex
139-
--> $DIR/regex.rs:68:36
139+
--> $DIR/regex.rs:69:36
140140
|
141-
68 | let trivial_empty = Regex::new("");
141+
69 | let trivial_empty = Regex::new("");
142142
| ^^
143143
|
144144
= help: the regex is unlikely to be useful as it is
145145

146146
error: trivial regex
147-
--> $DIR/regex.rs:70:36
147+
--> $DIR/regex.rs:71:36
148148
|
149-
70 | let trivial_empty = Regex::new("^");
149+
71 | let trivial_empty = Regex::new("^");
150150
| ^^^
151151
|
152152
= help: the regex is unlikely to be useful as it is
153153

154154
error: trivial regex
155-
--> $DIR/regex.rs:72:36
155+
--> $DIR/regex.rs:73:36
156156
|
157-
72 | let trivial_empty = Regex::new("^$");
157+
73 | let trivial_empty = Regex::new("^$");
158158
| ^^^^
159159
|
160160
= help: consider using `str::is_empty`
161161

162162
error: trivial regex
163-
--> $DIR/regex.rs:74:44
163+
--> $DIR/regex.rs:75:44
164164
|
165-
74 | let binary_trivial_empty = BRegex::new("^$");
165+
75 | let binary_trivial_empty = BRegex::new("^$");
166166
| ^^^^
167167
|
168168
= help: consider using `str::is_empty`

0 commit comments

Comments
 (0)