@@ -99,60 +99,19 @@ impl LateLintPass for RegexPass {
99
99
args. len( ) >= 1 ,
100
100
let Some ( def) = cx. tcx. def_map. borrow( ) . get( & fun. id)
101
101
] , {
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
- } ;
107
-
108
102
let def_id = def. def_id( ) ;
109
- let utf8 = if match_def_path( cx, def_id, utf8_path) {
110
- true
111
- } else if match_def_path( cx, def_id, bytes_path) {
112
- false
113
- } else {
114
- return ;
115
- } ;
116
-
117
- let builder = regex_syntax:: ExprBuilder :: new( ) . unicode( utf8) ;
118
-
119
- if let ExprLit ( ref lit) = args[ re_idx] . node {
120
- if let LitKind :: Str ( ref r, _) = lit. node {
121
- match builder. parse( r) {
122
- Ok ( r) => {
123
- if let Some ( repl) = is_trivial_regex( & r) {
124
- span_help_and_lint( cx, TRIVIAL_REGEX , args[ re_idx] . span,
125
- "trivial regex" ,
126
- & format!( "consider using {}" , repl) ) ;
127
- }
128
- }
129
- Err ( e) => {
130
- span_lint( cx,
131
- INVALID_REGEX ,
132
- str_span( args[ re_idx] . span, & r, e. position( ) ) ,
133
- & format!( "regex syntax error: {}" ,
134
- e. description( ) ) ) ;
135
- }
136
- }
137
- }
138
- } else if let Some ( r) = const_str( cx, & * args[ re_idx] ) {
139
- match builder. parse( & r) {
140
- Ok ( r) => {
141
- if let Some ( repl) = is_trivial_regex( & r) {
142
- span_help_and_lint( cx, TRIVIAL_REGEX , args[ re_idx] . span,
143
- "trivial regex" ,
144
- & format!( "consider using {}" , repl) ) ;
145
- }
146
- }
147
- Err ( e) => {
148
- span_lint( cx,
149
- INVALID_REGEX ,
150
- args[ re_idx] . span,
151
- & format!( "regex syntax error on position {}: {}" ,
152
- e. position( ) ,
153
- e. description( ) ) ) ;
154
- }
155
- }
103
+ if args. len( ) == 1 && match_def_path( cx, def_id, & paths:: REGEX_NEW ) {
104
+ check_regex( cx, & args[ 0 ] , true ) ;
105
+ } else if args. len( ) == 1 && match_def_path( cx, def_id, & paths:: REGEX_BYTES_NEW ) {
106
+ check_regex( cx, & args[ 0 ] , false ) ;
107
+ } else if args. len( ) == 2 && match_def_path( cx, def_id, & paths:: REGEX_WSL ) {
108
+ check_regex( cx, & args[ 1 ] , true ) ;
109
+ } else if args. len( ) == 2 && match_def_path( cx, def_id, & paths:: REGEX_BYTES_WSL ) {
110
+ check_regex( cx, & args[ 1 ] , false ) ;
111
+ } else if args. len( ) == 1 && match_def_path( cx, def_id, & paths:: REGEX_SET_NEW ) {
112
+ check_set( cx, & args[ 0 ] , true ) ;
113
+ } else if args. len( ) == 1 && match_def_path( cx, def_id, & paths:: REGEX_BYTES_SET_NEW ) {
114
+ check_set( cx, & args[ 0 ] , false ) ;
156
115
}
157
116
} }
158
117
}
@@ -210,3 +169,57 @@ fn is_trivial_regex(s: ®ex_syntax::Expr) -> Option<&'static str> {
210
169
_ => None ,
211
170
}
212
171
}
172
+
173
+ fn check_set ( cx : & LateContext , expr : & Expr , utf8 : bool ) {
174
+ if_let_chain ! { [
175
+ let ExprAddrOf ( _, ref expr) = expr. node,
176
+ let ExprVec ( ref exprs) = expr. node,
177
+ ] , {
178
+ for expr in exprs {
179
+ check_regex( cx, expr, utf8) ;
180
+ }
181
+ } }
182
+ }
183
+
184
+ fn check_regex ( cx : & LateContext , expr : & Expr , utf8 : bool ) {
185
+ let builder = regex_syntax:: ExprBuilder :: new ( ) . unicode ( utf8) ;
186
+
187
+ if let ExprLit ( ref lit) = expr. node {
188
+ if let LitKind :: Str ( ref r, _) = lit. node {
189
+ match builder. parse ( r) {
190
+ Ok ( r) => {
191
+ if let Some ( repl) = is_trivial_regex ( & r) {
192
+ span_help_and_lint ( cx, TRIVIAL_REGEX , expr. span ,
193
+ "trivial regex" ,
194
+ & format ! ( "consider using {}" , repl) ) ;
195
+ }
196
+ }
197
+ Err ( e) => {
198
+ span_lint ( cx,
199
+ INVALID_REGEX ,
200
+ str_span ( expr. span , r, e. position ( ) ) ,
201
+ & format ! ( "regex syntax error: {}" ,
202
+ e. description( ) ) ) ;
203
+ }
204
+ }
205
+ }
206
+ } else if let Some ( r) = const_str ( cx, expr) {
207
+ match builder. parse ( & r) {
208
+ Ok ( r) => {
209
+ if let Some ( repl) = is_trivial_regex ( & r) {
210
+ span_help_and_lint ( cx, TRIVIAL_REGEX , expr. span ,
211
+ "trivial regex" ,
212
+ & format ! ( "consider using {}" , repl) ) ;
213
+ }
214
+ }
215
+ Err ( e) => {
216
+ span_lint ( cx,
217
+ INVALID_REGEX ,
218
+ expr. span ,
219
+ & format ! ( "regex syntax error on position {}: {}" ,
220
+ e. position( ) ,
221
+ e. description( ) ) ) ;
222
+ }
223
+ }
224
+ }
225
+ }
0 commit comments