Skip to content

Commit 8d9e427

Browse files
committed
Auto merge of #10839 - lochetti:fix_10825, r=llogiq
Fixing `invalid_regex` with invalid UTF8. Also, adding more test cases Fixing false positive and false negative when dealing with regex that could match invalid UTF8. This PR fixes #10825 changelog: [`invalid_regex`]: Fixing false positive and false negative when dealing with regex that could match invalid UTF8
2 parents dc17e73 + ffc2bc8 commit 8d9e427

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

clippy_lints/src/regex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ fn check_set<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
177177
}
178178

179179
fn check_regex<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, utf8: bool) {
180-
let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(!utf8).build();
180+
let mut parser = regex_syntax::ParserBuilder::new().unicode(true).utf8(utf8).build();
181181

182182
if let ExprKind::Lit(lit) = expr.kind {
183183
if let LitKind::Str(ref r, style) = lit.node {

tests/ui/regex.rs

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ fn syntax_error() {
4242
let escaped_string_span = Regex::new("\\b\\c");
4343

4444
let aux_span = Regex::new("(?ixi)");
45+
46+
let should_not_lint = Regex::new("(?u).");
47+
let should_not_lint = BRegex::new("(?u).");
48+
let invalid_utf8_should_not_lint = BRegex::new("(?-u).");
49+
let invalid_utf8_should_lint = Regex::new("(?-u).");
4550
}
4651

4752
fn trivial_regex() {
@@ -71,6 +76,8 @@ fn trivial_regex() {
7176
// non-trivial regexes
7277
let non_trivial_dot = Regex::new("a.b");
7378
let non_trivial_dot_builder = RegexBuilder::new("a.b");
79+
let non_trivial_dot = Regex::new(".");
80+
let non_trivial_dot = BRegex::new(".");
7481
let non_trivial_eq = Regex::new("^foo|bar$");
7582
let non_trivial_starts_with = Regex::new("^foo|bar");
7683
let non_trivial_ends_with = Regex::new("^foo|bar");

tests/ui/regex.stderr

+18-12
Original file line numberDiff line numberDiff line change
@@ -99,93 +99,99 @@ error: regex syntax error: duplicate flag
9999
LL | let aux_span = Regex::new("(?ixi)");
100100
| ^ ^
101101

102+
error: regex syntax error: pattern can match invalid UTF-8
103+
--> $DIR/regex.rs:49:53
104+
|
105+
LL | let invalid_utf8_should_lint = Regex::new("(?-u).");
106+
| ^
107+
102108
error: trivial regex
103-
--> $DIR/regex.rs:48:33
109+
--> $DIR/regex.rs:53:33
104110
|
105111
LL | let trivial_eq = Regex::new("^foobar$");
106112
| ^^^^^^^^^^
107113
|
108114
= help: consider using `==` on `str`s
109115

110116
error: trivial regex
111-
--> $DIR/regex.rs:50:48
117+
--> $DIR/regex.rs:55:48
112118
|
113119
LL | let trivial_eq_builder = RegexBuilder::new("^foobar$");
114120
| ^^^^^^^^^^
115121
|
116122
= help: consider using `==` on `str`s
117123

118124
error: trivial regex
119-
--> $DIR/regex.rs:52:42
125+
--> $DIR/regex.rs:57:42
120126
|
121127
LL | let trivial_starts_with = Regex::new("^foobar");
122128
| ^^^^^^^^^
123129
|
124130
= help: consider using `str::starts_with`
125131

126132
error: trivial regex
127-
--> $DIR/regex.rs:54:40
133+
--> $DIR/regex.rs:59:40
128134
|
129135
LL | let trivial_ends_with = Regex::new("foobar$");
130136
| ^^^^^^^^^
131137
|
132138
= help: consider using `str::ends_with`
133139

134140
error: trivial regex
135-
--> $DIR/regex.rs:56:39
141+
--> $DIR/regex.rs:61:39
136142
|
137143
LL | let trivial_contains = Regex::new("foobar");
138144
| ^^^^^^^^
139145
|
140146
= help: consider using `str::contains`
141147

142148
error: trivial regex
143-
--> $DIR/regex.rs:58:39
149+
--> $DIR/regex.rs:63:39
144150
|
145151
LL | let trivial_contains = Regex::new(NOT_A_REAL_REGEX);
146152
| ^^^^^^^^^^^^^^^^
147153
|
148154
= help: consider using `str::contains`
149155

150156
error: trivial regex
151-
--> $DIR/regex.rs:60:40
157+
--> $DIR/regex.rs:65:40
152158
|
153159
LL | let trivial_backslash = Regex::new("a/.b");
154160
| ^^^^^^^
155161
|
156162
= help: consider using `str::contains`
157163

158164
error: trivial regex
159-
--> $DIR/regex.rs:63:36
165+
--> $DIR/regex.rs:68:36
160166
|
161167
LL | let trivial_empty = Regex::new("");
162168
| ^^
163169
|
164170
= help: the regex is unlikely to be useful as it is
165171

166172
error: trivial regex
167-
--> $DIR/regex.rs:65:36
173+
--> $DIR/regex.rs:70:36
168174
|
169175
LL | let trivial_empty = Regex::new("^");
170176
| ^^^
171177
|
172178
= help: the regex is unlikely to be useful as it is
173179

174180
error: trivial regex
175-
--> $DIR/regex.rs:67:36
181+
--> $DIR/regex.rs:72:36
176182
|
177183
LL | let trivial_empty = Regex::new("^$");
178184
| ^^^^
179185
|
180186
= help: consider using `str::is_empty`
181187

182188
error: trivial regex
183-
--> $DIR/regex.rs:69:44
189+
--> $DIR/regex.rs:74:44
184190
|
185191
LL | let binary_trivial_empty = BRegex::new("^$");
186192
| ^^^^
187193
|
188194
= help: consider using `str::is_empty`
189195

190-
error: aborting due to 23 previous errors
196+
error: aborting due to 24 previous errors
191197

0 commit comments

Comments
 (0)