]> git.proxmox.com Git - rustc.git/blame - src/tools/rust-analyzer/crates/ide-completion/src/tests/attribute.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / src / tools / rust-analyzer / crates / ide-completion / src / tests / attribute.rs
CommitLineData
064997fb
FG
1//! Completion tests for attributes.
2use expect_test::{expect, Expect};
3
4use crate::tests::{check_edit, completion_list};
5
6fn check(ra_fixture: &str, expect: Expect) {
7 let actual = completion_list(ra_fixture);
8 expect.assert_eq(&actual);
9}
10
11#[test]
12fn proc_macros() {
13 check(
14 r#"
15//- proc_macros: identity
16#[$0]
17struct Foo;
18"#,
19 expect![[r#"
20 at allow(…)
21 at cfg(…)
22 at cfg_attr(…)
23 at deny(…)
24 at deprecated
25 at derive(…)
26 at doc = "…"
27 at doc(alias = "…")
28 at doc(hidden)
29 at forbid(…)
30 at must_use
31 at no_mangle
32 at non_exhaustive
33 at repr(…)
34 at warn(…)
35 md proc_macros
36 kw crate::
37 kw self::
38 "#]],
39 )
40}
41
42#[test]
43fn proc_macros_on_comment() {
44 check(
45 r#"
46//- proc_macros: identity
47/// $0
48#[proc_macros::identity]
49struct Foo;
50"#,
51 expect![[r#""#]],
52 )
53}
54
55#[test]
56fn proc_macros_qualified() {
57 check(
58 r#"
59//- proc_macros: identity
60#[proc_macros::$0]
61struct Foo;
62"#,
63 expect![[r#"
64 at identity proc_macro identity
65 "#]],
66 )
67}
68
69#[test]
70fn inside_nested_attr() {
71 check(r#"#[cfg($0)]"#, expect![[]])
72}
73
74#[test]
75fn with_existing_attr() {
76 check(
77 r#"#[no_mangle] #[$0] mcall!();"#,
78 expect![[r#"
79 at allow(…)
80 at cfg(…)
81 at cfg_attr(…)
82 at deny(…)
83 at forbid(…)
84 at warn(…)
85 kw crate::
86 kw self::
87 "#]],
88 )
89}
90
91#[test]
92fn attr_on_source_file() {
93 check(
94 r#"#![$0]"#,
95 expect![[r#"
96 at allow(…)
97 at cfg(…)
98 at cfg_attr(…)
99 at crate_name = ""
100 at deny(…)
101 at deprecated
102 at doc = "…"
103 at doc(alias = "…")
104 at doc(hidden)
105 at feature(…)
106 at forbid(…)
107 at must_use
108 at no_implicit_prelude
109 at no_main
110 at no_mangle
111 at no_std
112 at recursion_limit = "…"
113 at type_length_limit = …
114 at warn(…)
115 at windows_subsystem = "…"
116 kw crate::
117 kw self::
118 "#]],
119 );
120}
121
122#[test]
123fn attr_on_module() {
124 check(
125 r#"#[$0] mod foo;"#,
126 expect![[r#"
127 at allow(…)
128 at cfg(…)
129 at cfg_attr(…)
130 at deny(…)
131 at deprecated
132 at doc = "…"
133 at doc(alias = "…")
134 at doc(hidden)
135 at forbid(…)
136 at macro_use
137 at must_use
138 at no_mangle
139 at path = "…"
140 at warn(…)
141 kw crate::
142 kw self::
143 kw super::
144 "#]],
145 );
146 check(
147 r#"mod foo {#![$0]}"#,
148 expect![[r#"
149 at allow(…)
150 at cfg(…)
151 at cfg_attr(…)
152 at deny(…)
153 at deprecated
154 at doc = "…"
155 at doc(alias = "…")
156 at doc(hidden)
157 at forbid(…)
158 at must_use
159 at no_implicit_prelude
160 at no_mangle
161 at warn(…)
162 kw crate::
163 kw self::
164 kw super::
165 "#]],
166 );
167}
168
169#[test]
170fn attr_on_macro_rules() {
171 check(
172 r#"#[$0] macro_rules! foo {}"#,
173 expect![[r#"
174 at allow(…)
175 at cfg(…)
176 at cfg_attr(…)
177 at deny(…)
178 at deprecated
179 at doc = "…"
180 at doc(alias = "…")
181 at doc(hidden)
182 at forbid(…)
183 at macro_export
184 at macro_use
185 at must_use
186 at no_mangle
187 at warn(…)
188 kw crate::
189 kw self::
190 "#]],
191 );
192}
193
194#[test]
195fn attr_on_macro_def() {
196 check(
197 r#"#[$0] macro foo {}"#,
198 expect![[r#"
199 at allow(…)
200 at cfg(…)
201 at cfg_attr(…)
202 at deny(…)
203 at deprecated
204 at doc = "…"
205 at doc(alias = "…")
206 at doc(hidden)
207 at forbid(…)
208 at must_use
209 at no_mangle
210 at warn(…)
211 kw crate::
212 kw self::
213 "#]],
214 );
215}
216
217#[test]
218fn attr_on_extern_crate() {
219 check(
220 r#"#[$0] extern crate foo;"#,
221 expect![[r#"
222 at allow(…)
223 at cfg(…)
224 at cfg_attr(…)
225 at deny(…)
226 at deprecated
227 at doc = "…"
228 at doc(alias = "…")
229 at doc(hidden)
230 at forbid(…)
231 at macro_use
232 at must_use
233 at no_mangle
234 at warn(…)
235 kw crate::
236 kw self::
237 "#]],
238 );
239}
240
241#[test]
242fn attr_on_use() {
243 check(
244 r#"#[$0] use foo;"#,
245 expect![[r#"
246 at allow(…)
247 at cfg(…)
248 at cfg_attr(…)
249 at deny(…)
250 at deprecated
251 at doc = "…"
252 at doc(alias = "…")
253 at doc(hidden)
254 at forbid(…)
255 at must_use
256 at no_mangle
257 at warn(…)
258 kw crate::
259 kw self::
260 "#]],
261 );
262}
263
264#[test]
265fn attr_on_type_alias() {
266 check(
267 r#"#[$0] type foo = ();"#,
268 expect![[r#"
269 at allow(…)
270 at cfg(…)
271 at cfg_attr(…)
272 at deny(…)
273 at deprecated
274 at doc = "…"
275 at doc(alias = "…")
276 at doc(hidden)
277 at forbid(…)
278 at must_use
279 at no_mangle
280 at warn(…)
281 kw crate::
282 kw self::
283 "#]],
284 );
285}
286
287#[test]
288fn attr_on_struct() {
289 check(
290 r#"
291//- minicore:derive
292#[$0]
293struct Foo;
294"#,
295 expect![[r#"
296 at allow(…)
297 at cfg(…)
298 at cfg_attr(…)
299 at deny(…)
300 at deprecated
301 at derive macro derive
302 at derive(…)
303 at doc = "…"
304 at doc(alias = "…")
305 at doc(hidden)
306 at forbid(…)
307 at must_use
308 at no_mangle
309 at non_exhaustive
310 at repr(…)
311 at warn(…)
312 md core
313 kw crate::
314 kw self::
315 "#]],
316 );
317}
318
319#[test]
320fn attr_on_enum() {
321 check(
322 r#"#[$0] enum Foo {}"#,
323 expect![[r#"
324 at allow(…)
325 at cfg(…)
326 at cfg_attr(…)
327 at deny(…)
328 at deprecated
329 at derive(…)
330 at doc = "…"
331 at doc(alias = "…")
332 at doc(hidden)
333 at forbid(…)
334 at must_use
335 at no_mangle
336 at non_exhaustive
337 at repr(…)
338 at warn(…)
339 kw crate::
340 kw self::
341 "#]],
342 );
343}
344
345#[test]
346fn attr_on_const() {
347 check(
348 r#"#[$0] const FOO: () = ();"#,
349 expect![[r#"
350 at allow(…)
351 at cfg(…)
352 at cfg_attr(…)
353 at deny(…)
354 at deprecated
355 at doc = "…"
356 at doc(alias = "…")
357 at doc(hidden)
358 at forbid(…)
359 at must_use
360 at no_mangle
361 at warn(…)
362 kw crate::
363 kw self::
364 "#]],
365 );
366}
367
368#[test]
369fn attr_on_static() {
370 check(
371 r#"#[$0] static FOO: () = ()"#,
372 expect![[r#"
373 at allow(…)
374 at cfg(…)
375 at cfg_attr(…)
376 at deny(…)
377 at deprecated
378 at doc = "…"
379 at doc(alias = "…")
380 at doc(hidden)
381 at export_name = "…"
382 at forbid(…)
383 at global_allocator
384 at link_name = "…"
385 at link_section = "…"
386 at must_use
387 at no_mangle
388 at used
389 at warn(…)
390 kw crate::
391 kw self::
392 "#]],
393 );
394}
395
396#[test]
397fn attr_on_trait() {
398 check(
399 r#"#[$0] trait Foo {}"#,
400 expect![[r#"
401 at allow(…)
402 at cfg(…)
403 at cfg_attr(…)
404 at deny(…)
405 at deprecated
406 at doc = "…"
407 at doc(alias = "…")
408 at doc(hidden)
409 at forbid(…)
410 at must_use
411 at must_use
412 at no_mangle
413 at warn(…)
414 kw crate::
415 kw self::
416 "#]],
417 );
418}
419
420#[test]
421fn attr_on_impl() {
422 check(
423 r#"#[$0] impl () {}"#,
424 expect![[r#"
425 at allow(…)
426 at automatically_derived
427 at cfg(…)
428 at cfg_attr(…)
429 at deny(…)
430 at deprecated
431 at doc = "…"
432 at doc(alias = "…")
433 at doc(hidden)
434 at forbid(…)
435 at must_use
436 at no_mangle
437 at warn(…)
438 kw crate::
439 kw self::
440 "#]],
441 );
442 check(
443 r#"impl () {#![$0]}"#,
444 expect![[r#"
445 at allow(…)
446 at cfg(…)
447 at cfg_attr(…)
448 at deny(…)
449 at deprecated
450 at doc = "…"
451 at doc(alias = "…")
452 at doc(hidden)
453 at forbid(…)
454 at must_use
455 at no_mangle
456 at warn(…)
457 kw crate::
458 kw self::
459 "#]],
460 );
461}
462
463#[test]
464fn attr_on_extern_block() {
465 check(
466 r#"#[$0] extern {}"#,
467 expect![[r#"
468 at allow(…)
469 at cfg(…)
470 at cfg_attr(…)
471 at deny(…)
472 at deprecated
473 at doc = "…"
474 at doc(alias = "…")
475 at doc(hidden)
476 at forbid(…)
477 at link
478 at must_use
479 at no_mangle
480 at warn(…)
481 kw crate::
482 kw self::
483 "#]],
484 );
485 check(
486 r#"extern {#![$0]}"#,
487 expect![[r#"
488 at allow(…)
489 at cfg(…)
490 at cfg_attr(…)
491 at deny(…)
492 at deprecated
493 at doc = "…"
494 at doc(alias = "…")
495 at doc(hidden)
496 at forbid(…)
497 at link
498 at must_use
499 at no_mangle
500 at warn(…)
501 kw crate::
502 kw self::
503 "#]],
504 );
505}
506
507#[test]
508fn attr_on_variant() {
509 check(
510 r#"enum Foo { #[$0] Bar }"#,
511 expect![[r#"
512 at allow(…)
513 at cfg(…)
514 at cfg_attr(…)
515 at deny(…)
516 at forbid(…)
517 at non_exhaustive
518 at warn(…)
519 kw crate::
520 kw self::
521 "#]],
522 );
523}
524
525#[test]
526fn attr_on_fn() {
527 check(
528 r#"#[$0] fn main() {}"#,
529 expect![[r#"
530 at allow(…)
531 at cfg(…)
532 at cfg_attr(…)
533 at cold
534 at deny(…)
535 at deprecated
536 at doc = "…"
537 at doc(alias = "…")
538 at doc(hidden)
539 at export_name = "…"
540 at forbid(…)
541 at ignore = "…"
542 at inline
543 at link_name = "…"
544 at link_section = "…"
545 at must_use
546 at must_use
547 at no_mangle
548 at panic_handler
549 at proc_macro
550 at proc_macro_attribute
551 at proc_macro_derive(…)
552 at should_panic
553 at target_feature(enable = "…")
554 at test
555 at track_caller
556 at warn(…)
557 kw crate::
558 kw self::
559 "#]],
560 );
561}
562
563#[test]
564fn attr_in_source_file_end() {
565 check(
566 r#"#[$0]"#,
567 expect![[r#"
568 at allow(…)
569 at automatically_derived
570 at cfg(…)
571 at cfg_attr(…)
572 at cold
573 at deny(…)
574 at deprecated
575 at derive(…)
576 at doc = "…"
577 at doc(alias = "…")
578 at doc(hidden)
579 at export_name = "…"
580 at forbid(…)
581 at global_allocator
582 at ignore = "…"
583 at inline
584 at link
585 at link_name = "…"
586 at link_section = "…"
587 at macro_export
588 at macro_use
589 at must_use
590 at no_mangle
591 at non_exhaustive
592 at panic_handler
593 at path = "…"
594 at proc_macro
595 at proc_macro_attribute
596 at proc_macro_derive(…)
597 at repr(…)
598 at should_panic
599 at target_feature(enable = "…")
600 at test
601 at track_caller
602 at used
603 at warn(…)
604 kw crate::
605 kw self::
606 "#]],
607 );
608}
609
f25598a0
FG
610#[test]
611fn invalid_path() {
612 check(
613 r#"
614//- proc_macros: identity
615#[proc_macros:::$0]
616struct Foo;
617"#,
618 expect![[r#""#]],
619 );
620
621 check(
622 r#"
623//- minicore: derive, copy
624mod foo {
625 pub use Copy as Bar;
626}
627#[derive(foo:::::$0)]
628struct Foo;
629"#,
630 expect![""],
631 );
632}
633
064997fb
FG
634mod cfg {
635 use super::*;
636
637 #[test]
638 fn cfg_target_endian() {
639 check(
640 r#"#[cfg(target_endian = $0"#,
641 expect![[r#"
642 ba big
643 ba little
644 "#]],
645 );
646 }
647}
648
649mod derive {
650 use super::*;
651
652 fn check_derive(ra_fixture: &str, expect: Expect) {
653 let actual = completion_list(ra_fixture);
654 expect.assert_eq(&actual);
655 }
656
657 #[test]
658 fn no_completion_for_incorrect_derive() {
659 check_derive(
660 r#"
661//- minicore: derive, copy, clone, ord, eq, default, fmt
662#[derive{$0)] struct Test;
663"#,
664 expect![[]],
665 )
666 }
667
668 #[test]
669 fn empty_derive() {
670 check_derive(
671 r#"
672//- minicore: derive, copy, clone, ord, eq, default, fmt
673#[derive($0)] struct Test;
674"#,
675 expect![[r#"
676 de Clone macro Clone
677 de Clone, Copy
678 de Default macro Default
679 de PartialEq macro PartialEq
680 de PartialEq, Eq
681 de PartialEq, Eq, PartialOrd, Ord
682 de PartialEq, PartialOrd
683 md core
684 kw crate::
685 kw self::
686 "#]],
687 );
688 }
689
690 #[test]
691 fn derive_with_input_before() {
692 check_derive(
693 r#"
694//- minicore: derive, copy, clone, ord, eq, default, fmt
695#[derive(serde::Serialize, PartialEq, $0)] struct Test;
696"#,
697 expect![[r#"
698 de Clone macro Clone
699 de Clone, Copy
700 de Default macro Default
701 de Eq
702 de Eq, PartialOrd, Ord
703 de PartialOrd
704 md core
705 kw crate::
706 kw self::
707 "#]],
708 )
709 }
710
711 #[test]
712 fn derive_with_input_after() {
713 check_derive(
714 r#"
715//- minicore: derive, copy, clone, ord, eq, default, fmt
716#[derive($0 serde::Serialize, PartialEq)] struct Test;
717"#,
718 expect![[r#"
719 de Clone macro Clone
720 de Clone, Copy
721 de Default macro Default
722 de Eq
723 de Eq, PartialOrd, Ord
724 de PartialOrd
725 md core
726 kw crate::
727 kw self::
728 "#]],
729 );
730 }
731
732 #[test]
733 fn derive_with_existing_derives() {
734 check_derive(
735 r#"
736//- minicore: derive, copy, clone, ord, eq, default, fmt
737#[derive(PartialEq, Eq, Or$0)] struct Test;
738"#,
739 expect![[r#"
740 de Clone macro Clone
741 de Clone, Copy
742 de Default macro Default
743 de PartialOrd
744 de PartialOrd, Ord
745 md core
746 kw crate::
747 kw self::
748 "#]],
749 );
750 }
751
752 #[test]
753 fn derive_flyimport() {
754 check_derive(
755 r#"
756//- proc_macros: derive_identity
757//- minicore: derive
758#[derive(der$0)] struct Test;
759"#,
760 expect![[r#"
761 de DeriveIdentity (use proc_macros::DeriveIdentity) proc_macro DeriveIdentity
762 md core
763 md proc_macros
764 kw crate::
765 kw self::
766 "#]],
767 );
768 check_derive(
769 r#"
770//- proc_macros: derive_identity
771//- minicore: derive
772use proc_macros::DeriveIdentity;
773#[derive(der$0)] struct Test;
774"#,
775 expect![[r#"
776 de DeriveIdentity proc_macro DeriveIdentity
777 md core
778 md proc_macros
779 kw crate::
780 kw self::
781 "#]],
782 );
783 }
784
785 #[test]
786 fn derive_flyimport_edit() {
787 check_edit(
788 "DeriveIdentity",
789 r#"
790//- proc_macros: derive_identity
791//- minicore: derive
792#[derive(der$0)] struct Test;
793"#,
794 r#"
795use proc_macros::DeriveIdentity;
796
797#[derive(DeriveIdentity)] struct Test;
798"#,
799 );
800 }
801
802 #[test]
803 fn qualified() {
804 check_derive(
805 r#"
806//- proc_macros: derive_identity
807//- minicore: derive, copy, clone
808#[derive(proc_macros::$0)] struct Test;
809"#,
810 expect![[r#"
811 de DeriveIdentity proc_macro DeriveIdentity
812 "#]],
813 );
814 check_derive(
815 r#"
816//- proc_macros: derive_identity
817//- minicore: derive, copy, clone
818#[derive(proc_macros::C$0)] struct Test;
819"#,
820 expect![[r#"
821 de DeriveIdentity proc_macro DeriveIdentity
822 "#]],
823 );
824 }
825}
826
827mod lint {
828 use super::*;
829
830 #[test]
831 fn lint_empty() {
832 check_edit(
833 "deprecated",
834 r#"#[allow($0)] struct Test;"#,
835 r#"#[allow(deprecated)] struct Test;"#,
836 )
837 }
838
839 #[test]
840 fn lint_with_existing() {
841 check_edit(
842 "deprecated",
843 r#"#[allow(keyword_idents, $0)] struct Test;"#,
844 r#"#[allow(keyword_idents, deprecated)] struct Test;"#,
845 )
846 }
847
848 #[test]
849 fn lint_qualified() {
850 check_edit(
851 "deprecated",
852 r#"#[allow(keyword_idents, $0)] struct Test;"#,
853 r#"#[allow(keyword_idents, deprecated)] struct Test;"#,
854 )
855 }
856
857 #[test]
858 fn lint_feature() {
859 check_edit(
860 "box_syntax",
861 r#"#[feature(box_$0)] struct Test;"#,
862 r#"#[feature(box_syntax)] struct Test;"#,
863 )
864 }
865
866 #[test]
867 fn lint_clippy_unqualified() {
868 check_edit(
869 "clippy::as_conversions",
870 r#"#[allow($0)] struct Test;"#,
871 r#"#[allow(clippy::as_conversions)] struct Test;"#,
872 );
873 }
874
875 #[test]
876 fn lint_clippy_qualified() {
877 check_edit(
878 "as_conversions",
879 r#"#[allow(clippy::$0)] struct Test;"#,
880 r#"#[allow(clippy::as_conversions)] struct Test;"#,
881 );
882 }
883
884 #[test]
885 fn lint_rustdoc_unqualified() {
886 check_edit(
887 "rustdoc::bare_urls",
888 r#"#[allow($0)] struct Test;"#,
889 r#"#[allow(rustdoc::bare_urls)] struct Test;"#,
890 );
891 }
892
893 #[test]
894 fn lint_rustdoc_qualified() {
895 check_edit(
896 "bare_urls",
897 r#"#[allow(rustdoc::$0)] struct Test;"#,
898 r#"#[allow(rustdoc::bare_urls)] struct Test;"#,
899 );
900 }
901
902 #[test]
903 fn lint_unclosed() {
904 check_edit(
905 "deprecated",
906 r#"#[allow(dep$0 struct Test;"#,
907 r#"#[allow(deprecated struct Test;"#,
908 );
909 check_edit(
910 "bare_urls",
911 r#"#[allow(rustdoc::$0 struct Test;"#,
912 r#"#[allow(rustdoc::bare_urls struct Test;"#,
913 );
914 }
915}
916
917mod repr {
918 use super::*;
919
920 fn check_repr(ra_fixture: &str, expect: Expect) {
921 let actual = completion_list(ra_fixture);
922 expect.assert_eq(&actual);
923 }
924
925 #[test]
926 fn no_completion_for_incorrect_repr() {
927 check_repr(r#"#[repr{$0)] struct Test;"#, expect![[]])
928 }
929
930 #[test]
931 fn empty() {
932 check_repr(
933 r#"#[repr($0)] struct Test;"#,
934 expect![[r#"
935 ba C
936 ba align($0)
937 ba i16
938 ba i28
939 ba i32
940 ba i64
941 ba i8
942 ba isize
943 ba packed
944 ba transparent
945 ba u128
946 ba u16
947 ba u32
948 ba u64
949 ba u8
950 ba usize
951 "#]],
952 );
953 }
954
955 #[test]
956 fn transparent() {
957 check_repr(r#"#[repr(transparent, $0)] struct Test;"#, expect![[r#""#]]);
958 }
959
960 #[test]
961 fn align() {
962 check_repr(
963 r#"#[repr(align(1), $0)] struct Test;"#,
964 expect![[r#"
965 ba C
966 ba i16
967 ba i28
968 ba i32
969 ba i64
970 ba i8
971 ba isize
972 ba transparent
973 ba u128
974 ba u16
975 ba u32
976 ba u64
977 ba u8
978 ba usize
979 "#]],
980 );
981 }
982
983 #[test]
984 fn packed() {
985 check_repr(
986 r#"#[repr(packed, $0)] struct Test;"#,
987 expect![[r#"
988 ba C
989 ba i16
990 ba i28
991 ba i32
992 ba i64
993 ba i8
994 ba isize
995 ba transparent
996 ba u128
997 ba u16
998 ba u32
999 ba u64
1000 ba u8
1001 ba usize
1002 "#]],
1003 );
1004 }
1005
1006 #[test]
1007 fn c() {
1008 check_repr(
1009 r#"#[repr(C, $0)] struct Test;"#,
1010 expect![[r#"
1011 ba align($0)
1012 ba i16
1013 ba i28
1014 ba i32
1015 ba i64
1016 ba i8
1017 ba isize
1018 ba packed
1019 ba u128
1020 ba u16
1021 ba u32
1022 ba u64
1023 ba u8
1024 ba usize
1025 "#]],
1026 );
1027 }
1028
1029 #[test]
1030 fn prim() {
1031 check_repr(
1032 r#"#[repr(usize, $0)] struct Test;"#,
1033 expect![[r#"
1034 ba C
1035 ba align($0)
1036 ba packed
1037 "#]],
1038 );
1039 }
1040}