@@ -148,62 +148,62 @@ fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expecte
148
148
fn ssr_function_to_method ( ) {
149
149
assert_ssr_transform (
150
150
"my_function($a, $b) ==>> ($a).my_method($b)" ,
151
- "loop { my_function( other_func(x, y), z + w) }" ,
152
- "loop { (other_func(x, y)).my_method(z + w) }" ,
151
+ "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }" ,
152
+ "fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }" ,
153
153
)
154
154
}
155
155
156
156
#[ test]
157
157
fn ssr_nested_function ( ) {
158
158
assert_ssr_transform (
159
159
"foo($a, $b, $c) ==>> bar($c, baz($a, $b))" ,
160
- "fn main { foo (x + value.method(b), x+y-z, true && false) }" ,
161
- "fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }" ,
160
+ "fn foo() {} fn main { foo (x + value.method(b), x+y-z, true && false) }" ,
161
+ "fn foo() {} fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }" ,
162
162
)
163
163
}
164
164
165
165
#[ test]
166
166
fn ssr_expected_spacing ( ) {
167
167
assert_ssr_transform (
168
168
"foo($x) + bar() ==>> bar($x)" ,
169
- "fn main() { foo(5) + bar() }" ,
170
- "fn main() { bar(5) }" ,
169
+ "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }" ,
170
+ "fn foo() {} fn bar() {} fn main() { bar(5) }" ,
171
171
) ;
172
172
}
173
173
174
174
#[ test]
175
175
fn ssr_with_extra_space ( ) {
176
176
assert_ssr_transform (
177
177
"foo($x ) + bar() ==>> bar($x)" ,
178
- "fn main() { foo( 5 ) +bar( ) }" ,
179
- "fn main() { bar(5) }" ,
178
+ "fn foo() {} fn bar() {} fn main() { foo( 5 ) +bar( ) }" ,
179
+ "fn foo() {} fn bar() {} fn main() { bar(5) }" ,
180
180
) ;
181
181
}
182
182
183
183
#[ test]
184
184
fn ssr_keeps_nested_comment ( ) {
185
185
assert_ssr_transform (
186
186
"foo($x) ==>> bar($x)" ,
187
- "fn main() { foo(other(5 /* using 5 */)) }" ,
188
- "fn main() { bar(other(5 /* using 5 */)) }" ,
187
+ "fn foo() {} fn main() { foo(other(5 /* using 5 */)) }" ,
188
+ "fn foo() {} fn main() { bar(other(5 /* using 5 */)) }" ,
189
189
)
190
190
}
191
191
192
192
#[ test]
193
193
fn ssr_keeps_comment ( ) {
194
194
assert_ssr_transform (
195
195
"foo($x) ==>> bar($x)" ,
196
- "fn main() { foo(5 /* using 5 */) }" ,
197
- "fn main() { bar(5)/* using 5 */ }" ,
196
+ "fn foo() {} fn main() { foo(5 /* using 5 */) }" ,
197
+ "fn foo() {} fn main() { bar(5)/* using 5 */ }" ,
198
198
)
199
199
}
200
200
201
201
#[ test]
202
202
fn ssr_struct_lit ( ) {
203
203
assert_ssr_transform (
204
204
"foo{a: $a, b: $b} ==>> foo::new($a, $b)" ,
205
- "fn main() { foo{b:2, a:1} }" ,
206
- "fn main() { foo::new(1, 2) }" ,
205
+ "fn foo() {} fn main() { foo{b:2, a:1} }" ,
206
+ "fn foo() {} fn main() { foo::new(1, 2) }" ,
207
207
)
208
208
}
209
209
@@ -225,16 +225,18 @@ fn match_fn_definition() {
225
225
226
226
#[ test]
227
227
fn match_struct_definition ( ) {
228
- assert_matches (
229
- " struct $n {$f: Option<String>}" ,
230
- " struct Bar {} struct Foo {name: Option<String>}" ,
231
- & [ " struct Foo {name: Option<String>}"] ,
232
- ) ;
228
+ let code = r#"
229
+ struct Option<T> {}
230
+ struct Bar {}
231
+ struct Foo {name: Option<String>}"# ;
232
+ assert_matches ( "struct $n {$f: Option<String>}" , code , & [ "struct Foo {name: Option<String>}" ] ) ;
233
233
}
234
234
235
235
#[ test]
236
236
fn match_expr ( ) {
237
- let code = "fn f() -> i32 {foo(40 + 2, 42)}" ;
237
+ let code = r#"
238
+ fn foo() {}
239
+ fn f() -> i32 {foo(40 + 2, 42)}"# ;
238
240
assert_matches ( "foo($a, $b)" , code, & [ "foo(40 + 2, 42)" ] ) ;
239
241
assert_no_match ( "foo($a, $b, $c)" , code) ;
240
242
assert_no_match ( "foo($a)" , code) ;
@@ -263,7 +265,9 @@ fn match_nested_method_calls_with_macro_call() {
263
265
264
266
#[ test]
265
267
fn match_complex_expr ( ) {
266
- let code = "fn f() -> i32 {foo(bar(40, 2), 42)}" ;
268
+ let code = r#"
269
+ fn foo() {} fn bar() {}
270
+ fn f() -> i32 {foo(bar(40, 2), 42)}"# ;
267
271
assert_matches ( "foo($a, $b)" , code, & [ "foo(bar(40, 2), 42)" ] ) ;
268
272
assert_no_match ( "foo($a, $b, $c)" , code) ;
269
273
assert_no_match ( "foo($a)" , code) ;
@@ -274,53 +278,62 @@ fn match_complex_expr() {
274
278
#[ test]
275
279
fn match_with_trailing_commas ( ) {
276
280
// Code has comma, pattern doesn't.
277
- assert_matches ( "foo($a, $b)" , "fn f() {foo(1, 2,);}" , & [ "foo(1, 2,)" ] ) ;
278
- assert_matches ( "Foo{$a, $b}" , "fn f() {Foo{1, 2,};}" , & [ "Foo{1, 2,}" ] ) ;
281
+ assert_matches ( "foo($a, $b)" , "fn foo() {} fn f() {foo(1, 2,);}" , & [ "foo(1, 2,)" ] ) ;
282
+ assert_matches ( "Foo{$a, $b}" , "struct Foo {} fn f() {Foo{1, 2,};}" , & [ "Foo{1, 2,}" ] ) ;
279
283
280
284
// Pattern has comma, code doesn't.
281
- assert_matches ( "foo($a, $b,)" , "fn f() {foo(1, 2);}" , & [ "foo(1, 2)" ] ) ;
282
- assert_matches ( "Foo{$a, $b,}" , "fn f() {Foo{1, 2};}" , & [ "Foo{1, 2}" ] ) ;
285
+ assert_matches ( "foo($a, $b,)" , "fn foo() {} fn f() {foo(1, 2);}" , & [ "foo(1, 2)" ] ) ;
286
+ assert_matches ( "Foo{$a, $b,}" , "struct Foo {} fn f() {Foo{1, 2};}" , & [ "Foo{1, 2}" ] ) ;
283
287
}
284
288
285
289
#[ test]
286
290
fn match_type ( ) {
287
291
assert_matches ( "i32" , "fn f() -> i32 {1 + 2}" , & [ "i32" ] ) ;
288
- assert_matches ( "Option<$a>" , "fn f() -> Option<i32> {42}" , & [ "Option<i32>" ] ) ;
289
- assert_no_match ( "Option<$a>" , "fn f() -> Result<i32, ()> {42}" ) ;
292
+ assert_matches (
293
+ "Option<$a>" ,
294
+ "struct Option<T> {} fn f() -> Option<i32> {42}" ,
295
+ & [ "Option<i32>" ] ,
296
+ ) ;
297
+ assert_no_match (
298
+ "Option<$a>" ,
299
+ "struct Option<T> {} struct Result<T, E> {} fn f() -> Result<i32, ()> {42}" ,
300
+ ) ;
290
301
}
291
302
292
303
#[ test]
293
304
fn match_struct_instantiation ( ) {
294
- assert_matches (
295
- "Foo {bar: 1, baz: 2}" ,
296
- "fn f() {Foo {bar: 1, baz: 2}}" ,
297
- & [ "Foo {bar: 1, baz: 2}" ] ,
298
- ) ;
305
+ let code = r#"
306
+ struct Foo {bar: i32, baz: i32}
307
+ fn f() {Foo {bar: 1, baz: 2}}"# ;
308
+ assert_matches ( "Foo {bar: 1, baz: 2}" , code, & [ "Foo {bar: 1, baz: 2}" ] ) ;
299
309
// Now with placeholders for all parts of the struct.
300
- assert_matches (
301
- "Foo {$a: $b, $c: $d}" ,
302
- "fn f() {Foo {bar: 1, baz: 2}}" ,
303
- & [ "Foo {bar: 1, baz: 2}" ] ,
304
- ) ;
305
- assert_matches ( "Foo {}" , "fn f() {Foo {}}" , & [ "Foo {}" ] ) ;
310
+ assert_matches ( "Foo {$a: $b, $c: $d}" , code, & [ "Foo {bar: 1, baz: 2}" ] ) ;
311
+ assert_matches ( "Foo {}" , "struct Foo {} fn f() {Foo {}}" , & [ "Foo {}" ] ) ;
306
312
}
307
313
308
314
#[ test]
309
315
fn match_path ( ) {
310
- assert_matches ( "foo::bar" , "fn f() {foo::bar(42)}" , & [ "foo::bar" ] ) ;
311
- assert_matches ( "$a::bar" , "fn f() {foo::bar(42)}" , & [ "foo::bar" ] ) ;
312
- assert_matches ( "foo::$b" , "fn f() {foo::bar(42)}" , & [ "foo::bar" ] ) ;
316
+ let code = r#"
317
+ mod foo {
318
+ fn bar() {}
319
+ }
320
+ fn f() {foo::bar(42)}"# ;
321
+ assert_matches ( "foo::bar" , code, & [ "foo::bar" ] ) ;
322
+ assert_matches ( "$a::bar" , code, & [ "foo::bar" ] ) ;
323
+ assert_matches ( "foo::$b" , code, & [ "foo::bar" ] ) ;
313
324
}
314
325
315
326
#[ test]
316
327
fn match_pattern ( ) {
317
- assert_matches ( "Some($a)" , "fn f() {if let Some(x) = foo() {}}" , & [ "Some(x)" ] ) ;
328
+ assert_matches ( "Some($a)" , "struct Some(); fn f() {if let Some(x) = foo() {}}" , & [ "Some(x)" ] ) ;
318
329
}
319
330
320
331
#[ test]
321
332
fn literal_constraint ( ) {
322
333
mark:: check!( literal_constraint) ;
323
334
let code = r#"
335
+ enum Option<T> { Some(T), None }
336
+ use Option::Some;
324
337
fn f1() {
325
338
let x1 = Some(42);
326
339
let x2 = Some("foo");
@@ -337,24 +350,36 @@ fn literal_constraint() {
337
350
fn match_reordered_struct_instantiation ( ) {
338
351
assert_matches (
339
352
"Foo {aa: 1, b: 2, ccc: 3}" ,
340
- "fn f() {Foo {b: 2, ccc: 3, aa: 1}}" ,
353
+ "struct Foo {} fn f() {Foo {b: 2, ccc: 3, aa: 1}}" ,
341
354
& [ "Foo {b: 2, ccc: 3, aa: 1}" ] ,
342
355
) ;
343
- assert_no_match ( "Foo {a: 1}" , "fn f() {Foo {b: 1}}" ) ;
344
- assert_no_match ( "Foo {a: 1}" , "fn f() {Foo {a: 2}}" ) ;
345
- assert_no_match ( "Foo {a: 1, b: 2}" , "fn f() {Foo {a: 1}}" ) ;
346
- assert_no_match ( "Foo {a: 1, b: 2}" , "fn f() {Foo {b: 2}}" ) ;
347
- assert_no_match ( "Foo {a: 1, }" , "fn f() {Foo {a: 1, b: 2}}" ) ;
348
- assert_no_match ( "Foo {a: 1, z: 9}" , "fn f() {Foo {a: 1}}" ) ;
356
+ assert_no_match ( "Foo {a: 1}" , "struct Foo {} fn f() {Foo {b: 1}}" ) ;
357
+ assert_no_match ( "Foo {a: 1}" , "struct Foo {} fn f() {Foo {a: 2}}" ) ;
358
+ assert_no_match ( "Foo {a: 1, b: 2}" , "struct Foo {} fn f() {Foo {a: 1}}" ) ;
359
+ assert_no_match ( "Foo {a: 1, b: 2}" , "struct Foo {} fn f() {Foo {b: 2}}" ) ;
360
+ assert_no_match ( "Foo {a: 1, }" , "struct Foo {} fn f() {Foo {a: 1, b: 2}}" ) ;
361
+ assert_no_match ( "Foo {a: 1, z: 9}" , "struct Foo {} fn f() {Foo {a: 1}}" ) ;
349
362
}
350
363
351
364
#[ test]
352
365
fn match_macro_invocation ( ) {
353
- assert_matches ( "foo!($a)" , "fn() {foo(foo!(foo()))}" , & [ "foo!(foo())" ] ) ;
354
- assert_matches ( "foo!(41, $a, 43)" , "fn() {foo!(41, 42, 43)}" , & [ "foo!(41, 42, 43)" ] ) ;
355
- assert_no_match ( "foo!(50, $a, 43)" , "fn() {foo!(41, 42, 43}" ) ;
356
- assert_no_match ( "foo!(41, $a, 50)" , "fn() {foo!(41, 42, 43}" ) ;
357
- assert_matches ( "foo!($a())" , "fn() {foo!(bar())}" , & [ "foo!(bar())" ] ) ;
366
+ assert_matches (
367
+ "foo!($a)" ,
368
+ "macro_rules! foo {() => {}} fn() {foo(foo!(foo()))}" ,
369
+ & [ "foo!(foo())" ] ,
370
+ ) ;
371
+ assert_matches (
372
+ "foo!(41, $a, 43)" ,
373
+ "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43)}" ,
374
+ & [ "foo!(41, 42, 43)" ] ,
375
+ ) ;
376
+ assert_no_match ( "foo!(50, $a, 43)" , "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}" ) ;
377
+ assert_no_match ( "foo!(41, $a, 50)" , "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}" ) ;
378
+ assert_matches (
379
+ "foo!($a())" ,
380
+ "macro_rules! foo {() => {}} fn() {foo!(bar())}" ,
381
+ & [ "foo!(bar())" ] ,
382
+ ) ;
358
383
}
359
384
360
385
// When matching within a macro expansion, we only allow matches of nodes that originated from
@@ -389,56 +414,60 @@ fn no_match_split_expression() {
389
414
390
415
#[ test]
391
416
fn replace_function_call ( ) {
392
- assert_ssr_transform ( "foo() ==>> bar()" , "fn f1() {foo(); foo();}" , "fn f1() {bar(); bar();}" ) ;
417
+ assert_ssr_transform (
418
+ "foo() ==>> bar()" ,
419
+ "fn foo() {} fn f1() {foo(); foo();}" ,
420
+ "fn foo() {} fn f1() {bar(); bar();}" ,
421
+ ) ;
393
422
}
394
423
395
424
#[ test]
396
425
fn replace_function_call_with_placeholders ( ) {
397
426
assert_ssr_transform (
398
427
"foo($a, $b) ==>> bar($b, $a)" ,
399
- "fn f1() {foo(5, 42)}" ,
400
- "fn f1() {bar(42, 5)}" ,
428
+ "fn foo() {} fn f1() {foo(5, 42)}" ,
429
+ "fn foo() {} fn f1() {bar(42, 5)}" ,
401
430
) ;
402
431
}
403
432
404
433
#[ test]
405
434
fn replace_nested_function_calls ( ) {
406
435
assert_ssr_transform (
407
436
"foo($a) ==>> bar($a)" ,
408
- "fn f1() {foo(foo(42))}" ,
409
- "fn f1() {bar(bar(42))}" ,
437
+ "fn foo() {} fn f1() {foo(foo(42))}" ,
438
+ "fn foo() {} fn f1() {bar(bar(42))}" ,
410
439
) ;
411
440
}
412
441
413
442
#[ test]
414
443
fn replace_type ( ) {
415
444
assert_ssr_transform (
416
445
"Result<(), $a> ==>> Option<$a>" ,
417
- "fn f1() -> Result<(), Vec<Error>> {foo()}" ,
418
- "fn f1() -> Option<Vec<Error>> {foo()}" ,
446
+ "struct Result<T, E> {} fn f1() -> Result<(), Vec<Error>> {foo()}" ,
447
+ "struct Result<T, E> {} fn f1() -> Option<Vec<Error>> {foo()}" ,
419
448
) ;
420
449
}
421
450
422
451
#[ test]
423
452
fn replace_struct_init ( ) {
424
453
assert_ssr_transform (
425
454
"Foo {a: $a, b: $b} ==>> Foo::new($a, $b)" ,
426
- "fn f1() {Foo{b: 1, a: 2}}" ,
427
- "fn f1() {Foo::new(2, 1)}" ,
455
+ "struct Foo {} fn f1() {Foo{b: 1, a: 2}}" ,
456
+ "struct Foo {} fn f1() {Foo::new(2, 1)}" ,
428
457
) ;
429
458
}
430
459
431
460
#[ test]
432
461
fn replace_macro_invocations ( ) {
433
462
assert_ssr_transform (
434
463
"try!($a) ==>> $a?" ,
435
- "fn f1() -> Result<(), E> {bar(try!(foo()));}" ,
436
- "fn f1() -> Result<(), E> {bar(foo()?);}" ,
464
+ "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}" ,
465
+ "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}" ,
437
466
) ;
438
467
assert_ssr_transform (
439
468
"foo!($a($b)) ==>> foo($b, $a)" ,
440
- "fn f1() {foo!(abc(def() + 2));}" ,
441
- "fn f1() {foo(def() + 2, abc);}" ,
469
+ "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}" ,
470
+ "macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}" ,
442
471
) ;
443
472
}
444
473
@@ -527,6 +556,7 @@ fn preserves_whitespace_within_macro_expansion() {
527
556
#[ test]
528
557
fn match_failure_reasons ( ) {
529
558
let code = r#"
559
+ fn bar() {}
530
560
macro_rules! foo {
531
561
($a:expr) => {
532
562
1 + $a + 2
0 commit comments