1 //! Generated by `sourcegen_assists_docs`, do not edit by hand.
2
3 use super::check_doc_test;
4
5 #[test]
doctest_add_braces()6 fn doctest_add_braces() {
7 check_doc_test(
8 "add_braces",
9 r#####"
10 fn foo(n: i32) -> i32 {
11 match n {
12 1 =>$0 n + 1,
13 _ => 0
14 }
15 }
16 "#####,
17 r#####"
18 fn foo(n: i32) -> i32 {
19 match n {
20 1 => {
21 n + 1
22 },
23 _ => 0
24 }
25 }
26 "#####,
27 )
28 }
29
30 #[test]
doctest_add_explicit_type()31 fn doctest_add_explicit_type() {
32 check_doc_test(
33 "add_explicit_type",
34 r#####"
35 fn main() {
36 let x$0 = 92;
37 }
38 "#####,
39 r#####"
40 fn main() {
41 let x: i32 = 92;
42 }
43 "#####,
44 )
45 }
46
47 #[test]
doctest_add_hash()48 fn doctest_add_hash() {
49 check_doc_test(
50 "add_hash",
51 r#####"
52 fn main() {
53 r#"Hello,$0 World!"#;
54 }
55 "#####,
56 r#####"
57 fn main() {
58 r##"Hello, World!"##;
59 }
60 "#####,
61 )
62 }
63
64 #[test]
doctest_add_impl_default_members()65 fn doctest_add_impl_default_members() {
66 check_doc_test(
67 "add_impl_default_members",
68 r#####"
69 trait Trait {
70 type X;
71 fn foo(&self);
72 fn bar(&self) {}
73 }
74
75 impl Trait for () {
76 type X = ();
77 fn foo(&self) {}$0
78 }
79 "#####,
80 r#####"
81 trait Trait {
82 type X;
83 fn foo(&self);
84 fn bar(&self) {}
85 }
86
87 impl Trait for () {
88 type X = ();
89 fn foo(&self) {}
90
91 $0fn bar(&self) {}
92 }
93 "#####,
94 )
95 }
96
97 #[test]
doctest_add_impl_missing_members()98 fn doctest_add_impl_missing_members() {
99 check_doc_test(
100 "add_impl_missing_members",
101 r#####"
102 trait Trait<T> {
103 type X;
104 fn foo(&self) -> T;
105 fn bar(&self) {}
106 }
107
108 impl Trait<u32> for () {$0
109
110 }
111 "#####,
112 r#####"
113 trait Trait<T> {
114 type X;
115 fn foo(&self) -> T;
116 fn bar(&self) {}
117 }
118
119 impl Trait<u32> for () {
120 $0type X;
121
122 fn foo(&self) -> u32 {
123 todo!()
124 }
125 }
126 "#####,
127 )
128 }
129
130 #[test]
doctest_add_label_to_loop()131 fn doctest_add_label_to_loop() {
132 check_doc_test(
133 "add_label_to_loop",
134 r#####"
135 fn main() {
136 loop$0 {
137 break;
138 continue;
139 }
140 }
141 "#####,
142 r#####"
143 fn main() {
144 'l: loop {
145 break 'l;
146 continue 'l;
147 }
148 }
149 "#####,
150 )
151 }
152
153 #[test]
doctest_add_lifetime_to_type()154 fn doctest_add_lifetime_to_type() {
155 check_doc_test(
156 "add_lifetime_to_type",
157 r#####"
158 struct Point {
159 x: &$0u32,
160 y: u32,
161 }
162 "#####,
163 r#####"
164 struct Point<'a> {
165 x: &'a u32,
166 y: u32,
167 }
168 "#####,
169 )
170 }
171
172 #[test]
doctest_add_missing_match_arms()173 fn doctest_add_missing_match_arms() {
174 check_doc_test(
175 "add_missing_match_arms",
176 r#####"
177 enum Action { Move { distance: u32 }, Stop }
178
179 fn handle(action: Action) {
180 match action {
181 $0
182 }
183 }
184 "#####,
185 r#####"
186 enum Action { Move { distance: u32 }, Stop }
187
188 fn handle(action: Action) {
189 match action {
190 $0Action::Move { distance } => todo!(),
191 Action::Stop => todo!(),
192 }
193 }
194 "#####,
195 )
196 }
197
198 #[test]
doctest_add_return_type()199 fn doctest_add_return_type() {
200 check_doc_test(
201 "add_return_type",
202 r#####"
203 fn foo() { 4$02i32 }
204 "#####,
205 r#####"
206 fn foo() -> i32 { 42i32 }
207 "#####,
208 )
209 }
210
211 #[test]
doctest_add_turbo_fish()212 fn doctest_add_turbo_fish() {
213 check_doc_test(
214 "add_turbo_fish",
215 r#####"
216 fn make<T>() -> T { todo!() }
217 fn main() {
218 let x = make$0();
219 }
220 "#####,
221 r#####"
222 fn make<T>() -> T { todo!() }
223 fn main() {
224 let x = make::<${0:_}>();
225 }
226 "#####,
227 )
228 }
229
230 #[test]
doctest_apply_demorgan()231 fn doctest_apply_demorgan() {
232 check_doc_test(
233 "apply_demorgan",
234 r#####"
235 fn main() {
236 if x != 4 ||$0 y < 3.14 {}
237 }
238 "#####,
239 r#####"
240 fn main() {
241 if !(x == 4 && y >= 3.14) {}
242 }
243 "#####,
244 )
245 }
246
247 #[test]
doctest_auto_import()248 fn doctest_auto_import() {
249 check_doc_test(
250 "auto_import",
251 r#####"
252 fn main() {
253 let map = HashMap$0::new();
254 }
255 pub mod std { pub mod collections { pub struct HashMap { } } }
256 "#####,
257 r#####"
258 use std::collections::HashMap;
259
260 fn main() {
261 let map = HashMap::new();
262 }
263 pub mod std { pub mod collections { pub struct HashMap { } } }
264 "#####,
265 )
266 }
267
268 #[test]
doctest_change_visibility()269 fn doctest_change_visibility() {
270 check_doc_test(
271 "change_visibility",
272 r#####"
273 $0fn frobnicate() {}
274 "#####,
275 r#####"
276 pub(crate) fn frobnicate() {}
277 "#####,
278 )
279 }
280
281 #[test]
doctest_convert_bool_then_to_if()282 fn doctest_convert_bool_then_to_if() {
283 check_doc_test(
284 "convert_bool_then_to_if",
285 r#####"
286 //- minicore: bool_impl
287 fn main() {
288 (0 == 0).then$0(|| val)
289 }
290 "#####,
291 r#####"
292 fn main() {
293 if 0 == 0 {
294 Some(val)
295 } else {
296 None
297 }
298 }
299 "#####,
300 )
301 }
302
303 #[test]
doctest_convert_for_loop_with_for_each()304 fn doctest_convert_for_loop_with_for_each() {
305 check_doc_test(
306 "convert_for_loop_with_for_each",
307 r#####"
308 fn main() {
309 let x = vec![1, 2, 3];
310 for$0 v in x {
311 let y = v * 2;
312 }
313 }
314 "#####,
315 r#####"
316 fn main() {
317 let x = vec![1, 2, 3];
318 x.into_iter().for_each(|v| {
319 let y = v * 2;
320 });
321 }
322 "#####,
323 )
324 }
325
326 #[test]
doctest_convert_if_to_bool_then()327 fn doctest_convert_if_to_bool_then() {
328 check_doc_test(
329 "convert_if_to_bool_then",
330 r#####"
331 //- minicore: option
332 fn main() {
333 if$0 cond {
334 Some(val)
335 } else {
336 None
337 }
338 }
339 "#####,
340 r#####"
341 fn main() {
342 cond.then(|| val)
343 }
344 "#####,
345 )
346 }
347
348 #[test]
doctest_convert_integer_literal()349 fn doctest_convert_integer_literal() {
350 check_doc_test(
351 "convert_integer_literal",
352 r#####"
353 const _: i32 = 10$0;
354 "#####,
355 r#####"
356 const _: i32 = 0b1010;
357 "#####,
358 )
359 }
360
361 #[test]
doctest_convert_into_to_from()362 fn doctest_convert_into_to_from() {
363 check_doc_test(
364 "convert_into_to_from",
365 r#####"
366 //- minicore: from
367 impl $0Into<Thing> for usize {
368 fn into(self) -> Thing {
369 Thing {
370 b: self.to_string(),
371 a: self
372 }
373 }
374 }
375 "#####,
376 r#####"
377 impl From<usize> for Thing {
378 fn from(val: usize) -> Self {
379 Thing {
380 b: val.to_string(),
381 a: val
382 }
383 }
384 }
385 "#####,
386 )
387 }
388
389 #[test]
doctest_convert_iter_for_each_to_for()390 fn doctest_convert_iter_for_each_to_for() {
391 check_doc_test(
392 "convert_iter_for_each_to_for",
393 r#####"
394 //- minicore: iterators
395 use core::iter;
396 fn main() {
397 let iter = iter::repeat((9, 2));
398 iter.for_each$0(|(x, y)| {
399 println!("x: {}, y: {}", x, y);
400 });
401 }
402 "#####,
403 r#####"
404 use core::iter;
405 fn main() {
406 let iter = iter::repeat((9, 2));
407 for (x, y) in iter {
408 println!("x: {}, y: {}", x, y);
409 }
410 }
411 "#####,
412 )
413 }
414
415 #[test]
doctest_convert_let_else_to_match()416 fn doctest_convert_let_else_to_match() {
417 check_doc_test(
418 "convert_let_else_to_match",
419 r#####"
420 fn main() {
421 let Ok(mut x) = f() else$0 { return };
422 }
423 "#####,
424 r#####"
425 fn main() {
426 let mut x = match f() {
427 Ok(x) => x,
428 _ => return,
429 };
430 }
431 "#####,
432 )
433 }
434
435 #[test]
doctest_convert_match_to_let_else()436 fn doctest_convert_match_to_let_else() {
437 check_doc_test(
438 "convert_match_to_let_else",
439 r#####"
440 //- minicore: option
441 fn foo(opt: Option<()>) {
442 let val$0 = match opt {
443 Some(it) => it,
444 None => return,
445 };
446 }
447 "#####,
448 r#####"
449 fn foo(opt: Option<()>) {
450 let Some(val) = opt else { return };
451 }
452 "#####,
453 )
454 }
455
456 #[test]
doctest_convert_named_struct_to_tuple_struct()457 fn doctest_convert_named_struct_to_tuple_struct() {
458 check_doc_test(
459 "convert_named_struct_to_tuple_struct",
460 r#####"
461 struct Point$0 { x: f32, y: f32 }
462
463 impl Point {
464 pub fn new(x: f32, y: f32) -> Self {
465 Point { x, y }
466 }
467
468 pub fn x(&self) -> f32 {
469 self.x
470 }
471
472 pub fn y(&self) -> f32 {
473 self.y
474 }
475 }
476 "#####,
477 r#####"
478 struct Point(f32, f32);
479
480 impl Point {
481 pub fn new(x: f32, y: f32) -> Self {
482 Point(x, y)
483 }
484
485 pub fn x(&self) -> f32 {
486 self.0
487 }
488
489 pub fn y(&self) -> f32 {
490 self.1
491 }
492 }
493 "#####,
494 )
495 }
496
497 #[test]
doctest_convert_nested_function_to_closure()498 fn doctest_convert_nested_function_to_closure() {
499 check_doc_test(
500 "convert_nested_function_to_closure",
501 r#####"
502 fn main() {
503 fn fo$0o(label: &str, number: u64) {
504 println!("{}: {}", label, number);
505 }
506
507 foo("Bar", 100);
508 }
509 "#####,
510 r#####"
511 fn main() {
512 let foo = |label: &str, number: u64| {
513 println!("{}: {}", label, number);
514 };
515
516 foo("Bar", 100);
517 }
518 "#####,
519 )
520 }
521
522 #[test]
doctest_convert_to_guarded_return()523 fn doctest_convert_to_guarded_return() {
524 check_doc_test(
525 "convert_to_guarded_return",
526 r#####"
527 fn main() {
528 $0if cond {
529 foo();
530 bar();
531 }
532 }
533 "#####,
534 r#####"
535 fn main() {
536 if !cond {
537 return;
538 }
539 foo();
540 bar();
541 }
542 "#####,
543 )
544 }
545
546 #[test]
doctest_convert_tuple_struct_to_named_struct()547 fn doctest_convert_tuple_struct_to_named_struct() {
548 check_doc_test(
549 "convert_tuple_struct_to_named_struct",
550 r#####"
551 struct Point$0(f32, f32);
552
553 impl Point {
554 pub fn new(x: f32, y: f32) -> Self {
555 Point(x, y)
556 }
557
558 pub fn x(&self) -> f32 {
559 self.0
560 }
561
562 pub fn y(&self) -> f32 {
563 self.1
564 }
565 }
566 "#####,
567 r#####"
568 struct Point { field1: f32, field2: f32 }
569
570 impl Point {
571 pub fn new(x: f32, y: f32) -> Self {
572 Point { field1: x, field2: y }
573 }
574
575 pub fn x(&self) -> f32 {
576 self.field1
577 }
578
579 pub fn y(&self) -> f32 {
580 self.field2
581 }
582 }
583 "#####,
584 )
585 }
586
587 #[test]
doctest_convert_two_arm_bool_match_to_matches_macro()588 fn doctest_convert_two_arm_bool_match_to_matches_macro() {
589 check_doc_test(
590 "convert_two_arm_bool_match_to_matches_macro",
591 r#####"
592 fn main() {
593 match scrutinee$0 {
594 Some(val) if val.cond() => true,
595 _ => false,
596 }
597 }
598 "#####,
599 r#####"
600 fn main() {
601 matches!(scrutinee, Some(val) if val.cond())
602 }
603 "#####,
604 )
605 }
606
607 #[test]
doctest_convert_while_to_loop()608 fn doctest_convert_while_to_loop() {
609 check_doc_test(
610 "convert_while_to_loop",
611 r#####"
612 fn main() {
613 $0while cond {
614 foo();
615 }
616 }
617 "#####,
618 r#####"
619 fn main() {
620 loop {
621 if !cond {
622 break;
623 }
624 foo();
625 }
626 }
627 "#####,
628 )
629 }
630
631 #[test]
doctest_destructure_tuple_binding()632 fn doctest_destructure_tuple_binding() {
633 check_doc_test(
634 "destructure_tuple_binding",
635 r#####"
636 fn main() {
637 let $0t = (1,2);
638 let v = t.0;
639 }
640 "#####,
641 r#####"
642 fn main() {
643 let ($0_0, _1) = (1,2);
644 let v = _0;
645 }
646 "#####,
647 )
648 }
649
650 #[test]
doctest_desugar_doc_comment()651 fn doctest_desugar_doc_comment() {
652 check_doc_test(
653 "desugar_doc_comment",
654 r#####"
655 /// Multi-line$0
656 /// comment
657 "#####,
658 r#####"
659 #[doc = r"Multi-line
660 comment"]
661 "#####,
662 )
663 }
664
665 #[test]
doctest_expand_glob_import()666 fn doctest_expand_glob_import() {
667 check_doc_test(
668 "expand_glob_import",
669 r#####"
670 mod foo {
671 pub struct Bar;
672 pub struct Baz;
673 }
674
675 use foo::*$0;
676
677 fn qux(bar: Bar, baz: Baz) {}
678 "#####,
679 r#####"
680 mod foo {
681 pub struct Bar;
682 pub struct Baz;
683 }
684
685 use foo::{Bar, Baz};
686
687 fn qux(bar: Bar, baz: Baz) {}
688 "#####,
689 )
690 }
691
692 #[test]
doctest_extract_expressions_from_format_string()693 fn doctest_extract_expressions_from_format_string() {
694 check_doc_test(
695 "extract_expressions_from_format_string",
696 r#####"
697 macro_rules! format_args {
698 ($lit:literal $(tt:tt)*) => { 0 },
699 }
700 macro_rules! print {
701 ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
702 }
703
704 fn main() {
705 print!("{var} {x + 1}$0");
706 }
707 "#####,
708 r#####"
709 macro_rules! format_args {
710 ($lit:literal $(tt:tt)*) => { 0 },
711 }
712 macro_rules! print {
713 ($($arg:tt)*) => (std::io::_print(format_args!($($arg)*)));
714 }
715
716 fn main() {
717 print!("{var} {}"$0, x + 1);
718 }
719 "#####,
720 )
721 }
722
723 #[test]
doctest_extract_function()724 fn doctest_extract_function() {
725 check_doc_test(
726 "extract_function",
727 r#####"
728 fn main() {
729 let n = 1;
730 $0let m = n + 2;
731 // calculate
732 let k = m + n;$0
733 let g = 3;
734 }
735 "#####,
736 r#####"
737 fn main() {
738 let n = 1;
739 fun_name(n);
740 let g = 3;
741 }
742
743 fn $0fun_name(n: i32) {
744 let m = n + 2;
745 // calculate
746 let k = m + n;
747 }
748 "#####,
749 )
750 }
751
752 #[test]
doctest_extract_module()753 fn doctest_extract_module() {
754 check_doc_test(
755 "extract_module",
756 r#####"
757 $0fn foo(name: i32) -> i32 {
758 name + 1
759 }$0
760
761 fn bar(name: i32) -> i32 {
762 name + 2
763 }
764 "#####,
765 r#####"
766 mod modname {
767 pub(crate) fn foo(name: i32) -> i32 {
768 name + 1
769 }
770 }
771
772 fn bar(name: i32) -> i32 {
773 name + 2
774 }
775 "#####,
776 )
777 }
778
779 #[test]
doctest_extract_struct_from_enum_variant()780 fn doctest_extract_struct_from_enum_variant() {
781 check_doc_test(
782 "extract_struct_from_enum_variant",
783 r#####"
784 enum A { $0One(u32, u32) }
785 "#####,
786 r#####"
787 struct One(u32, u32);
788
789 enum A { One(One) }
790 "#####,
791 )
792 }
793
794 #[test]
doctest_extract_type_alias()795 fn doctest_extract_type_alias() {
796 check_doc_test(
797 "extract_type_alias",
798 r#####"
799 struct S {
800 field: $0(u8, u8, u8)$0,
801 }
802 "#####,
803 r#####"
804 type $0Type = (u8, u8, u8);
805
806 struct S {
807 field: Type,
808 }
809 "#####,
810 )
811 }
812
813 #[test]
doctest_extract_variable()814 fn doctest_extract_variable() {
815 check_doc_test(
816 "extract_variable",
817 r#####"
818 fn main() {
819 $0(1 + 2)$0 * 4;
820 }
821 "#####,
822 r#####"
823 fn main() {
824 let $0var_name = (1 + 2);
825 var_name * 4;
826 }
827 "#####,
828 )
829 }
830
831 #[test]
doctest_fix_visibility()832 fn doctest_fix_visibility() {
833 check_doc_test(
834 "fix_visibility",
835 r#####"
836 mod m {
837 fn frobnicate() {}
838 }
839 fn main() {
840 m::frobnicate$0();
841 }
842 "#####,
843 r#####"
844 mod m {
845 $0pub(crate) fn frobnicate() {}
846 }
847 fn main() {
848 m::frobnicate();
849 }
850 "#####,
851 )
852 }
853
854 #[test]
doctest_flip_binexpr()855 fn doctest_flip_binexpr() {
856 check_doc_test(
857 "flip_binexpr",
858 r#####"
859 fn main() {
860 let _ = 90 +$0 2;
861 }
862 "#####,
863 r#####"
864 fn main() {
865 let _ = 2 + 90;
866 }
867 "#####,
868 )
869 }
870
871 #[test]
doctest_flip_comma()872 fn doctest_flip_comma() {
873 check_doc_test(
874 "flip_comma",
875 r#####"
876 fn main() {
877 ((1, 2),$0 (3, 4));
878 }
879 "#####,
880 r#####"
881 fn main() {
882 ((3, 4), (1, 2));
883 }
884 "#####,
885 )
886 }
887
888 #[test]
doctest_flip_trait_bound()889 fn doctest_flip_trait_bound() {
890 check_doc_test(
891 "flip_trait_bound",
892 r#####"
893 fn foo<T: Clone +$0 Copy>() { }
894 "#####,
895 r#####"
896 fn foo<T: Copy + Clone>() { }
897 "#####,
898 )
899 }
900
901 #[test]
doctest_generate_constant()902 fn doctest_generate_constant() {
903 check_doc_test(
904 "generate_constant",
905 r#####"
906 struct S { i: usize }
907 impl S { pub fn new(n: usize) {} }
908 fn main() {
909 let v = S::new(CAPA$0CITY);
910 }
911 "#####,
912 r#####"
913 struct S { i: usize }
914 impl S { pub fn new(n: usize) {} }
915 fn main() {
916 const CAPACITY: usize = $0;
917 let v = S::new(CAPACITY);
918 }
919 "#####,
920 )
921 }
922
923 #[test]
doctest_generate_default_from_enum_variant()924 fn doctest_generate_default_from_enum_variant() {
925 check_doc_test(
926 "generate_default_from_enum_variant",
927 r#####"
928 enum Version {
929 Undefined,
930 Minor$0,
931 Major,
932 }
933 "#####,
934 r#####"
935 enum Version {
936 Undefined,
937 Minor,
938 Major,
939 }
940
941 impl Default for Version {
942 fn default() -> Self {
943 Self::Minor
944 }
945 }
946 "#####,
947 )
948 }
949
950 #[test]
doctest_generate_default_from_new()951 fn doctest_generate_default_from_new() {
952 check_doc_test(
953 "generate_default_from_new",
954 r#####"
955 struct Example { _inner: () }
956
957 impl Example {
958 pub fn n$0ew() -> Self {
959 Self { _inner: () }
960 }
961 }
962 "#####,
963 r#####"
964 struct Example { _inner: () }
965
966 impl Example {
967 pub fn new() -> Self {
968 Self { _inner: () }
969 }
970 }
971
972 impl Default for Example {
973 fn default() -> Self {
974 Self::new()
975 }
976 }
977 "#####,
978 )
979 }
980
981 #[test]
doctest_generate_delegate_methods()982 fn doctest_generate_delegate_methods() {
983 check_doc_test(
984 "generate_delegate_methods",
985 r#####"
986 struct Age(u8);
987 impl Age {
988 fn age(&self) -> u8 {
989 self.0
990 }
991 }
992
993 struct Person {
994 ag$0e: Age,
995 }
996 "#####,
997 r#####"
998 struct Age(u8);
999 impl Age {
1000 fn age(&self) -> u8 {
1001 self.0
1002 }
1003 }
1004
1005 struct Person {
1006 age: Age,
1007 }
1008
1009 impl Person {
1010 $0fn age(&self) -> u8 {
1011 self.age.age()
1012 }
1013 }
1014 "#####,
1015 )
1016 }
1017
1018 #[test]
doctest_generate_deref()1019 fn doctest_generate_deref() {
1020 check_doc_test(
1021 "generate_deref",
1022 r#####"
1023 //- minicore: deref, deref_mut
1024 struct A;
1025 struct B {
1026 $0a: A
1027 }
1028 "#####,
1029 r#####"
1030 struct A;
1031 struct B {
1032 a: A
1033 }
1034
1035 impl core::ops::Deref for B {
1036 type Target = A;
1037
1038 fn deref(&self) -> &Self::Target {
1039 &self.a
1040 }
1041 }
1042 "#####,
1043 )
1044 }
1045
1046 #[test]
doctest_generate_derive()1047 fn doctest_generate_derive() {
1048 check_doc_test(
1049 "generate_derive",
1050 r#####"
1051 struct Point {
1052 x: u32,
1053 y: u32,$0
1054 }
1055 "#####,
1056 r#####"
1057 #[derive($0)]
1058 struct Point {
1059 x: u32,
1060 y: u32,
1061 }
1062 "#####,
1063 )
1064 }
1065
1066 #[test]
doctest_generate_doc_example()1067 fn doctest_generate_doc_example() {
1068 check_doc_test(
1069 "generate_doc_example",
1070 r#####"
1071 /// Adds two numbers.$0
1072 pub fn add(a: i32, b: i32) -> i32 { a + b }
1073 "#####,
1074 r#####"
1075 /// Adds two numbers.
1076 ///
1077 /// # Examples
1078 ///
1079 /// ```
1080 /// use test::add;
1081 ///
1082 /// assert_eq!(add(a, b), );
1083 /// ```
1084 pub fn add(a: i32, b: i32) -> i32 { a + b }
1085 "#####,
1086 )
1087 }
1088
1089 #[test]
doctest_generate_documentation_template()1090 fn doctest_generate_documentation_template() {
1091 check_doc_test(
1092 "generate_documentation_template",
1093 r#####"
1094 pub struct S;
1095 impl S {
1096 pub unsafe fn set_len$0(&mut self, len: usize) -> Result<(), std::io::Error> {
1097 /* ... */
1098 }
1099 }
1100 "#####,
1101 r#####"
1102 pub struct S;
1103 impl S {
1104 /// Sets the length of this [`S`].
1105 ///
1106 /// # Errors
1107 ///
1108 /// This function will return an error if .
1109 ///
1110 /// # Safety
1111 ///
1112 /// .
1113 pub unsafe fn set_len(&mut self, len: usize) -> Result<(), std::io::Error> {
1114 /* ... */
1115 }
1116 }
1117 "#####,
1118 )
1119 }
1120
1121 #[test]
doctest_generate_enum_as_method()1122 fn doctest_generate_enum_as_method() {
1123 check_doc_test(
1124 "generate_enum_as_method",
1125 r#####"
1126 enum Value {
1127 Number(i32),
1128 Text(String)$0,
1129 }
1130 "#####,
1131 r#####"
1132 enum Value {
1133 Number(i32),
1134 Text(String),
1135 }
1136
1137 impl Value {
1138 fn as_text(&self) -> Option<&String> {
1139 if let Self::Text(v) = self {
1140 Some(v)
1141 } else {
1142 None
1143 }
1144 }
1145 }
1146 "#####,
1147 )
1148 }
1149
1150 #[test]
doctest_generate_enum_is_method()1151 fn doctest_generate_enum_is_method() {
1152 check_doc_test(
1153 "generate_enum_is_method",
1154 r#####"
1155 enum Version {
1156 Undefined,
1157 Minor$0,
1158 Major,
1159 }
1160 "#####,
1161 r#####"
1162 enum Version {
1163 Undefined,
1164 Minor,
1165 Major,
1166 }
1167
1168 impl Version {
1169 /// Returns `true` if the version is [`Minor`].
1170 ///
1171 /// [`Minor`]: Version::Minor
1172 #[must_use]
1173 fn is_minor(&self) -> bool {
1174 matches!(self, Self::Minor)
1175 }
1176 }
1177 "#####,
1178 )
1179 }
1180
1181 #[test]
doctest_generate_enum_try_into_method()1182 fn doctest_generate_enum_try_into_method() {
1183 check_doc_test(
1184 "generate_enum_try_into_method",
1185 r#####"
1186 enum Value {
1187 Number(i32),
1188 Text(String)$0,
1189 }
1190 "#####,
1191 r#####"
1192 enum Value {
1193 Number(i32),
1194 Text(String),
1195 }
1196
1197 impl Value {
1198 fn try_into_text(self) -> Result<String, Self> {
1199 if let Self::Text(v) = self {
1200 Ok(v)
1201 } else {
1202 Err(self)
1203 }
1204 }
1205 }
1206 "#####,
1207 )
1208 }
1209
1210 #[test]
doctest_generate_enum_variant()1211 fn doctest_generate_enum_variant() {
1212 check_doc_test(
1213 "generate_enum_variant",
1214 r#####"
1215 enum Countries {
1216 Ghana,
1217 }
1218
1219 fn main() {
1220 let country = Countries::Lesotho$0;
1221 }
1222 "#####,
1223 r#####"
1224 enum Countries {
1225 Ghana,
1226 Lesotho,
1227 }
1228
1229 fn main() {
1230 let country = Countries::Lesotho;
1231 }
1232 "#####,
1233 )
1234 }
1235
1236 #[test]
doctest_generate_from_impl_for_enum()1237 fn doctest_generate_from_impl_for_enum() {
1238 check_doc_test(
1239 "generate_from_impl_for_enum",
1240 r#####"
1241 enum A { $0One(u32) }
1242 "#####,
1243 r#####"
1244 enum A { One(u32) }
1245
1246 impl From<u32> for A {
1247 fn from(v: u32) -> Self {
1248 Self::One(v)
1249 }
1250 }
1251 "#####,
1252 )
1253 }
1254
1255 #[test]
doctest_generate_function()1256 fn doctest_generate_function() {
1257 check_doc_test(
1258 "generate_function",
1259 r#####"
1260 struct Baz;
1261 fn baz() -> Baz { Baz }
1262 fn foo() {
1263 bar$0("", baz());
1264 }
1265
1266 "#####,
1267 r#####"
1268 struct Baz;
1269 fn baz() -> Baz { Baz }
1270 fn foo() {
1271 bar("", baz());
1272 }
1273
1274 fn bar(arg: &str, baz: Baz) ${0:-> _} {
1275 todo!()
1276 }
1277
1278 "#####,
1279 )
1280 }
1281
1282 #[test]
doctest_generate_getter()1283 fn doctest_generate_getter() {
1284 check_doc_test(
1285 "generate_getter",
1286 r#####"
1287 //- minicore: as_ref
1288 pub struct String;
1289 impl AsRef<str> for String {
1290 fn as_ref(&self) -> &str {
1291 ""
1292 }
1293 }
1294
1295 struct Person {
1296 nam$0e: String,
1297 }
1298 "#####,
1299 r#####"
1300 pub struct String;
1301 impl AsRef<str> for String {
1302 fn as_ref(&self) -> &str {
1303 ""
1304 }
1305 }
1306
1307 struct Person {
1308 name: String,
1309 }
1310
1311 impl Person {
1312 fn $0name(&self) -> &str {
1313 self.name.as_ref()
1314 }
1315 }
1316 "#####,
1317 )
1318 }
1319
1320 #[test]
doctest_generate_getter_mut()1321 fn doctest_generate_getter_mut() {
1322 check_doc_test(
1323 "generate_getter_mut",
1324 r#####"
1325 struct Person {
1326 nam$0e: String,
1327 }
1328 "#####,
1329 r#####"
1330 struct Person {
1331 name: String,
1332 }
1333
1334 impl Person {
1335 fn $0name_mut(&mut self) -> &mut String {
1336 &mut self.name
1337 }
1338 }
1339 "#####,
1340 )
1341 }
1342
1343 #[test]
doctest_generate_impl()1344 fn doctest_generate_impl() {
1345 check_doc_test(
1346 "generate_impl",
1347 r#####"
1348 struct Ctx$0<T: Clone> {
1349 data: T,
1350 }
1351 "#####,
1352 r#####"
1353 struct Ctx<T: Clone> {
1354 data: T,
1355 }
1356
1357 impl<T: Clone> Ctx<T> {
1358 $0
1359 }
1360 "#####,
1361 )
1362 }
1363
1364 #[test]
doctest_generate_is_empty_from_len()1365 fn doctest_generate_is_empty_from_len() {
1366 check_doc_test(
1367 "generate_is_empty_from_len",
1368 r#####"
1369 struct MyStruct { data: Vec<String> }
1370
1371 impl MyStruct {
1372 #[must_use]
1373 p$0ub fn len(&self) -> usize {
1374 self.data.len()
1375 }
1376 }
1377 "#####,
1378 r#####"
1379 struct MyStruct { data: Vec<String> }
1380
1381 impl MyStruct {
1382 #[must_use]
1383 pub fn len(&self) -> usize {
1384 self.data.len()
1385 }
1386
1387 #[must_use]
1388 pub fn is_empty(&self) -> bool {
1389 self.len() == 0
1390 }
1391 }
1392 "#####,
1393 )
1394 }
1395
1396 #[test]
doctest_generate_new()1397 fn doctest_generate_new() {
1398 check_doc_test(
1399 "generate_new",
1400 r#####"
1401 struct Ctx<T: Clone> {
1402 data: T,$0
1403 }
1404 "#####,
1405 r#####"
1406 struct Ctx<T: Clone> {
1407 data: T,
1408 }
1409
1410 impl<T: Clone> Ctx<T> {
1411 fn $0new(data: T) -> Self { Self { data } }
1412 }
1413 "#####,
1414 )
1415 }
1416
1417 #[test]
doctest_generate_setter()1418 fn doctest_generate_setter() {
1419 check_doc_test(
1420 "generate_setter",
1421 r#####"
1422 struct Person {
1423 nam$0e: String,
1424 }
1425 "#####,
1426 r#####"
1427 struct Person {
1428 name: String,
1429 }
1430
1431 impl Person {
1432 fn set_name(&mut self, name: String) {
1433 self.name = name;
1434 }
1435 }
1436 "#####,
1437 )
1438 }
1439
1440 #[test]
doctest_generate_trait_impl()1441 fn doctest_generate_trait_impl() {
1442 check_doc_test(
1443 "generate_trait_impl",
1444 r#####"
1445 struct $0Ctx<T: Clone> {
1446 data: T,
1447 }
1448 "#####,
1449 r#####"
1450 struct Ctx<T: Clone> {
1451 data: T,
1452 }
1453
1454 impl<T: Clone> $0 for Ctx<T> {
1455
1456 }
1457 "#####,
1458 )
1459 }
1460
1461 #[test]
doctest_inline_call()1462 fn doctest_inline_call() {
1463 check_doc_test(
1464 "inline_call",
1465 r#####"
1466 //- minicore: option
1467 fn foo(name: Option<&str>) {
1468 let name = name.unwrap$0();
1469 }
1470 "#####,
1471 r#####"
1472 fn foo(name: Option<&str>) {
1473 let name = match name {
1474 Some(val) => val,
1475 None => panic!("called `Option::unwrap()` on a `None` value"),
1476 };
1477 }
1478 "#####,
1479 )
1480 }
1481
1482 #[test]
doctest_inline_const_as_literal()1483 fn doctest_inline_const_as_literal() {
1484 check_doc_test(
1485 "inline_const_as_literal",
1486 r#####"
1487 const STRING: &str = "Hello, World!";
1488
1489 fn something() -> &'static str {
1490 STRING$0
1491 }
1492 "#####,
1493 r#####"
1494 const STRING: &str = "Hello, World!";
1495
1496 fn something() -> &'static str {
1497 "Hello, World!"
1498 }
1499 "#####,
1500 )
1501 }
1502
1503 #[test]
doctest_inline_into_callers()1504 fn doctest_inline_into_callers() {
1505 check_doc_test(
1506 "inline_into_callers",
1507 r#####"
1508 fn print(_: &str) {}
1509 fn foo$0(word: &str) {
1510 if !word.is_empty() {
1511 print(word);
1512 }
1513 }
1514 fn bar() {
1515 foo("안녕하세요");
1516 foo("여러분");
1517 }
1518 "#####,
1519 r#####"
1520 fn print(_: &str) {}
1521
1522 fn bar() {
1523 {
1524 let word = "안녕하세요";
1525 if !word.is_empty() {
1526 print(word);
1527 }
1528 };
1529 {
1530 let word = "여러분";
1531 if !word.is_empty() {
1532 print(word);
1533 }
1534 };
1535 }
1536 "#####,
1537 )
1538 }
1539
1540 #[test]
doctest_inline_local_variable()1541 fn doctest_inline_local_variable() {
1542 check_doc_test(
1543 "inline_local_variable",
1544 r#####"
1545 fn main() {
1546 let x$0 = 1 + 2;
1547 x * 4;
1548 }
1549 "#####,
1550 r#####"
1551 fn main() {
1552 (1 + 2) * 4;
1553 }
1554 "#####,
1555 )
1556 }
1557
1558 #[test]
doctest_inline_macro()1559 fn doctest_inline_macro() {
1560 check_doc_test(
1561 "inline_macro",
1562 r#####"
1563 macro_rules! num {
1564 (+$($t:tt)+) => (1 + num!($($t )+));
1565 (-$($t:tt)+) => (-1 + num!($($t )+));
1566 (+) => (1);
1567 (-) => (-1);
1568 }
1569
1570 fn main() {
1571 let number = num$0!(+ + + - + +);
1572 println!("{number}");
1573 }
1574 "#####,
1575 r#####"
1576 macro_rules! num {
1577 (+$($t:tt)+) => (1 + num!($($t )+));
1578 (-$($t:tt)+) => (-1 + num!($($t )+));
1579 (+) => (1);
1580 (-) => (-1);
1581 }
1582
1583 fn main() {
1584 let number = 1+num!(+ + - + +);
1585 println!("{number}");
1586 }
1587 "#####,
1588 )
1589 }
1590
1591 #[test]
doctest_inline_type_alias()1592 fn doctest_inline_type_alias() {
1593 check_doc_test(
1594 "inline_type_alias",
1595 r#####"
1596 type A<T = u32> = Vec<T>;
1597
1598 fn main() {
1599 let a: $0A;
1600 }
1601 "#####,
1602 r#####"
1603 type A<T = u32> = Vec<T>;
1604
1605 fn main() {
1606 let a: Vec<u32>;
1607 }
1608 "#####,
1609 )
1610 }
1611
1612 #[test]
doctest_inline_type_alias_uses()1613 fn doctest_inline_type_alias_uses() {
1614 check_doc_test(
1615 "inline_type_alias_uses",
1616 r#####"
1617 type $0A = i32;
1618 fn id(x: A) -> A {
1619 x
1620 };
1621 fn foo() {
1622 let _: A = 3;
1623 }
1624 "#####,
1625 r#####"
1626
1627 fn id(x: i32) -> i32 {
1628 x
1629 };
1630 fn foo() {
1631 let _: i32 = 3;
1632 }
1633 "#####,
1634 )
1635 }
1636
1637 #[test]
doctest_introduce_named_generic()1638 fn doctest_introduce_named_generic() {
1639 check_doc_test(
1640 "introduce_named_generic",
1641 r#####"
1642 fn foo(bar: $0impl Bar) {}
1643 "#####,
1644 r#####"
1645 fn foo<$0B: Bar>(bar: B) {}
1646 "#####,
1647 )
1648 }
1649
1650 #[test]
doctest_introduce_named_lifetime()1651 fn doctest_introduce_named_lifetime() {
1652 check_doc_test(
1653 "introduce_named_lifetime",
1654 r#####"
1655 impl Cursor<'_$0> {
1656 fn node(self) -> &SyntaxNode {
1657 match self {
1658 Cursor::Replace(node) | Cursor::Before(node) => node,
1659 }
1660 }
1661 }
1662 "#####,
1663 r#####"
1664 impl<'a> Cursor<'a> {
1665 fn node(self) -> &SyntaxNode {
1666 match self {
1667 Cursor::Replace(node) | Cursor::Before(node) => node,
1668 }
1669 }
1670 }
1671 "#####,
1672 )
1673 }
1674
1675 #[test]
doctest_invert_if()1676 fn doctest_invert_if() {
1677 check_doc_test(
1678 "invert_if",
1679 r#####"
1680 fn main() {
1681 if$0 !y { A } else { B }
1682 }
1683 "#####,
1684 r#####"
1685 fn main() {
1686 if y { B } else { A }
1687 }
1688 "#####,
1689 )
1690 }
1691
1692 #[test]
doctest_line_to_block()1693 fn doctest_line_to_block() {
1694 check_doc_test(
1695 "line_to_block",
1696 r#####"
1697 // Multi-line$0
1698 // comment
1699 "#####,
1700 r#####"
1701 /*
1702 Multi-line
1703 comment
1704 */
1705 "#####,
1706 )
1707 }
1708
1709 #[test]
doctest_make_raw_string()1710 fn doctest_make_raw_string() {
1711 check_doc_test(
1712 "make_raw_string",
1713 r#####"
1714 fn main() {
1715 "Hello,$0 World!";
1716 }
1717 "#####,
1718 r#####"
1719 fn main() {
1720 r#"Hello, World!"#;
1721 }
1722 "#####,
1723 )
1724 }
1725
1726 #[test]
doctest_make_usual_string()1727 fn doctest_make_usual_string() {
1728 check_doc_test(
1729 "make_usual_string",
1730 r#####"
1731 fn main() {
1732 r#"Hello,$0 "World!""#;
1733 }
1734 "#####,
1735 r#####"
1736 fn main() {
1737 "Hello, \"World!\"";
1738 }
1739 "#####,
1740 )
1741 }
1742
1743 #[test]
doctest_merge_imports()1744 fn doctest_merge_imports() {
1745 check_doc_test(
1746 "merge_imports",
1747 r#####"
1748 use std::$0fmt::Formatter;
1749 use std::io;
1750 "#####,
1751 r#####"
1752 use std::{fmt::Formatter, io};
1753 "#####,
1754 )
1755 }
1756
1757 #[test]
doctest_merge_match_arms()1758 fn doctest_merge_match_arms() {
1759 check_doc_test(
1760 "merge_match_arms",
1761 r#####"
1762 enum Action { Move { distance: u32 }, Stop }
1763
1764 fn handle(action: Action) {
1765 match action {
1766 $0Action::Move(..) => foo(),
1767 Action::Stop => foo(),
1768 }
1769 }
1770 "#####,
1771 r#####"
1772 enum Action { Move { distance: u32 }, Stop }
1773
1774 fn handle(action: Action) {
1775 match action {
1776 Action::Move(..) | Action::Stop => foo(),
1777 }
1778 }
1779 "#####,
1780 )
1781 }
1782
1783 #[test]
doctest_move_arm_cond_to_match_guard()1784 fn doctest_move_arm_cond_to_match_guard() {
1785 check_doc_test(
1786 "move_arm_cond_to_match_guard",
1787 r#####"
1788 enum Action { Move { distance: u32 }, Stop }
1789
1790 fn handle(action: Action) {
1791 match action {
1792 Action::Move { distance } => $0if distance > 10 { foo() },
1793 _ => (),
1794 }
1795 }
1796 "#####,
1797 r#####"
1798 enum Action { Move { distance: u32 }, Stop }
1799
1800 fn handle(action: Action) {
1801 match action {
1802 Action::Move { distance } if distance > 10 => foo(),
1803 _ => (),
1804 }
1805 }
1806 "#####,
1807 )
1808 }
1809
1810 #[test]
doctest_move_bounds_to_where_clause()1811 fn doctest_move_bounds_to_where_clause() {
1812 check_doc_test(
1813 "move_bounds_to_where_clause",
1814 r#####"
1815 fn apply<T, U, $0F: FnOnce(T) -> U>(f: F, x: T) -> U {
1816 f(x)
1817 }
1818 "#####,
1819 r#####"
1820 fn apply<T, U, F>(f: F, x: T) -> U where F: FnOnce(T) -> U {
1821 f(x)
1822 }
1823 "#####,
1824 )
1825 }
1826
1827 #[test]
doctest_move_const_to_impl()1828 fn doctest_move_const_to_impl() {
1829 check_doc_test(
1830 "move_const_to_impl",
1831 r#####"
1832 struct S;
1833 impl S {
1834 fn foo() -> usize {
1835 /// The answer.
1836 const C$0: usize = 42;
1837
1838 C * C
1839 }
1840 }
1841 "#####,
1842 r#####"
1843 struct S;
1844 impl S {
1845 /// The answer.
1846 const C: usize = 42;
1847
1848 fn foo() -> usize {
1849 Self::C * Self::C
1850 }
1851 }
1852 "#####,
1853 )
1854 }
1855
1856 #[test]
doctest_move_from_mod_rs()1857 fn doctest_move_from_mod_rs() {
1858 check_doc_test(
1859 "move_from_mod_rs",
1860 r#####"
1861 //- /main.rs
1862 mod a;
1863 //- /a/mod.rs
1864 $0fn t() {}$0
1865 "#####,
1866 r#####"
1867 fn t() {}
1868 "#####,
1869 )
1870 }
1871
1872 #[test]
doctest_move_guard_to_arm_body()1873 fn doctest_move_guard_to_arm_body() {
1874 check_doc_test(
1875 "move_guard_to_arm_body",
1876 r#####"
1877 enum Action { Move { distance: u32 }, Stop }
1878
1879 fn handle(action: Action) {
1880 match action {
1881 Action::Move { distance } $0if distance > 10 => foo(),
1882 _ => (),
1883 }
1884 }
1885 "#####,
1886 r#####"
1887 enum Action { Move { distance: u32 }, Stop }
1888
1889 fn handle(action: Action) {
1890 match action {
1891 Action::Move { distance } => if distance > 10 {
1892 foo()
1893 },
1894 _ => (),
1895 }
1896 }
1897 "#####,
1898 )
1899 }
1900
1901 #[test]
doctest_move_module_to_file()1902 fn doctest_move_module_to_file() {
1903 check_doc_test(
1904 "move_module_to_file",
1905 r#####"
1906 mod $0foo {
1907 fn t() {}
1908 }
1909 "#####,
1910 r#####"
1911 mod foo;
1912 "#####,
1913 )
1914 }
1915
1916 #[test]
doctest_move_to_mod_rs()1917 fn doctest_move_to_mod_rs() {
1918 check_doc_test(
1919 "move_to_mod_rs",
1920 r#####"
1921 //- /main.rs
1922 mod a;
1923 //- /a.rs
1924 $0fn t() {}$0
1925 "#####,
1926 r#####"
1927 fn t() {}
1928 "#####,
1929 )
1930 }
1931
1932 #[test]
doctest_promote_local_to_const()1933 fn doctest_promote_local_to_const() {
1934 check_doc_test(
1935 "promote_local_to_const",
1936 r#####"
1937 fn main() {
1938 let foo$0 = true;
1939
1940 if foo {
1941 println!("It's true");
1942 } else {
1943 println!("It's false");
1944 }
1945 }
1946 "#####,
1947 r#####"
1948 fn main() {
1949 const $0FOO: bool = true;
1950
1951 if FOO {
1952 println!("It's true");
1953 } else {
1954 println!("It's false");
1955 }
1956 }
1957 "#####,
1958 )
1959 }
1960
1961 #[test]
doctest_pull_assignment_up()1962 fn doctest_pull_assignment_up() {
1963 check_doc_test(
1964 "pull_assignment_up",
1965 r#####"
1966 fn main() {
1967 let mut foo = 6;
1968
1969 if true {
1970 $0foo = 5;
1971 } else {
1972 foo = 4;
1973 }
1974 }
1975 "#####,
1976 r#####"
1977 fn main() {
1978 let mut foo = 6;
1979
1980 foo = if true {
1981 5
1982 } else {
1983 4
1984 };
1985 }
1986 "#####,
1987 )
1988 }
1989
1990 #[test]
doctest_qualify_method_call()1991 fn doctest_qualify_method_call() {
1992 check_doc_test(
1993 "qualify_method_call",
1994 r#####"
1995 struct Foo;
1996 impl Foo {
1997 fn foo(&self) {}
1998 }
1999 fn main() {
2000 let foo = Foo;
2001 foo.fo$0o();
2002 }
2003 "#####,
2004 r#####"
2005 struct Foo;
2006 impl Foo {
2007 fn foo(&self) {}
2008 }
2009 fn main() {
2010 let foo = Foo;
2011 Foo::foo(&foo);
2012 }
2013 "#####,
2014 )
2015 }
2016
2017 #[test]
doctest_qualify_path()2018 fn doctest_qualify_path() {
2019 check_doc_test(
2020 "qualify_path",
2021 r#####"
2022 fn main() {
2023 let map = HashMap$0::new();
2024 }
2025 pub mod std { pub mod collections { pub struct HashMap { } } }
2026 "#####,
2027 r#####"
2028 fn main() {
2029 let map = std::collections::HashMap::new();
2030 }
2031 pub mod std { pub mod collections { pub struct HashMap { } } }
2032 "#####,
2033 )
2034 }
2035
2036 #[test]
doctest_reformat_number_literal()2037 fn doctest_reformat_number_literal() {
2038 check_doc_test(
2039 "reformat_number_literal",
2040 r#####"
2041 const _: i32 = 1012345$0;
2042 "#####,
2043 r#####"
2044 const _: i32 = 1_012_345;
2045 "#####,
2046 )
2047 }
2048
2049 #[test]
doctest_remove_dbg()2050 fn doctest_remove_dbg() {
2051 check_doc_test(
2052 "remove_dbg",
2053 r#####"
2054 fn main() {
2055 let x = $0dbg!(42 * dbg!(4 + 2));$0
2056 }
2057 "#####,
2058 r#####"
2059 fn main() {
2060 let x = 42 * (4 + 2);
2061 }
2062 "#####,
2063 )
2064 }
2065
2066 #[test]
doctest_remove_hash()2067 fn doctest_remove_hash() {
2068 check_doc_test(
2069 "remove_hash",
2070 r#####"
2071 fn main() {
2072 r#"Hello,$0 World!"#;
2073 }
2074 "#####,
2075 r#####"
2076 fn main() {
2077 r"Hello, World!";
2078 }
2079 "#####,
2080 )
2081 }
2082
2083 #[test]
doctest_remove_mut()2084 fn doctest_remove_mut() {
2085 check_doc_test(
2086 "remove_mut",
2087 r#####"
2088 impl Walrus {
2089 fn feed(&mut$0 self, amount: u32) {}
2090 }
2091 "#####,
2092 r#####"
2093 impl Walrus {
2094 fn feed(&self, amount: u32) {}
2095 }
2096 "#####,
2097 )
2098 }
2099
2100 #[test]
doctest_remove_parentheses()2101 fn doctest_remove_parentheses() {
2102 check_doc_test(
2103 "remove_parentheses",
2104 r#####"
2105 fn main() {
2106 _ = $0(2) + 2;
2107 }
2108 "#####,
2109 r#####"
2110 fn main() {
2111 _ = 2 + 2;
2112 }
2113 "#####,
2114 )
2115 }
2116
2117 #[test]
doctest_remove_unused_param()2118 fn doctest_remove_unused_param() {
2119 check_doc_test(
2120 "remove_unused_param",
2121 r#####"
2122 fn frobnicate(x: i32$0) {}
2123
2124 fn main() {
2125 frobnicate(92);
2126 }
2127 "#####,
2128 r#####"
2129 fn frobnicate() {}
2130
2131 fn main() {
2132 frobnicate();
2133 }
2134 "#####,
2135 )
2136 }
2137
2138 #[test]
doctest_reorder_fields()2139 fn doctest_reorder_fields() {
2140 check_doc_test(
2141 "reorder_fields",
2142 r#####"
2143 struct Foo {foo: i32, bar: i32};
2144 const test: Foo = $0Foo {bar: 0, foo: 1}
2145 "#####,
2146 r#####"
2147 struct Foo {foo: i32, bar: i32};
2148 const test: Foo = Foo {foo: 1, bar: 0}
2149 "#####,
2150 )
2151 }
2152
2153 #[test]
doctest_reorder_impl_items()2154 fn doctest_reorder_impl_items() {
2155 check_doc_test(
2156 "reorder_impl_items",
2157 r#####"
2158 trait Foo {
2159 type A;
2160 const B: u8;
2161 fn c();
2162 }
2163
2164 struct Bar;
2165 $0impl Foo for Bar$0 {
2166 const B: u8 = 17;
2167 fn c() {}
2168 type A = String;
2169 }
2170 "#####,
2171 r#####"
2172 trait Foo {
2173 type A;
2174 const B: u8;
2175 fn c();
2176 }
2177
2178 struct Bar;
2179 impl Foo for Bar {
2180 type A = String;
2181 const B: u8 = 17;
2182 fn c() {}
2183 }
2184 "#####,
2185 )
2186 }
2187
2188 #[test]
doctest_replace_arith_with_checked()2189 fn doctest_replace_arith_with_checked() {
2190 check_doc_test(
2191 "replace_arith_with_checked",
2192 r#####"
2193 fn main() {
2194 let x = 1 $0+ 2;
2195 }
2196 "#####,
2197 r#####"
2198 fn main() {
2199 let x = 1.checked_add(2);
2200 }
2201 "#####,
2202 )
2203 }
2204
2205 #[test]
doctest_replace_arith_with_saturating()2206 fn doctest_replace_arith_with_saturating() {
2207 check_doc_test(
2208 "replace_arith_with_saturating",
2209 r#####"
2210 fn main() {
2211 let x = 1 $0+ 2;
2212 }
2213 "#####,
2214 r#####"
2215 fn main() {
2216 let x = 1.saturating_add(2);
2217 }
2218 "#####,
2219 )
2220 }
2221
2222 #[test]
doctest_replace_arith_with_wrapping()2223 fn doctest_replace_arith_with_wrapping() {
2224 check_doc_test(
2225 "replace_arith_with_wrapping",
2226 r#####"
2227 fn main() {
2228 let x = 1 $0+ 2;
2229 }
2230 "#####,
2231 r#####"
2232 fn main() {
2233 let x = 1.wrapping_add(2);
2234 }
2235 "#####,
2236 )
2237 }
2238
2239 #[test]
doctest_replace_char_with_string()2240 fn doctest_replace_char_with_string() {
2241 check_doc_test(
2242 "replace_char_with_string",
2243 r#####"
2244 fn main() {
2245 find('{$0');
2246 }
2247 "#####,
2248 r#####"
2249 fn main() {
2250 find("{");
2251 }
2252 "#####,
2253 )
2254 }
2255
2256 #[test]
doctest_replace_derive_with_manual_impl()2257 fn doctest_replace_derive_with_manual_impl() {
2258 check_doc_test(
2259 "replace_derive_with_manual_impl",
2260 r#####"
2261 //- minicore: derive
2262 trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<()>; }
2263 #[derive(Deb$0ug, Display)]
2264 struct S;
2265 "#####,
2266 r#####"
2267 trait Debug { fn fmt(&self, f: &mut Formatter) -> Result<()>; }
2268 #[derive(Display)]
2269 struct S;
2270
2271 impl Debug for S {
2272 $0fn fmt(&self, f: &mut Formatter) -> Result<()> {
2273 f.debug_struct("S").finish()
2274 }
2275 }
2276 "#####,
2277 )
2278 }
2279
2280 #[test]
doctest_replace_if_let_with_match()2281 fn doctest_replace_if_let_with_match() {
2282 check_doc_test(
2283 "replace_if_let_with_match",
2284 r#####"
2285 enum Action { Move { distance: u32 }, Stop }
2286
2287 fn handle(action: Action) {
2288 $0if let Action::Move { distance } = action {
2289 foo(distance)
2290 } else {
2291 bar()
2292 }
2293 }
2294 "#####,
2295 r#####"
2296 enum Action { Move { distance: u32 }, Stop }
2297
2298 fn handle(action: Action) {
2299 match action {
2300 Action::Move { distance } => foo(distance),
2301 _ => bar(),
2302 }
2303 }
2304 "#####,
2305 )
2306 }
2307
2308 #[test]
doctest_replace_let_with_if_let()2309 fn doctest_replace_let_with_if_let() {
2310 check_doc_test(
2311 "replace_let_with_if_let",
2312 r#####"
2313 enum Option<T> { Some(T), None }
2314
2315 fn main(action: Action) {
2316 $0let x = compute();
2317 }
2318
2319 fn compute() -> Option<i32> { None }
2320 "#####,
2321 r#####"
2322 enum Option<T> { Some(T), None }
2323
2324 fn main(action: Action) {
2325 if let Some(x) = compute() {
2326 }
2327 }
2328
2329 fn compute() -> Option<i32> { None }
2330 "#####,
2331 )
2332 }
2333
2334 #[test]
doctest_replace_match_with_if_let()2335 fn doctest_replace_match_with_if_let() {
2336 check_doc_test(
2337 "replace_match_with_if_let",
2338 r#####"
2339 enum Action { Move { distance: u32 }, Stop }
2340
2341 fn handle(action: Action) {
2342 $0match action {
2343 Action::Move { distance } => foo(distance),
2344 _ => bar(),
2345 }
2346 }
2347 "#####,
2348 r#####"
2349 enum Action { Move { distance: u32 }, Stop }
2350
2351 fn handle(action: Action) {
2352 if let Action::Move { distance } = action {
2353 foo(distance)
2354 } else {
2355 bar()
2356 }
2357 }
2358 "#####,
2359 )
2360 }
2361
2362 #[test]
doctest_replace_named_generic_with_impl()2363 fn doctest_replace_named_generic_with_impl() {
2364 check_doc_test(
2365 "replace_named_generic_with_impl",
2366 r#####"
2367 fn new<P$0: AsRef<Path>>(location: P) -> Self {}
2368 "#####,
2369 r#####"
2370 fn new(location: impl AsRef<Path>) -> Self {}
2371 "#####,
2372 )
2373 }
2374
2375 #[test]
doctest_replace_qualified_name_with_use()2376 fn doctest_replace_qualified_name_with_use() {
2377 check_doc_test(
2378 "replace_qualified_name_with_use",
2379 r#####"
2380 mod std { pub mod collections { pub struct HashMap<T, U>(T, U); } }
2381 fn process(map: std::collections::$0HashMap<String, String>) {}
2382 "#####,
2383 r#####"
2384 use std::collections::HashMap;
2385
2386 mod std { pub mod collections { pub struct HashMap<T, U>(T, U); } }
2387 fn process(map: HashMap<String, String>) {}
2388 "#####,
2389 )
2390 }
2391
2392 #[test]
doctest_replace_string_with_char()2393 fn doctest_replace_string_with_char() {
2394 check_doc_test(
2395 "replace_string_with_char",
2396 r#####"
2397 fn main() {
2398 find("{$0");
2399 }
2400 "#####,
2401 r#####"
2402 fn main() {
2403 find('{');
2404 }
2405 "#####,
2406 )
2407 }
2408
2409 #[test]
doctest_replace_try_expr_with_match()2410 fn doctest_replace_try_expr_with_match() {
2411 check_doc_test(
2412 "replace_try_expr_with_match",
2413 r#####"
2414 //- minicore: try, option
2415 fn handle() {
2416 let pat = Some(true)$0?;
2417 }
2418 "#####,
2419 r#####"
2420 fn handle() {
2421 let pat = match Some(true) {
2422 Some(it) => it,
2423 None => return None,
2424 };
2425 }
2426 "#####,
2427 )
2428 }
2429
2430 #[test]
doctest_replace_turbofish_with_explicit_type()2431 fn doctest_replace_turbofish_with_explicit_type() {
2432 check_doc_test(
2433 "replace_turbofish_with_explicit_type",
2434 r#####"
2435 fn make<T>() -> T { ) }
2436 fn main() {
2437 let a = make$0::<i32>();
2438 }
2439 "#####,
2440 r#####"
2441 fn make<T>() -> T { ) }
2442 fn main() {
2443 let a: i32 = make();
2444 }
2445 "#####,
2446 )
2447 }
2448
2449 #[test]
doctest_replace_with_eager_method()2450 fn doctest_replace_with_eager_method() {
2451 check_doc_test(
2452 "replace_with_eager_method",
2453 r#####"
2454 //- minicore:option, fn
2455 fn foo() {
2456 let a = Some(1);
2457 a.unwra$0p_or_else(|| 2);
2458 }
2459 "#####,
2460 r#####"
2461 fn foo() {
2462 let a = Some(1);
2463 a.unwrap_or(2);
2464 }
2465 "#####,
2466 )
2467 }
2468
2469 #[test]
doctest_replace_with_lazy_method()2470 fn doctest_replace_with_lazy_method() {
2471 check_doc_test(
2472 "replace_with_lazy_method",
2473 r#####"
2474 //- minicore:option, fn
2475 fn foo() {
2476 let a = Some(1);
2477 a.unwra$0p_or(2);
2478 }
2479 "#####,
2480 r#####"
2481 fn foo() {
2482 let a = Some(1);
2483 a.unwrap_or_else(|| 2);
2484 }
2485 "#####,
2486 )
2487 }
2488
2489 #[test]
doctest_sort_items()2490 fn doctest_sort_items() {
2491 check_doc_test(
2492 "sort_items",
2493 r#####"
2494 struct $0Foo$0 { second: u32, first: String }
2495 "#####,
2496 r#####"
2497 struct Foo { first: String, second: u32 }
2498 "#####,
2499 )
2500 }
2501
2502 #[test]
doctest_sort_items_1()2503 fn doctest_sort_items_1() {
2504 check_doc_test(
2505 "sort_items",
2506 r#####"
2507 trait $0Bar$0 {
2508 fn second(&self) -> u32;
2509 fn first(&self) -> String;
2510 }
2511 "#####,
2512 r#####"
2513 trait Bar {
2514 fn first(&self) -> String;
2515 fn second(&self) -> u32;
2516 }
2517 "#####,
2518 )
2519 }
2520
2521 #[test]
doctest_sort_items_2()2522 fn doctest_sort_items_2() {
2523 check_doc_test(
2524 "sort_items",
2525 r#####"
2526 struct Baz;
2527 impl $0Baz$0 {
2528 fn second(&self) -> u32;
2529 fn first(&self) -> String;
2530 }
2531 "#####,
2532 r#####"
2533 struct Baz;
2534 impl Baz {
2535 fn first(&self) -> String;
2536 fn second(&self) -> u32;
2537 }
2538 "#####,
2539 )
2540 }
2541
2542 #[test]
doctest_sort_items_3()2543 fn doctest_sort_items_3() {
2544 check_doc_test(
2545 "sort_items",
2546 r#####"
2547 enum $0Animal$0 {
2548 Dog(String, f64),
2549 Cat { weight: f64, name: String },
2550 }
2551 "#####,
2552 r#####"
2553 enum Animal {
2554 Cat { weight: f64, name: String },
2555 Dog(String, f64),
2556 }
2557 "#####,
2558 )
2559 }
2560
2561 #[test]
doctest_sort_items_4()2562 fn doctest_sort_items_4() {
2563 check_doc_test(
2564 "sort_items",
2565 r#####"
2566 enum Animal {
2567 Dog(String, f64),
2568 Cat $0{ weight: f64, name: String }$0,
2569 }
2570 "#####,
2571 r#####"
2572 enum Animal {
2573 Dog(String, f64),
2574 Cat { name: String, weight: f64 },
2575 }
2576 "#####,
2577 )
2578 }
2579
2580 #[test]
doctest_split_import()2581 fn doctest_split_import() {
2582 check_doc_test(
2583 "split_import",
2584 r#####"
2585 use std::$0collections::HashMap;
2586 "#####,
2587 r#####"
2588 use std::{collections::HashMap};
2589 "#####,
2590 )
2591 }
2592
2593 #[test]
doctest_toggle_ignore()2594 fn doctest_toggle_ignore() {
2595 check_doc_test(
2596 "toggle_ignore",
2597 r#####"
2598 $0#[test]
2599 fn arithmetics {
2600 assert_eq!(2 + 2, 5);
2601 }
2602 "#####,
2603 r#####"
2604 #[test]
2605 #[ignore]
2606 fn arithmetics {
2607 assert_eq!(2 + 2, 5);
2608 }
2609 "#####,
2610 )
2611 }
2612
2613 #[test]
doctest_unmerge_match_arm()2614 fn doctest_unmerge_match_arm() {
2615 check_doc_test(
2616 "unmerge_match_arm",
2617 r#####"
2618 enum Action { Move { distance: u32 }, Stop }
2619
2620 fn handle(action: Action) {
2621 match action {
2622 Action::Move(..) $0| Action::Stop => foo(),
2623 }
2624 }
2625 "#####,
2626 r#####"
2627 enum Action { Move { distance: u32 }, Stop }
2628
2629 fn handle(action: Action) {
2630 match action {
2631 Action::Move(..) => foo(),
2632 Action::Stop => foo(),
2633 }
2634 }
2635 "#####,
2636 )
2637 }
2638
2639 #[test]
doctest_unmerge_use()2640 fn doctest_unmerge_use() {
2641 check_doc_test(
2642 "unmerge_use",
2643 r#####"
2644 use std::fmt::{Debug, Display$0};
2645 "#####,
2646 r#####"
2647 use std::fmt::{Debug};
2648 use std::fmt::Display;
2649 "#####,
2650 )
2651 }
2652
2653 #[test]
doctest_unnecessary_async()2654 fn doctest_unnecessary_async() {
2655 check_doc_test(
2656 "unnecessary_async",
2657 r#####"
2658 pub async f$0n foo() {}
2659 pub async fn bar() { foo().await }
2660 "#####,
2661 r#####"
2662 pub fn foo() {}
2663 pub async fn bar() { foo() }
2664 "#####,
2665 )
2666 }
2667
2668 #[test]
doctest_unqualify_method_call()2669 fn doctest_unqualify_method_call() {
2670 check_doc_test(
2671 "unqualify_method_call",
2672 r#####"
2673 fn main() {
2674 std::ops::Add::add$0(1, 2);
2675 }
2676 mod std { pub mod ops { pub trait Add { fn add(self, _: Self) {} } impl Add for i32 {} } }
2677 "#####,
2678 r#####"
2679 fn main() {
2680 1.add(2);
2681 }
2682 mod std { pub mod ops { pub trait Add { fn add(self, _: Self) {} } impl Add for i32 {} } }
2683 "#####,
2684 )
2685 }
2686
2687 #[test]
doctest_unwrap_block()2688 fn doctest_unwrap_block() {
2689 check_doc_test(
2690 "unwrap_block",
2691 r#####"
2692 fn foo() {
2693 if true {$0
2694 println!("foo");
2695 }
2696 }
2697 "#####,
2698 r#####"
2699 fn foo() {
2700 println!("foo");
2701 }
2702 "#####,
2703 )
2704 }
2705
2706 #[test]
doctest_unwrap_result_return_type()2707 fn doctest_unwrap_result_return_type() {
2708 check_doc_test(
2709 "unwrap_result_return_type",
2710 r#####"
2711 //- minicore: result
2712 fn foo() -> Result<i32>$0 { Ok(42i32) }
2713 "#####,
2714 r#####"
2715 fn foo() -> i32 { 42i32 }
2716 "#####,
2717 )
2718 }
2719
2720 #[test]
doctest_unwrap_tuple()2721 fn doctest_unwrap_tuple() {
2722 check_doc_test(
2723 "unwrap_tuple",
2724 r#####"
2725 //- minicore: result
2726 fn main() {
2727 $0let (foo, bar) = ("Foo", "Bar");
2728 }
2729 "#####,
2730 r#####"
2731 fn main() {
2732 let foo = "Foo";
2733 let bar = "Bar";
2734 }
2735 "#####,
2736 )
2737 }
2738
2739 #[test]
doctest_wrap_return_type_in_result()2740 fn doctest_wrap_return_type_in_result() {
2741 check_doc_test(
2742 "wrap_return_type_in_result",
2743 r#####"
2744 //- minicore: result
2745 fn foo() -> i32$0 { 42i32 }
2746 "#####,
2747 r#####"
2748 fn foo() -> Result<i32, ${0:_}> { Ok(42i32) }
2749 "#####,
2750 )
2751 }
2752