• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use expect_test::{expect, Expect};
2 use ide_db::{
3     base_db::{salsa::Durability, FileId, FilePosition, FileRange, SourceDatabaseExt},
4     FxHashSet,
5 };
6 use test_utils::RangeOrOffset;
7 use triomphe::Arc;
8 
9 use crate::{MatchFinder, SsrRule};
10 
parse_error_text(query: &str) -> String11 fn parse_error_text(query: &str) -> String {
12     format!("{}", query.parse::<SsrRule>().unwrap_err())
13 }
14 
15 #[test]
parser_empty_query()16 fn parser_empty_query() {
17     assert_eq!(parse_error_text(""), "Parse error: Cannot find delimiter `==>>`");
18 }
19 
20 #[test]
parser_no_delimiter()21 fn parser_no_delimiter() {
22     assert_eq!(parse_error_text("foo()"), "Parse error: Cannot find delimiter `==>>`");
23 }
24 
25 #[test]
parser_two_delimiters()26 fn parser_two_delimiters() {
27     assert_eq!(
28         parse_error_text("foo() ==>> a ==>> b "),
29         "Parse error: More than one delimiter found"
30     );
31 }
32 
33 #[test]
parser_repeated_name()34 fn parser_repeated_name() {
35     assert_eq!(
36         parse_error_text("foo($a, $a) ==>>"),
37         "Parse error: Placeholder `$a` repeats more than once"
38     );
39 }
40 
41 #[test]
parser_invalid_pattern()42 fn parser_invalid_pattern() {
43     assert_eq!(
44         parse_error_text(" ==>> ()"),
45         "Parse error: Not a valid Rust expression, type, item, path or pattern"
46     );
47 }
48 
49 #[test]
parser_invalid_template()50 fn parser_invalid_template() {
51     assert_eq!(
52         parse_error_text("() ==>> )"),
53         "Parse error: Not a valid Rust expression, type, item, path or pattern"
54     );
55 }
56 
57 #[test]
parser_undefined_placeholder_in_replacement()58 fn parser_undefined_placeholder_in_replacement() {
59     assert_eq!(
60         parse_error_text("42 ==>> $a"),
61         "Parse error: Replacement contains undefined placeholders: $a"
62     );
63 }
64 
65 /// `code` may optionally contain a cursor marker `$0`. If it doesn't, then the position will be
66 /// the start of the file. If there's a second cursor marker, then we'll return a single range.
single_file(code: &str) -> (ide_db::RootDatabase, FilePosition, Vec<FileRange>)67 pub(crate) fn single_file(code: &str) -> (ide_db::RootDatabase, FilePosition, Vec<FileRange>) {
68     use ide_db::base_db::fixture::WithFixture;
69     use ide_db::symbol_index::SymbolsDatabase;
70     let (mut db, file_id, range_or_offset) = if code.contains(test_utils::CURSOR_MARKER) {
71         ide_db::RootDatabase::with_range_or_offset(code)
72     } else {
73         let (db, file_id) = ide_db::RootDatabase::with_single_file(code);
74         (db, file_id, RangeOrOffset::Offset(0.into()))
75     };
76     let selections;
77     let position;
78     match range_or_offset {
79         RangeOrOffset::Range(range) => {
80             position = FilePosition { file_id, offset: range.start() };
81             selections = vec![FileRange { file_id, range }];
82         }
83         RangeOrOffset::Offset(offset) => {
84             position = FilePosition { file_id, offset };
85             selections = vec![];
86         }
87     }
88     let mut local_roots = FxHashSet::default();
89     local_roots.insert(ide_db::base_db::fixture::WORKSPACE);
90     db.set_local_roots_with_durability(Arc::new(local_roots), Durability::HIGH);
91     (db, position, selections)
92 }
93 
assert_ssr_transform(rule: &str, input: &str, expected: Expect)94 fn assert_ssr_transform(rule: &str, input: &str, expected: Expect) {
95     assert_ssr_transforms(&[rule], input, expected);
96 }
97 
assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect)98 fn assert_ssr_transforms(rules: &[&str], input: &str, expected: Expect) {
99     let (db, position, selections) = single_file(input);
100     let mut match_finder = MatchFinder::in_context(&db, position, selections).unwrap();
101     for rule in rules {
102         let rule: SsrRule = rule.parse().unwrap();
103         match_finder.add_rule(rule).unwrap();
104     }
105     let edits = match_finder.edits();
106     if edits.is_empty() {
107         panic!("No edits were made");
108     }
109     // Note, db.file_text is not necessarily the same as `input`, since fixture parsing alters
110     // stuff.
111     let mut actual = db.file_text(position.file_id).to_string();
112     edits[&position.file_id].apply(&mut actual);
113     expected.assert_eq(&actual);
114 }
115 
print_match_debug_info(match_finder: &MatchFinder<'_>, file_id: FileId, snippet: &str)116 fn print_match_debug_info(match_finder: &MatchFinder<'_>, file_id: FileId, snippet: &str) {
117     let debug_info = match_finder.debug_where_text_equal(file_id, snippet);
118     println!(
119         "Match debug info: {} nodes had text exactly equal to '{}'",
120         debug_info.len(),
121         snippet
122     );
123     for (index, d) in debug_info.iter().enumerate() {
124         println!("Node #{index}\n{d:#?}\n");
125     }
126 }
127 
assert_matches(pattern: &str, code: &str, expected: &[&str])128 fn assert_matches(pattern: &str, code: &str, expected: &[&str]) {
129     let (db, position, selections) = single_file(code);
130     let mut match_finder = MatchFinder::in_context(&db, position, selections).unwrap();
131     match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
132     let matched_strings: Vec<String> =
133         match_finder.matches().flattened().matches.iter().map(|m| m.matched_text()).collect();
134     if matched_strings != expected && !expected.is_empty() {
135         print_match_debug_info(&match_finder, position.file_id, expected[0]);
136     }
137     assert_eq!(matched_strings, expected);
138 }
139 
assert_no_match(pattern: &str, code: &str)140 fn assert_no_match(pattern: &str, code: &str) {
141     let (db, position, selections) = single_file(code);
142     let mut match_finder = MatchFinder::in_context(&db, position, selections).unwrap();
143     match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
144     let matches = match_finder.matches().flattened().matches;
145     if !matches.is_empty() {
146         print_match_debug_info(&match_finder, position.file_id, &matches[0].matched_text());
147         panic!("Got {} matches when we expected none: {matches:#?}", matches.len());
148     }
149 }
150 
assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str)151 fn assert_match_failure_reason(pattern: &str, code: &str, snippet: &str, expected_reason: &str) {
152     let (db, position, selections) = single_file(code);
153     let mut match_finder = MatchFinder::in_context(&db, position, selections).unwrap();
154     match_finder.add_search_pattern(pattern.parse().unwrap()).unwrap();
155     let mut reasons = Vec::new();
156     for d in match_finder.debug_where_text_equal(position.file_id, snippet) {
157         if let Some(reason) = d.match_failure_reason() {
158             reasons.push(reason.to_owned());
159         }
160     }
161     assert_eq!(reasons, vec![expected_reason]);
162 }
163 
164 #[test]
ssr_let_stmt_in_macro_match()165 fn ssr_let_stmt_in_macro_match() {
166     assert_matches(
167         "let a = 0",
168         r#"
169             macro_rules! m1 { ($a:stmt) => {$a}; }
170             fn f() {m1!{ let a = 0 };}"#,
171         // FIXME: Whitespace is not part of the matched block
172         &["leta=0"],
173     );
174 }
175 
176 #[test]
ssr_let_stmt_in_fn_match()177 fn ssr_let_stmt_in_fn_match() {
178     assert_matches("let $a = 10;", "fn main() { let x = 10; x }", &["let x = 10;"]);
179     assert_matches("let $a = $b;", "fn main() { let x = 10; x }", &["let x = 10;"]);
180 }
181 
182 #[test]
ssr_block_expr_match()183 fn ssr_block_expr_match() {
184     assert_matches("{ let $a = $b; }", "fn main() { let x = 10; }", &["{ let x = 10; }"]);
185     assert_matches("{ let $a = $b; $c }", "fn main() { let x = 10; x }", &["{ let x = 10; x }"]);
186 }
187 
188 #[test]
ssr_let_stmt_replace()189 fn ssr_let_stmt_replace() {
190     // Pattern and template with trailing semicolon
191     assert_ssr_transform(
192         "let $a = $b; ==>> let $a = 11;",
193         "fn main() { let x = 10; x }",
194         expect![["fn main() { let x = 11; x }"]],
195     );
196 }
197 
198 #[test]
ssr_let_stmt_replace_expr()199 fn ssr_let_stmt_replace_expr() {
200     // Trailing semicolon should be dropped from the new expression
201     assert_ssr_transform(
202         "let $a = $b; ==>> $b",
203         "fn main() { let x = 10; }",
204         expect![["fn main() { 10 }"]],
205     );
206 }
207 
208 #[test]
ssr_blockexpr_replace_stmt_with_stmt()209 fn ssr_blockexpr_replace_stmt_with_stmt() {
210     assert_ssr_transform(
211         "if $a() {$b;} ==>> $b;",
212         "{
213     if foo() {
214         bar();
215     }
216     Ok(())
217 }",
218         expect![[r#"{
219     bar();
220     Ok(())
221 }"#]],
222     );
223 }
224 
225 #[test]
ssr_blockexpr_match_trailing_expr()226 fn ssr_blockexpr_match_trailing_expr() {
227     assert_matches(
228         "if $a() {$b;}",
229         "{
230     if foo() {
231         bar();
232     }
233 }",
234         &["if foo() {
235         bar();
236     }"],
237     );
238 }
239 
240 #[test]
ssr_blockexpr_replace_trailing_expr_with_stmt()241 fn ssr_blockexpr_replace_trailing_expr_with_stmt() {
242     assert_ssr_transform(
243         "if $a() {$b;} ==>> $b;",
244         "{
245     if foo() {
246         bar();
247     }
248 }",
249         expect![["{
250     bar();
251 }"]],
252     );
253 }
254 
255 #[test]
ssr_function_to_method()256 fn ssr_function_to_method() {
257     assert_ssr_transform(
258         "my_function($a, $b) ==>> ($a).my_method($b)",
259         "fn my_function() {} fn main() { loop { my_function( other_func(x, y), z + w) } }",
260         expect![["fn my_function() {} fn main() { loop { (other_func(x, y)).my_method(z + w) } }"]],
261     )
262 }
263 
264 #[test]
ssr_nested_function()265 fn ssr_nested_function() {
266     assert_ssr_transform(
267         "foo($a, $b, $c) ==>> bar($c, baz($a, $b))",
268         r#"
269             //- /lib.rs crate:foo
270             fn foo() {}
271             fn bar() {}
272             fn baz() {}
273             fn main { foo  (x + value.method(b), x+y-z, true && false) }
274             "#,
275         expect![[r#"
276             fn foo() {}
277             fn bar() {}
278             fn baz() {}
279             fn main { bar(true && false, baz(x + value.method(b), x+y-z)) }
280         "#]],
281     )
282 }
283 
284 #[test]
ssr_expected_spacing()285 fn ssr_expected_spacing() {
286     assert_ssr_transform(
287         "foo($x) + bar() ==>> bar($x)",
288         "fn foo() {} fn bar() {} fn main() { foo(5) + bar() }",
289         expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]],
290     );
291 }
292 
293 #[test]
ssr_with_extra_space()294 fn ssr_with_extra_space() {
295     assert_ssr_transform(
296         "foo($x  ) +    bar() ==>> bar($x)",
297         "fn foo() {} fn bar() {} fn main() { foo(  5 )  +bar(   ) }",
298         expect![["fn foo() {} fn bar() {} fn main() { bar(5) }"]],
299     );
300 }
301 
302 #[test]
ssr_keeps_nested_comment()303 fn ssr_keeps_nested_comment() {
304     assert_ssr_transform(
305         "foo($x) ==>> bar($x)",
306         "fn foo() {} fn bar() {} fn main() { foo(other(5 /* using 5 */)) }",
307         expect![["fn foo() {} fn bar() {} fn main() { bar(other(5 /* using 5 */)) }"]],
308     )
309 }
310 
311 #[test]
ssr_keeps_comment()312 fn ssr_keeps_comment() {
313     assert_ssr_transform(
314         "foo($x) ==>> bar($x)",
315         "fn foo() {} fn bar() {} fn main() { foo(5 /* using 5 */) }",
316         expect![["fn foo() {} fn bar() {} fn main() { bar(5)/* using 5 */ }"]],
317     )
318 }
319 
320 #[test]
ssr_struct_lit()321 fn ssr_struct_lit() {
322     assert_ssr_transform(
323         "Foo{a: $a, b: $b} ==>> Foo::new($a, $b)",
324         r#"
325             struct Foo() {}
326             impl Foo { fn new() {} }
327             fn main() { Foo{b:2, a:1} }
328             "#,
329         expect![[r#"
330             struct Foo() {}
331             impl Foo { fn new() {} }
332             fn main() { Foo::new(1, 2) }
333         "#]],
334     )
335 }
336 
337 #[test]
ssr_struct_def()338 fn ssr_struct_def() {
339     assert_ssr_transform(
340         "struct Foo { $f: $t } ==>> struct Foo($t);",
341         r#"struct Foo { field: i32 }"#,
342         expect![[r#"struct Foo(i32);"#]],
343     )
344 }
345 
346 #[test]
ignores_whitespace()347 fn ignores_whitespace() {
348     assert_matches("1+2", "fn f() -> i32 {1  +  2}", &["1  +  2"]);
349     assert_matches("1 + 2", "fn f() -> i32 {1+2}", &["1+2"]);
350 }
351 
352 #[test]
no_match()353 fn no_match() {
354     assert_no_match("1 + 3", "fn f() -> i32 {1  +  2}");
355 }
356 
357 #[test]
match_fn_definition()358 fn match_fn_definition() {
359     assert_matches("fn $a($b: $t) {$c}", "fn f(a: i32) {bar()}", &["fn f(a: i32) {bar()}"]);
360 }
361 
362 #[test]
match_struct_definition()363 fn match_struct_definition() {
364     let code = r#"
365         struct Option<T> {}
366         struct Bar {}
367         struct Foo {name: Option<String>}"#;
368     assert_matches("struct $n {$f: Option<String>}", code, &["struct Foo {name: Option<String>}"]);
369 }
370 
371 #[test]
match_expr()372 fn match_expr() {
373     let code = r#"
374         fn foo() {}
375         fn f() -> i32 {foo(40 + 2, 42)}"#;
376     assert_matches("foo($a, $b)", code, &["foo(40 + 2, 42)"]);
377     assert_no_match("foo($a, $b, $c)", code);
378     assert_no_match("foo($a)", code);
379 }
380 
381 #[test]
match_nested_method_calls()382 fn match_nested_method_calls() {
383     assert_matches(
384         "$a.z().z().z()",
385         "fn f() {h().i().j().z().z().z().d().e()}",
386         &["h().i().j().z().z().z()"],
387     );
388 }
389 
390 // Make sure that our node matching semantics don't differ within macro calls.
391 #[test]
match_nested_method_calls_with_macro_call()392 fn match_nested_method_calls_with_macro_call() {
393     assert_matches(
394         "$a.z().z().z()",
395         r#"
396             macro_rules! m1 { ($a:expr) => {$a}; }
397             fn f() {m1!(h().i().j().z().z().z().d().e())}"#,
398         &["h().i().j().z().z().z()"],
399     );
400 }
401 
402 #[test]
match_complex_expr()403 fn match_complex_expr() {
404     let code = r#"
405         fn foo() {} fn bar() {}
406         fn f() -> i32 {foo(bar(40, 2), 42)}"#;
407     assert_matches("foo($a, $b)", code, &["foo(bar(40, 2), 42)"]);
408     assert_no_match("foo($a, $b, $c)", code);
409     assert_no_match("foo($a)", code);
410     assert_matches("bar($a, $b)", code, &["bar(40, 2)"]);
411 }
412 
413 // Trailing commas in the code should be ignored.
414 #[test]
match_with_trailing_commas()415 fn match_with_trailing_commas() {
416     // Code has comma, pattern doesn't.
417     assert_matches("foo($a, $b)", "fn foo() {} fn f() {foo(1, 2,);}", &["foo(1, 2,)"]);
418     assert_matches("Foo{$a, $b}", "struct Foo {} fn f() {Foo{1, 2,};}", &["Foo{1, 2,}"]);
419 
420     // Pattern has comma, code doesn't.
421     assert_matches("foo($a, $b,)", "fn foo() {} fn f() {foo(1, 2);}", &["foo(1, 2)"]);
422     assert_matches("Foo{$a, $b,}", "struct Foo {} fn f() {Foo{1, 2};}", &["Foo{1, 2}"]);
423 }
424 
425 #[test]
match_type()426 fn match_type() {
427     assert_matches("i32", "fn f() -> i32 {1  +  2}", &["i32"]);
428     assert_matches(
429         "Option<$a>",
430         "struct Option<T> {} fn f() -> Option<i32> {42}",
431         &["Option<i32>"],
432     );
433     assert_no_match(
434         "Option<$a>",
435         "struct Option<T> {} struct Result<T, E> {} fn f() -> Result<i32, ()> {42}",
436     );
437 }
438 
439 #[test]
match_struct_instantiation()440 fn match_struct_instantiation() {
441     let code = r#"
442         struct Foo {bar: i32, baz: i32}
443         fn f() {Foo {bar: 1, baz: 2}}"#;
444     assert_matches("Foo {bar: 1, baz: 2}", code, &["Foo {bar: 1, baz: 2}"]);
445     // Now with placeholders for all parts of the struct.
446     assert_matches("Foo {$a: $b, $c: $d}", code, &["Foo {bar: 1, baz: 2}"]);
447     assert_matches("Foo {}", "struct Foo {} fn f() {Foo {}}", &["Foo {}"]);
448 }
449 
450 #[test]
match_path()451 fn match_path() {
452     let code = r#"
453         mod foo {
454             pub fn bar() {}
455         }
456         fn f() {foo::bar(42)}"#;
457     assert_matches("foo::bar", code, &["foo::bar"]);
458     assert_matches("$a::bar", code, &["foo::bar"]);
459     assert_matches("foo::$b", code, &["foo::bar"]);
460 }
461 
462 #[test]
match_pattern()463 fn match_pattern() {
464     assert_matches("Some($a)", "struct Some(); fn f() {if let Some(x) = foo() {}}", &["Some(x)"]);
465 }
466 
467 // If our pattern has a full path, e.g. a::b::c() and the code has c(), but c resolves to
468 // a::b::c, then we should match.
469 #[test]
match_fully_qualified_fn_path()470 fn match_fully_qualified_fn_path() {
471     let code = r#"
472         mod a {
473             pub mod b {
474                 pub fn c(_: i32) {}
475             }
476         }
477         use a::b::c;
478         fn f1() {
479             c(42);
480         }
481         "#;
482     assert_matches("a::b::c($a)", code, &["c(42)"]);
483 }
484 
485 #[test]
match_resolved_type_name()486 fn match_resolved_type_name() {
487     let code = r#"
488         mod m1 {
489             pub mod m2 {
490                 pub trait Foo<T> {}
491             }
492         }
493         mod m3 {
494             trait Foo<T> {}
495             fn f1(f: Option<&dyn Foo<bool>>) {}
496         }
497         mod m4 {
498             use crate::m1::m2::Foo;
499             fn f1(f: Option<&dyn Foo<i32>>) {}
500         }
501         "#;
502     assert_matches("m1::m2::Foo<$t>", code, &["Foo<i32>"]);
503 }
504 
505 #[test]
type_arguments_within_path()506 fn type_arguments_within_path() {
507     cov_mark::check!(type_arguments_within_path);
508     let code = r#"
509         mod foo {
510             pub struct Bar<T> {t: T}
511             impl<T> Bar<T> {
512                 pub fn baz() {}
513             }
514         }
515         fn f1() {foo::Bar::<i32>::baz();}
516         "#;
517     assert_no_match("foo::Bar::<i64>::baz()", code);
518     assert_matches("foo::Bar::<i32>::baz()", code, &["foo::Bar::<i32>::baz()"]);
519 }
520 
521 #[test]
literal_constraint()522 fn literal_constraint() {
523     cov_mark::check!(literal_constraint);
524     let code = r#"
525         enum Option<T> { Some(T), None }
526         use Option::Some;
527         fn f1() {
528             let x1 = Some(42);
529             let x2 = Some("foo");
530             let x3 = Some(x1);
531             let x4 = Some(40 + 2);
532             let x5 = Some(true);
533         }
534         "#;
535     assert_matches("Some(${a:kind(literal)})", code, &["Some(42)", "Some(\"foo\")", "Some(true)"]);
536     assert_matches("Some(${a:not(kind(literal))})", code, &["Some(x1)", "Some(40 + 2)"]);
537 }
538 
539 #[test]
match_reordered_struct_instantiation()540 fn match_reordered_struct_instantiation() {
541     assert_matches(
542         "Foo {aa: 1, b: 2, ccc: 3}",
543         "struct Foo {} fn f() {Foo {b: 2, ccc: 3, aa: 1}}",
544         &["Foo {b: 2, ccc: 3, aa: 1}"],
545     );
546     assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {b: 1}}");
547     assert_no_match("Foo {a: 1}", "struct Foo {} fn f() {Foo {a: 2}}");
548     assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {a: 1}}");
549     assert_no_match("Foo {a: 1, b: 2}", "struct Foo {} fn f() {Foo {b: 2}}");
550     assert_no_match("Foo {a: 1, }", "struct Foo {} fn f() {Foo {a: 1, b: 2}}");
551     assert_no_match("Foo {a: 1, z: 9}", "struct Foo {} fn f() {Foo {a: 1}}");
552 }
553 
554 #[test]
match_macro_invocation()555 fn match_macro_invocation() {
556     assert_matches(
557         "foo!($a)",
558         "macro_rules! foo {() => {}} fn() {foo(foo!(foo()))}",
559         &["foo!(foo())"],
560     );
561     assert_matches(
562         "foo!(41, $a, 43)",
563         "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43)}",
564         &["foo!(41, 42, 43)"],
565     );
566     assert_no_match("foo!(50, $a, 43)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
567     assert_no_match("foo!(41, $a, 50)", "macro_rules! foo {() => {}} fn() {foo!(41, 42, 43}");
568     assert_matches(
569         "foo!($a())",
570         "macro_rules! foo {() => {}} fn() {foo!(bar())}",
571         &["foo!(bar())"],
572     );
573 }
574 
575 // When matching within a macro expansion, we only allow matches of nodes that originated from
576 // the macro call, not from the macro definition.
577 #[test]
no_match_expression_from_macro()578 fn no_match_expression_from_macro() {
579     assert_no_match(
580         "$a.clone()",
581         r#"
582             macro_rules! m1 {
583                 () => {42.clone()}
584             }
585             fn f1() {m1!()}
586             "#,
587     );
588 }
589 
590 // We definitely don't want to allow matching of an expression that part originates from the
591 // macro call `42` and part from the macro definition `.clone()`.
592 #[test]
no_match_split_expression()593 fn no_match_split_expression() {
594     assert_no_match(
595         "$a.clone()",
596         r#"
597             macro_rules! m1 {
598                 ($x:expr) => {$x.clone()}
599             }
600             fn f1() {m1!(42)}
601             "#,
602     );
603 }
604 
605 #[test]
replace_function_call()606 fn replace_function_call() {
607     // This test also makes sure that we ignore empty-ranges.
608     assert_ssr_transform(
609         "foo() ==>> bar()",
610         "fn foo() {$0$0} fn bar() {} fn f1() {foo(); foo();}",
611         expect![["fn foo() {} fn bar() {} fn f1() {bar(); bar();}"]],
612     );
613 }
614 
615 #[test]
replace_function_call_with_placeholders()616 fn replace_function_call_with_placeholders() {
617     assert_ssr_transform(
618         "foo($a, $b) ==>> bar($b, $a)",
619         "fn foo() {} fn bar() {} fn f1() {foo(5, 42)}",
620         expect![["fn foo() {} fn bar() {} fn f1() {bar(42, 5)}"]],
621     );
622 }
623 
624 #[test]
replace_nested_function_calls()625 fn replace_nested_function_calls() {
626     assert_ssr_transform(
627         "foo($a) ==>> bar($a)",
628         "fn foo() {} fn bar() {} fn f1() {foo(foo(42))}",
629         expect![["fn foo() {} fn bar() {} fn f1() {bar(bar(42))}"]],
630     );
631 }
632 
633 #[test]
replace_associated_function_call()634 fn replace_associated_function_call() {
635     assert_ssr_transform(
636         "Foo::new() ==>> Bar::new()",
637         r#"
638             struct Foo {}
639             impl Foo { fn new() {} }
640             struct Bar {}
641             impl Bar { fn new() {} }
642             fn f1() {Foo::new();}
643             "#,
644         expect![[r#"
645             struct Foo {}
646             impl Foo { fn new() {} }
647             struct Bar {}
648             impl Bar { fn new() {} }
649             fn f1() {Bar::new();}
650         "#]],
651     );
652 }
653 
654 #[test]
replace_associated_trait_default_function_call()655 fn replace_associated_trait_default_function_call() {
656     cov_mark::check!(replace_associated_trait_default_function_call);
657     assert_ssr_transform(
658         "Bar2::foo() ==>> Bar2::foo2()",
659         r#"
660             trait Foo { fn foo() {} }
661             pub struct Bar {}
662             impl Foo for Bar {}
663             pub struct Bar2 {}
664             impl Foo for Bar2 {}
665             impl Bar2 { fn foo2() {} }
666             fn main() {
667                 Bar::foo();
668                 Bar2::foo();
669             }
670         "#,
671         expect![[r#"
672             trait Foo { fn foo() {} }
673             pub struct Bar {}
674             impl Foo for Bar {}
675             pub struct Bar2 {}
676             impl Foo for Bar2 {}
677             impl Bar2 { fn foo2() {} }
678             fn main() {
679                 Bar::foo();
680                 Bar2::foo2();
681             }
682         "#]],
683     );
684 }
685 
686 #[test]
replace_associated_trait_constant()687 fn replace_associated_trait_constant() {
688     cov_mark::check!(replace_associated_trait_constant);
689     assert_ssr_transform(
690         "Bar2::VALUE ==>> Bar2::VALUE_2222",
691         r#"
692             trait Foo { const VALUE: i32; const VALUE_2222: i32; }
693             pub struct Bar {}
694             impl Foo for Bar { const VALUE: i32 = 1;  const VALUE_2222: i32 = 2; }
695             pub struct Bar2 {}
696             impl Foo for Bar2 { const VALUE: i32 = 1;  const VALUE_2222: i32 = 2; }
697             impl Bar2 { fn foo2() {} }
698             fn main() {
699                 Bar::VALUE;
700                 Bar2::VALUE;
701             }
702             "#,
703         expect![[r#"
704             trait Foo { const VALUE: i32; const VALUE_2222: i32; }
705             pub struct Bar {}
706             impl Foo for Bar { const VALUE: i32 = 1;  const VALUE_2222: i32 = 2; }
707             pub struct Bar2 {}
708             impl Foo for Bar2 { const VALUE: i32 = 1;  const VALUE_2222: i32 = 2; }
709             impl Bar2 { fn foo2() {} }
710             fn main() {
711                 Bar::VALUE;
712                 Bar2::VALUE_2222;
713             }
714         "#]],
715     );
716 }
717 
718 #[test]
replace_path_in_different_contexts()719 fn replace_path_in_different_contexts() {
720     // Note the $0 inside module a::b which marks the point where the rule is interpreted. We
721     // replace foo with bar, but both need different path qualifiers in different contexts. In f4,
722     // foo is unqualified because of a use statement, however the replacement needs to be fully
723     // qualified.
724     assert_ssr_transform(
725         "c::foo() ==>> c::bar()",
726         r#"
727             mod a {
728                 pub mod b {$0
729                     pub mod c {
730                         pub fn foo() {}
731                         pub fn bar() {}
732                         fn f1() { foo() }
733                     }
734                     fn f2() { c::foo() }
735                 }
736                 fn f3() { b::c::foo() }
737             }
738             use a::b::c::foo;
739             fn f4() { foo() }
740             "#,
741         expect![[r#"
742             mod a {
743                 pub mod b {
744                     pub mod c {
745                         pub fn foo() {}
746                         pub fn bar() {}
747                         fn f1() { bar() }
748                     }
749                     fn f2() { c::bar() }
750                 }
751                 fn f3() { b::c::bar() }
752             }
753             use a::b::c::foo;
754             fn f4() { a::b::c::bar() }
755             "#]],
756     );
757 }
758 
759 #[test]
replace_associated_function_with_generics()760 fn replace_associated_function_with_generics() {
761     assert_ssr_transform(
762         "c::Foo::<$a>::new() ==>> d::Bar::<$a>::default()",
763         r#"
764             mod c {
765                 pub struct Foo<T> {v: T}
766                 impl<T> Foo<T> { pub fn new() {} }
767                 fn f1() {
768                     Foo::<i32>::new();
769                 }
770             }
771             mod d {
772                 pub struct Bar<T> {v: T}
773                 impl<T> Bar<T> { pub fn default() {} }
774                 fn f1() {
775                     super::c::Foo::<i32>::new();
776                 }
777             }
778             "#,
779         expect![[r#"
780             mod c {
781                 pub struct Foo<T> {v: T}
782                 impl<T> Foo<T> { pub fn new() {} }
783                 fn f1() {
784                     crate::d::Bar::<i32>::default();
785                 }
786             }
787             mod d {
788                 pub struct Bar<T> {v: T}
789                 impl<T> Bar<T> { pub fn default() {} }
790                 fn f1() {
791                     Bar::<i32>::default();
792                 }
793             }
794             "#]],
795     );
796 }
797 
798 #[test]
replace_type()799 fn replace_type() {
800     assert_ssr_transform(
801         "Result<(), $a> ==>> Option<$a>",
802         "struct Result<T, E> {} struct Option<T> {} fn f1() -> Result<(), Vec<Error>> {foo()}",
803         expect![[
804             "struct Result<T, E> {} struct Option<T> {} fn f1() -> Option<Vec<Error>> {foo()}"
805         ]],
806     );
807     assert_ssr_transform(
808         "dyn Trait<$a> ==>> DynTrait<$a>",
809         r#"
810 trait Trait<T> {}
811 struct DynTrait<T> {}
812 fn f1() -> dyn Trait<Vec<Error>> {foo()}
813 "#,
814         expect![[r#"
815 trait Trait<T> {}
816 struct DynTrait<T> {}
817 fn f1() -> DynTrait<Vec<Error>> {foo()}
818 "#]],
819     );
820 }
821 
822 #[test]
replace_macro_invocations()823 fn replace_macro_invocations() {
824     assert_ssr_transform(
825         "try!($a) ==>> $a?",
826         "macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(try!(foo()));}",
827         expect![["macro_rules! try {() => {}} fn f1() -> Result<(), E> {bar(foo()?);}"]],
828     );
829     // FIXME: Figure out why this doesn't work anymore
830     // assert_ssr_transform(
831     //     "foo!($a($b)) ==>> foo($b, $a)",
832     //     "macro_rules! foo {() => {}} fn f1() {foo!(abc(def() + 2));}",
833     //     expect![["macro_rules! foo {() => {}} fn f1() {foo(def() + 2, abc);}"]],
834     // );
835 }
836 
837 #[test]
replace_binary_op()838 fn replace_binary_op() {
839     assert_ssr_transform(
840         "$a + $b ==>> $b + $a",
841         "fn f() {2 * 3 + 4 * 5}",
842         expect![["fn f() {4 * 5 + 2 * 3}"]],
843     );
844     assert_ssr_transform(
845         "$a + $b ==>> $b + $a",
846         "fn f() {1 + 2 + 3 + 4}",
847         expect![[r#"fn f() {4 + (3 + (2 + 1))}"#]],
848     );
849 }
850 
851 #[test]
match_binary_op()852 fn match_binary_op() {
853     assert_matches("$a + $b", "fn f() {1 + 2 + 3 + 4}", &["1 + 2", "1 + 2 + 3", "1 + 2 + 3 + 4"]);
854 }
855 
856 #[test]
multiple_rules()857 fn multiple_rules() {
858     assert_ssr_transforms(
859         &["$a + 1 ==>> add_one($a)", "$a + $b ==>> add($a, $b)"],
860         "fn add() {} fn add_one() {} fn f() -> i32 {3 + 2 + 1}",
861         expect![["fn add() {} fn add_one() {} fn f() -> i32 {add_one(add(3, 2))}"]],
862     )
863 }
864 
865 #[test]
multiple_rules_with_nested_matches()866 fn multiple_rules_with_nested_matches() {
867     assert_ssr_transforms(
868         &["foo1($a) ==>> bar1($a)", "foo2($a) ==>> bar2($a)"],
869         r#"
870             fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
871             fn f() {foo1(foo2(foo1(foo2(foo1(42)))))}
872             "#,
873         expect![[r#"
874             fn foo1() {} fn foo2() {} fn bar1() {} fn bar2() {}
875             fn f() {bar1(bar2(bar1(bar2(bar1(42)))))}
876         "#]],
877     )
878 }
879 
880 #[test]
match_within_macro_invocation()881 fn match_within_macro_invocation() {
882     let code = r#"
883             macro_rules! foo {
884                 ($a:stmt; $b:expr) => {
885                     $b
886                 };
887             }
888             struct A {}
889             impl A {
890                 fn bar() {}
891             }
892             fn f1() {
893                 let aaa = A {};
894                 foo!(macro_ignores_this(); aaa.bar());
895             }
896         "#;
897     assert_matches("$a.bar()", code, &["aaa.bar()"]);
898 }
899 
900 #[test]
replace_within_macro_expansion()901 fn replace_within_macro_expansion() {
902     assert_ssr_transform(
903         "$a.foo() ==>> bar($a)",
904         r#"
905             macro_rules! macro1 {
906                 ($a:expr) => {$a}
907             }
908             fn bar() {}
909             fn f() {macro1!(5.x().foo().o2())}
910             "#,
911         expect![[r#"
912             macro_rules! macro1 {
913                 ($a:expr) => {$a}
914             }
915             fn bar() {}
916             fn f() {macro1!(bar(5.x()).o2())}
917             "#]],
918     )
919 }
920 
921 #[test]
replace_outside_and_within_macro_expansion()922 fn replace_outside_and_within_macro_expansion() {
923     assert_ssr_transform(
924         "foo($a) ==>> bar($a)",
925         r#"
926             fn foo() {} fn bar() {}
927             macro_rules! macro1 {
928                 ($a:expr) => {$a}
929             }
930             fn f() {foo(foo(macro1!(foo(foo(42)))))}
931             "#,
932         expect![[r#"
933             fn foo() {} fn bar() {}
934             macro_rules! macro1 {
935                 ($a:expr) => {$a}
936             }
937             fn f() {bar(bar(macro1!(bar(bar(42)))))}
938         "#]],
939     )
940 }
941 
942 #[test]
preserves_whitespace_within_macro_expansion()943 fn preserves_whitespace_within_macro_expansion() {
944     assert_ssr_transform(
945         "$a + $b ==>> $b - $a",
946         r#"
947             macro_rules! macro1 {
948                 ($a:expr) => {$a}
949             }
950             fn f() {macro1!(1   *   2 + 3 + 4)}
951             "#,
952         expect![[r#"
953             macro_rules! macro1 {
954                 ($a:expr) => {$a}
955             }
956             fn f() {macro1!(4 - (3 - 1   *   2))}
957             "#]],
958     )
959 }
960 
961 #[test]
add_parenthesis_when_necessary()962 fn add_parenthesis_when_necessary() {
963     assert_ssr_transform(
964         "foo($a) ==>> $a.to_string()",
965         r#"
966         fn foo(_: i32) {}
967         fn bar3(v: i32) {
968             foo(1 + 2);
969             foo(-v);
970         }
971         "#,
972         expect![[r#"
973             fn foo(_: i32) {}
974             fn bar3(v: i32) {
975                 (1 + 2).to_string();
976                 (-v).to_string();
977             }
978         "#]],
979     )
980 }
981 
982 #[test]
match_failure_reasons()983 fn match_failure_reasons() {
984     let code = r#"
985         fn bar() {}
986         macro_rules! foo {
987             ($a:expr) => {
988                 1 + $a + 2
989             };
990         }
991         fn f1() {
992             bar(1, 2);
993             foo!(5 + 43.to_string() + 5);
994         }
995         "#;
996     assert_match_failure_reason(
997         "bar($a, 3)",
998         code,
999         "bar(1, 2)",
1000         r#"Pattern wanted token '3' (INT_NUMBER), but code had token '2' (INT_NUMBER)"#,
1001     );
1002     assert_match_failure_reason(
1003         "42.to_string()",
1004         code,
1005         "43.to_string()",
1006         r#"Pattern wanted token '42' (INT_NUMBER), but code had token '43' (INT_NUMBER)"#,
1007     );
1008 }
1009 
1010 #[test]
overlapping_possible_matches()1011 fn overlapping_possible_matches() {
1012     // There are three possible matches here, however the middle one, `foo(foo(foo(42)))` shouldn't
1013     // match because it overlaps with the outer match. The inner match is permitted since it's is
1014     // contained entirely within the placeholder of the outer match.
1015     assert_matches(
1016         "foo(foo($a))",
1017         "fn foo() {} fn main() {foo(foo(foo(foo(42))))}",
1018         &["foo(foo(42))", "foo(foo(foo(foo(42))))"],
1019     );
1020 }
1021 
1022 #[test]
use_declaration_with_braces()1023 fn use_declaration_with_braces() {
1024     // It would be OK for a path rule to match and alter a use declaration. We shouldn't mess it up
1025     // though. In particular, we must not change `use foo::{baz, bar}` to `use foo::{baz,
1026     // foo2::bar2}`.
1027     cov_mark::check!(use_declaration_with_braces);
1028     assert_ssr_transform(
1029         "foo::bar ==>> foo2::bar2",
1030         r#"
1031         mod foo { pub fn bar() {} pub fn baz() {} }
1032         mod foo2 { pub fn bar2() {} }
1033         use foo::{baz, bar};
1034         fn main() { bar() }
1035         "#,
1036         expect![["
1037         mod foo { pub fn bar() {} pub fn baz() {} }
1038         mod foo2 { pub fn bar2() {} }
1039         use foo::{baz, bar};
1040         fn main() { foo2::bar2() }
1041         "]],
1042     )
1043 }
1044 
1045 #[test]
ufcs_matches_method_call()1046 fn ufcs_matches_method_call() {
1047     let code = r#"
1048     struct Foo {}
1049     impl Foo {
1050         fn new(_: i32) -> Foo { Foo {} }
1051         fn do_stuff(&self, _: i32) {}
1052     }
1053     struct Bar {}
1054     impl Bar {
1055         fn new(_: i32) -> Bar { Bar {} }
1056         fn do_stuff(&self, v: i32) {}
1057     }
1058     fn main() {
1059         let b = Bar {};
1060         let f = Foo {};
1061         b.do_stuff(1);
1062         f.do_stuff(2);
1063         Foo::new(4).do_stuff(3);
1064         // Too many / too few args - should never match
1065         f.do_stuff(2, 10);
1066         f.do_stuff();
1067     }
1068     "#;
1069     assert_matches("Foo::do_stuff($a, $b)", code, &["f.do_stuff(2)", "Foo::new(4).do_stuff(3)"]);
1070     // The arguments needs special handling in the case of a function call matching a method call
1071     // and the first argument is different.
1072     assert_matches("Foo::do_stuff($a, 2)", code, &["f.do_stuff(2)"]);
1073     assert_matches("Foo::do_stuff(Foo::new(4), $b)", code, &["Foo::new(4).do_stuff(3)"]);
1074 
1075     assert_ssr_transform(
1076         "Foo::do_stuff(Foo::new($a), $b) ==>> Bar::new($b).do_stuff($a)",
1077         code,
1078         expect![[r#"
1079             struct Foo {}
1080             impl Foo {
1081                 fn new(_: i32) -> Foo { Foo {} }
1082                 fn do_stuff(&self, _: i32) {}
1083             }
1084             struct Bar {}
1085             impl Bar {
1086                 fn new(_: i32) -> Bar { Bar {} }
1087                 fn do_stuff(&self, v: i32) {}
1088             }
1089             fn main() {
1090                 let b = Bar {};
1091                 let f = Foo {};
1092                 b.do_stuff(1);
1093                 f.do_stuff(2);
1094                 Bar::new(3).do_stuff(4);
1095                 // Too many / too few args - should never match
1096                 f.do_stuff(2, 10);
1097                 f.do_stuff();
1098             }
1099         "#]],
1100     );
1101 }
1102 
1103 #[test]
pattern_is_a_single_segment_path()1104 fn pattern_is_a_single_segment_path() {
1105     cov_mark::check!(pattern_is_a_single_segment_path);
1106     // The first function should not be altered because the `foo` in scope at the cursor position is
1107     // a different `foo`. This case is special because "foo" can be parsed as a pattern (IDENT_PAT ->
1108     // NAME -> IDENT), which contains no path. If we're not careful we'll end up matching the `foo`
1109     // in `let foo` from the first function. Whether we should match the `let foo` in the second
1110     // function is less clear. At the moment, we don't. Doing so sounds like a rename operation,
1111     // which isn't really what SSR is for, especially since the replacement `bar` must be able to be
1112     // resolved, which means if we rename `foo` we'll get a name collision.
1113     assert_ssr_transform(
1114         "foo ==>> bar",
1115         r#"
1116         fn f1() -> i32 {
1117             let foo = 1;
1118             let bar = 2;
1119             foo
1120         }
1121         fn f1() -> i32 {
1122             let foo = 1;
1123             let bar = 2;
1124             foo$0
1125         }
1126         "#,
1127         expect![[r#"
1128             fn f1() -> i32 {
1129                 let foo = 1;
1130                 let bar = 2;
1131                 foo
1132             }
1133             fn f1() -> i32 {
1134                 let foo = 1;
1135                 let bar = 2;
1136                 bar
1137             }
1138         "#]],
1139     );
1140 }
1141 
1142 #[test]
replace_local_variable_reference()1143 fn replace_local_variable_reference() {
1144     // The pattern references a local variable `foo` in the block containing the cursor. We should
1145     // only replace references to this variable `foo`, not other variables that just happen to have
1146     // the same name.
1147     cov_mark::check!(cursor_after_semicolon);
1148     assert_ssr_transform(
1149         "foo + $a ==>> $a - foo",
1150         r#"
1151             fn bar1() -> i32 {
1152                 let mut res = 0;
1153                 let foo = 5;
1154                 res += foo + 1;
1155                 let foo = 10;
1156                 res += foo + 2;$0
1157                 res += foo + 3;
1158                 let foo = 15;
1159                 res += foo + 4;
1160                 res
1161             }
1162             "#,
1163         expect![[r#"
1164             fn bar1() -> i32 {
1165                 let mut res = 0;
1166                 let foo = 5;
1167                 res += foo + 1;
1168                 let foo = 10;
1169                 res += 2 - foo;
1170                 res += 3 - foo;
1171                 let foo = 15;
1172                 res += foo + 4;
1173                 res
1174             }
1175         "#]],
1176     )
1177 }
1178 
1179 #[test]
replace_path_within_selection()1180 fn replace_path_within_selection() {
1181     assert_ssr_transform(
1182         "foo ==>> bar",
1183         r#"
1184         fn main() {
1185             let foo = 41;
1186             let bar = 42;
1187             do_stuff(foo);
1188             do_stuff(foo);$0
1189             do_stuff(foo);
1190             do_stuff(foo);$0
1191             do_stuff(foo);
1192         }"#,
1193         expect![[r#"
1194             fn main() {
1195                 let foo = 41;
1196                 let bar = 42;
1197                 do_stuff(foo);
1198                 do_stuff(foo);
1199                 do_stuff(bar);
1200                 do_stuff(bar);
1201                 do_stuff(foo);
1202             }"#]],
1203     );
1204 }
1205 
1206 #[test]
replace_nonpath_within_selection()1207 fn replace_nonpath_within_selection() {
1208     cov_mark::check!(replace_nonpath_within_selection);
1209     assert_ssr_transform(
1210         "$a + $b ==>> $b * $a",
1211         r#"
1212         fn main() {
1213             let v = 1 + 2;$0
1214             let v2 = 3 + 3;
1215             let v3 = 4 + 5;$0
1216             let v4 = 6 + 7;
1217         }"#,
1218         expect![[r#"
1219             fn main() {
1220                 let v = 1 + 2;
1221                 let v2 = 3 * 3;
1222                 let v3 = 5 * 4;
1223                 let v4 = 6 + 7;
1224             }"#]],
1225     );
1226 }
1227 
1228 #[test]
replace_self()1229 fn replace_self() {
1230     // `foo(self)` occurs twice in the code, however only the first occurrence is the `self` that's
1231     // in scope where the rule is invoked.
1232     assert_ssr_transform(
1233         "foo(self) ==>> bar(self)",
1234         r#"
1235         struct S1 {}
1236         fn foo(_: &S1) {}
1237         fn bar(_: &S1) {}
1238         impl S1 {
1239             fn f1(&self) {
1240                 foo(self)$0
1241             }
1242             fn f2(&self) {
1243                 foo(self)
1244             }
1245         }
1246         "#,
1247         expect![[r#"
1248             struct S1 {}
1249             fn foo(_: &S1) {}
1250             fn bar(_: &S1) {}
1251             impl S1 {
1252                 fn f1(&self) {
1253                     bar(self)
1254                 }
1255                 fn f2(&self) {
1256                     foo(self)
1257                 }
1258             }
1259         "#]],
1260     );
1261 }
1262 
1263 #[test]
match_trait_method_call()1264 fn match_trait_method_call() {
1265     // `Bar::foo` and `Bar2::foo` resolve to the same function. Make sure we only match if the type
1266     // matches what's in the pattern. Also checks that we handle autoderef.
1267     let code = r#"
1268         pub struct Bar {}
1269         pub struct Bar2 {}
1270         pub trait Foo {
1271             fn foo(&self, _: i32) {}
1272         }
1273         impl Foo for Bar {}
1274         impl Foo for Bar2 {}
1275         fn main() {
1276             let v1 = Bar {};
1277             let v2 = Bar2 {};
1278             let v1_ref = &v1;
1279             let v2_ref = &v2;
1280             v1.foo(1);
1281             v2.foo(2);
1282             Bar::foo(&v1, 3);
1283             Bar2::foo(&v2, 4);
1284             v1_ref.foo(5);
1285             v2_ref.foo(6);
1286         }
1287         "#;
1288     assert_matches("Bar::foo($a, $b)", code, &["v1.foo(1)", "Bar::foo(&v1, 3)", "v1_ref.foo(5)"]);
1289     assert_matches("Bar2::foo($a, $b)", code, &["v2.foo(2)", "Bar2::foo(&v2, 4)", "v2_ref.foo(6)"]);
1290 }
1291 
1292 #[test]
replace_autoref_autoderef_capture()1293 fn replace_autoref_autoderef_capture() {
1294     // Here we have several calls to `$a.foo()`. In the first case autoref is applied, in the
1295     // second, we already have a reference, so it isn't. When $a is used in a context where autoref
1296     // doesn't apply, we need to prefix it with `&`. Finally, we have some cases where autoderef
1297     // needs to be applied.
1298     cov_mark::check!(replace_autoref_autoderef_capture);
1299     let code = r#"
1300         struct Foo {}
1301         impl Foo {
1302             fn foo(&self) {}
1303             fn foo2(&self) {}
1304         }
1305         fn bar(_: &Foo) {}
1306         fn main() {
1307             let f = Foo {};
1308             let fr = &f;
1309             let fr2 = &fr;
1310             let fr3 = &fr2;
1311             f.foo();
1312             fr.foo();
1313             fr2.foo();
1314             fr3.foo();
1315         }
1316         "#;
1317     assert_ssr_transform(
1318         "Foo::foo($a) ==>> bar($a)",
1319         code,
1320         expect![[r#"
1321             struct Foo {}
1322             impl Foo {
1323                 fn foo(&self) {}
1324                 fn foo2(&self) {}
1325             }
1326             fn bar(_: &Foo) {}
1327             fn main() {
1328                 let f = Foo {};
1329                 let fr = &f;
1330                 let fr2 = &fr;
1331                 let fr3 = &fr2;
1332                 bar(&f);
1333                 bar(&*fr);
1334                 bar(&**fr2);
1335                 bar(&***fr3);
1336             }
1337         "#]],
1338     );
1339     // If the placeholder is used as the receiver of another method call, then we don't need to
1340     // explicitly autoderef or autoref.
1341     assert_ssr_transform(
1342         "Foo::foo($a) ==>> $a.foo2()",
1343         code,
1344         expect![[r#"
1345             struct Foo {}
1346             impl Foo {
1347                 fn foo(&self) {}
1348                 fn foo2(&self) {}
1349             }
1350             fn bar(_: &Foo) {}
1351             fn main() {
1352                 let f = Foo {};
1353                 let fr = &f;
1354                 let fr2 = &fr;
1355                 let fr3 = &fr2;
1356                 f.foo2();
1357                 fr.foo2();
1358                 fr2.foo2();
1359                 fr3.foo2();
1360             }
1361         "#]],
1362     );
1363 }
1364 
1365 #[test]
replace_autoref_mut()1366 fn replace_autoref_mut() {
1367     let code = r#"
1368         struct Foo {}
1369         impl Foo {
1370             fn foo(&mut self) {}
1371         }
1372         fn bar(_: &mut Foo) {}
1373         fn main() {
1374             let mut f = Foo {};
1375             f.foo();
1376             let fr = &mut f;
1377             fr.foo();
1378         }
1379         "#;
1380     assert_ssr_transform(
1381         "Foo::foo($a) ==>> bar($a)",
1382         code,
1383         expect![[r#"
1384             struct Foo {}
1385             impl Foo {
1386                 fn foo(&mut self) {}
1387             }
1388             fn bar(_: &mut Foo) {}
1389             fn main() {
1390                 let mut f = Foo {};
1391                 bar(&mut f);
1392                 let fr = &mut f;
1393                 bar(&mut *fr);
1394             }
1395         "#]],
1396     );
1397 }
1398