]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/attribute.rs
Complete `repr` attribute parameters
[rust.git] / crates / ide_completion / src / completions / attribute.rs
1 //! Completion for attributes
2 //!
3 //! This module uses a bit of static metadata to provide completions
4 //! for built-in attributes.
5
6 use hir::HasAttrs;
7 use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES};
8 use once_cell::sync::Lazy;
9 use rustc_hash::{FxHashMap, FxHashSet};
10 use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, NodeOrToken, SyntaxKind, T};
11
12 use crate::{
13     context::CompletionContext,
14     item::{CompletionItem, CompletionItemKind, CompletionKind},
15     Completions,
16 };
17
18 mod derive;
19 mod lint;
20 mod repr;
21
22 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
23     let attribute = ctx.attribute_under_caret.as_ref()?;
24     match (attribute.path().and_then(|p| p.as_single_name_ref()), attribute.token_tree()) {
25         (Some(path), Some(token_tree)) => match path.text().as_str() {
26             "derive" => derive::complete_derive(acc, ctx, token_tree),
27             "repr" => repr::complete_repr(acc, ctx, token_tree),
28             "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
29             "allow" | "warn" | "deny" | "forbid" => {
30                 lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
31                 lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
32             }
33             _ => (),
34         },
35         (None, Some(_)) => (),
36         _ => complete_new_attribute(acc, ctx, attribute),
37     }
38     Some(())
39 }
40
41 fn complete_new_attribute(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
42     let is_inner = attribute.kind() == ast::AttrKind::Inner;
43     let attribute_annotated_item_kind =
44         attribute.syntax().parent().map(|it| it.kind()).filter(|_| {
45             is_inner
46             // If we got nothing coming after the attribute it could be anything so filter it the kind out
47                 || non_trivia_sibling(attribute.syntax().clone().into(), Direction::Next).is_some()
48         });
49     let attributes = attribute_annotated_item_kind.and_then(|kind| {
50         if ast::Expr::can_cast(kind) {
51             Some(EXPR_ATTRIBUTES)
52         } else {
53             KIND_TO_ATTRIBUTES.get(&kind).copied()
54         }
55     });
56
57     let add_completion = |attr_completion: &AttrCompletion| {
58         let mut item = CompletionItem::new(
59             CompletionKind::Attribute,
60             ctx.source_range(),
61             attr_completion.label,
62         );
63         item.kind(CompletionItemKind::Attribute);
64
65         if let Some(lookup) = attr_completion.lookup {
66             item.lookup_by(lookup);
67         }
68
69         if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
70             item.insert_snippet(cap, snippet);
71         }
72
73         if is_inner || !attr_completion.prefer_inner {
74             item.add_to(acc);
75         }
76     };
77
78     match attributes {
79         Some(applicable) => applicable
80             .iter()
81             .flat_map(|name| ATTRIBUTES.binary_search_by(|attr| attr.key().cmp(name)).ok())
82             .flat_map(|idx| ATTRIBUTES.get(idx))
83             .for_each(add_completion),
84         None if is_inner => ATTRIBUTES.iter().for_each(add_completion),
85         None => ATTRIBUTES.iter().filter(|compl| !compl.prefer_inner).for_each(add_completion),
86     }
87
88     // FIXME: write a test for this when we can
89     ctx.scope.process_all_names(&mut |name, scope_def| {
90         if let hir::ScopeDef::MacroDef(mac) = scope_def {
91             if mac.kind() == hir::MacroKind::Attr {
92                 let mut item = CompletionItem::new(
93                     CompletionKind::Attribute,
94                     ctx.source_range(),
95                     name.to_string(),
96                 );
97                 item.kind(CompletionItemKind::Attribute);
98                 if let Some(docs) = mac.docs(ctx.sema.db) {
99                     item.documentation(docs);
100                 }
101                 item.add_to(acc);
102             }
103         }
104     });
105 }
106
107 struct AttrCompletion {
108     label: &'static str,
109     lookup: Option<&'static str>,
110     snippet: Option<&'static str>,
111     prefer_inner: bool,
112 }
113
114 impl AttrCompletion {
115     fn key(&self) -> &'static str {
116         self.lookup.unwrap_or(self.label)
117     }
118
119     const fn prefer_inner(self) -> AttrCompletion {
120         AttrCompletion { prefer_inner: true, ..self }
121     }
122 }
123
124 const fn attr(
125     label: &'static str,
126     lookup: Option<&'static str>,
127     snippet: Option<&'static str>,
128 ) -> AttrCompletion {
129     AttrCompletion { label, lookup, snippet, prefer_inner: false }
130 }
131
132 macro_rules! attrs {
133     // attributes applicable to all items
134     [@ { item $($tt:tt)* } {$($acc:tt)*}] => {
135         attrs!(@ { $($tt)* } { $($acc)*, "deprecated", "doc", "dochidden", "docalias", "must_use", "no_mangle" })
136     };
137     // attributes applicable to all adts
138     [@ { adt $($tt:tt)* } {$($acc:tt)*}] => {
139         attrs!(@ { $($tt)* } { $($acc)*, "derive", "repr" })
140     };
141     // attributes applicable to all linkable things aka functions/statics
142     [@ { linkable $($tt:tt)* } {$($acc:tt)*}] => {
143         attrs!(@ { $($tt)* } { $($acc)*, "export_name", "link_name", "link_section" })
144     };
145     // error fallback for nicer error message
146     [@ { $ty:ident $($tt:tt)* } {$($acc:tt)*}] => {
147         compile_error!(concat!("unknown attr subtype ", stringify!($ty)))
148     };
149     // general push down accumulation
150     [@ { $lit:literal $($tt:tt)*} {$($acc:tt)*}] => {
151         attrs!(@ { $($tt)* } { $($acc)*, $lit })
152     };
153     [@ {$($tt:tt)+} {$($tt2:tt)*}] => {
154         compile_error!(concat!("Unexpected input ", stringify!($($tt)+)))
155     };
156     // final output construction
157     [@ {} {$($tt:tt)*}] => { &[$($tt)*] as _ };
158     // starting matcher
159     [$($tt:tt),*] => {
160         attrs!(@ { $($tt)* } { "allow", "cfg", "cfg_attr", "deny", "forbid", "warn" })
161     };
162 }
163
164 #[rustfmt::skip]
165 static KIND_TO_ATTRIBUTES: Lazy<FxHashMap<SyntaxKind, &[&str]>> = Lazy::new(|| {
166     use SyntaxKind::*;
167     std::array::IntoIter::new([
168         (
169             SOURCE_FILE,
170             attrs!(
171                 item,
172                 "crate_name", "feature", "no_implicit_prelude", "no_main", "no_std",
173                 "recursion_limit", "type_length_limit", "windows_subsystem"
174             ),
175         ),
176         (MODULE, attrs!(item, "no_implicit_prelude", "path")),
177         (ITEM_LIST, attrs!(item, "no_implicit_prelude")),
178         (MACRO_RULES, attrs!(item, "macro_export", "macro_use")),
179         (MACRO_DEF, attrs!(item)),
180         (EXTERN_CRATE, attrs!(item, "macro_use", "no_link")),
181         (USE, attrs!(item)),
182         (TYPE_ALIAS, attrs!(item)),
183         (STRUCT, attrs!(item, adt, "non_exhaustive")),
184         (ENUM, attrs!(item, adt, "non_exhaustive")),
185         (UNION, attrs!(item, adt)),
186         (CONST, attrs!(item)),
187         (
188             FN,
189             attrs!(
190                 item, linkable,
191                 "cold", "ignore", "inline", "must_use", "panic_handler", "proc_macro",
192                 "proc_macro_derive", "proc_macro_attribute", "should_panic", "target_feature",
193                 "test", "track_caller"
194             ),
195         ),
196         (STATIC, attrs!(item, linkable, "global_allocator", "used")),
197         (TRAIT, attrs!(item, "must_use")),
198         (IMPL, attrs!(item, "automatically_derived")),
199         (ASSOC_ITEM_LIST, attrs!(item)),
200         (EXTERN_BLOCK, attrs!(item, "link")),
201         (EXTERN_ITEM_LIST, attrs!(item, "link")),
202         (MACRO_CALL, attrs!()),
203         (SELF_PARAM, attrs!()),
204         (PARAM, attrs!()),
205         (RECORD_FIELD, attrs!()),
206         (VARIANT, attrs!("non_exhaustive")),
207         (TYPE_PARAM, attrs!()),
208         (CONST_PARAM, attrs!()),
209         (LIFETIME_PARAM, attrs!()),
210         (LET_STMT, attrs!()),
211         (EXPR_STMT, attrs!()),
212         (LITERAL, attrs!()),
213         (RECORD_EXPR_FIELD_LIST, attrs!()),
214         (RECORD_EXPR_FIELD, attrs!()),
215         (MATCH_ARM_LIST, attrs!()),
216         (MATCH_ARM, attrs!()),
217         (IDENT_PAT, attrs!()),
218         (RECORD_PAT_FIELD, attrs!()),
219     ])
220     .collect()
221 });
222 const EXPR_ATTRIBUTES: &[&str] = attrs!();
223
224 /// <https://doc.rust-lang.org/reference/attributes.html#built-in-attributes-index>
225 // Keep these sorted for the binary search!
226 const ATTRIBUTES: &[AttrCompletion] = &[
227     attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
228     attr("automatically_derived", None, None),
229     attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
230     attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
231     attr("cold", None, None),
232     attr(r#"crate_name = """#, Some("crate_name"), Some(r#"crate_name = "${0:crate_name}""#))
233         .prefer_inner(),
234     attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
235     attr(r#"deprecated"#, Some("deprecated"), Some(r#"deprecated"#)),
236     attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
237     attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
238     attr(r#"doc(alias = "…")"#, Some("docalias"), Some(r#"doc(alias = "${0:docs}")"#)),
239     attr(r#"doc(hidden)"#, Some("dochidden"), Some(r#"doc(hidden)"#)),
240     attr(
241         r#"export_name = "…""#,
242         Some("export_name"),
243         Some(r#"export_name = "${0:exported_symbol_name}""#),
244     ),
245     attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
246     attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
247     attr("global_allocator", None, None),
248     attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
249     attr("inline", Some("inline"), Some("inline")),
250     attr("link", None, None),
251     attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
252     attr(
253         r#"link_section = "…""#,
254         Some("link_section"),
255         Some(r#"link_section = "${0:section_name}""#),
256     ),
257     attr("macro_export", None, None),
258     attr("macro_use", None, None),
259     attr(r#"must_use"#, Some("must_use"), Some(r#"must_use"#)),
260     attr("no_implicit_prelude", None, None).prefer_inner(),
261     attr("no_link", None, None).prefer_inner(),
262     attr("no_main", None, None).prefer_inner(),
263     attr("no_mangle", None, None),
264     attr("no_std", None, None).prefer_inner(),
265     attr("non_exhaustive", None, None),
266     attr("panic_handler", None, None),
267     attr(r#"path = "…""#, Some("path"), Some(r#"path ="${0:path}""#)),
268     attr("proc_macro", None, None),
269     attr("proc_macro_attribute", None, None),
270     attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
271     attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
272         .prefer_inner(),
273     attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
274     attr("should_panic", Some("should_panic"), Some(r#"should_panic"#)),
275     attr(
276         r#"target_feature = "…""#,
277         Some("target_feature"),
278         Some(r#"target_feature = "${0:feature}""#),
279     ),
280     attr("test", None, None),
281     attr("track_caller", None, None),
282     attr("type_length_limit = …", Some("type_length_limit"), Some("type_length_limit = ${0:128}"))
283         .prefer_inner(),
284     attr("used", None, None),
285     attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
286     attr(
287         r#"windows_subsystem = "…""#,
288         Some("windows_subsystem"),
289         Some(r#"windows_subsystem = "${0:subsystem}""#),
290     )
291     .prefer_inner(),
292 ];
293
294 fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Option<FxHashSet<String>> {
295     let (l_paren, r_paren) = derive_input.l_paren_token().zip(derive_input.r_paren_token())?;
296     let mut input_derives = FxHashSet::default();
297     let mut tokens = derive_input
298         .syntax()
299         .children_with_tokens()
300         .filter_map(NodeOrToken::into_token)
301         .skip_while(|token| token != &l_paren)
302         .skip(1)
303         .take_while(|token| token != &r_paren)
304         .peekable();
305     let mut input = String::new();
306     while tokens.peek().is_some() {
307         for token in tokens.by_ref().take_while(|t| t.kind() != T![,]) {
308             input.push_str(token.text());
309         }
310
311         if !input.is_empty() {
312             input_derives.insert(input.trim().to_owned());
313         }
314
315         input.clear();
316     }
317
318     Some(input_derives)
319 }
320
321 #[cfg(test)]
322 mod tests {
323     use super::*;
324
325     use expect_test::{expect, Expect};
326
327     use crate::tests::completion_list;
328
329     #[test]
330     fn attributes_are_sorted() {
331         let mut attrs = ATTRIBUTES.iter().map(|attr| attr.key());
332         let mut prev = attrs.next().unwrap();
333
334         attrs.for_each(|next| {
335             assert!(
336                 prev < next,
337                 r#"ATTRIBUTES array is not sorted, "{}" should come after "{}""#,
338                 prev,
339                 next
340             );
341             prev = next;
342         });
343     }
344
345     fn check(ra_fixture: &str, expect: Expect) {
346         let actual = completion_list(ra_fixture);
347         expect.assert_eq(&actual);
348     }
349
350     #[test]
351     fn test_attribute_completion_inside_nested_attr() {
352         check(r#"#[cfg($0)]"#, expect![[]])
353     }
354
355     #[test]
356     fn test_attribute_completion_with_existing_attr() {
357         check(
358             r#"#[no_mangle] #[$0] mcall!();"#,
359             expect![[r#"
360                 at allow(…)
361                 at cfg(…)
362                 at cfg_attr(…)
363                 at deny(…)
364                 at forbid(…)
365                 at warn(…)
366             "#]],
367         )
368     }
369
370     #[test]
371     fn complete_attribute_on_source_file() {
372         check(
373             r#"#![$0]"#,
374             expect![[r#"
375                 at allow(…)
376                 at cfg(…)
377                 at cfg_attr(…)
378                 at deny(…)
379                 at forbid(…)
380                 at warn(…)
381                 at deprecated
382                 at doc = "…"
383                 at doc(hidden)
384                 at doc(alias = "…")
385                 at must_use
386                 at no_mangle
387                 at crate_name = ""
388                 at feature(…)
389                 at no_implicit_prelude
390                 at no_main
391                 at no_std
392                 at recursion_limit = …
393                 at type_length_limit = …
394                 at windows_subsystem = "…"
395             "#]],
396         );
397     }
398
399     #[test]
400     fn complete_attribute_on_module() {
401         check(
402             r#"#[$0] mod foo;"#,
403             expect![[r#"
404             at allow(…)
405             at cfg(…)
406             at cfg_attr(…)
407             at deny(…)
408             at forbid(…)
409             at warn(…)
410             at deprecated
411             at doc = "…"
412             at doc(hidden)
413             at doc(alias = "…")
414             at must_use
415             at no_mangle
416             at path = "…"
417         "#]],
418         );
419         check(
420             r#"mod foo {#![$0]}"#,
421             expect![[r#"
422                 at allow(…)
423                 at cfg(…)
424                 at cfg_attr(…)
425                 at deny(…)
426                 at forbid(…)
427                 at warn(…)
428                 at deprecated
429                 at doc = "…"
430                 at doc(hidden)
431                 at doc(alias = "…")
432                 at must_use
433                 at no_mangle
434                 at no_implicit_prelude
435             "#]],
436         );
437     }
438
439     #[test]
440     fn complete_attribute_on_macro_rules() {
441         check(
442             r#"#[$0] macro_rules! foo {}"#,
443             expect![[r#"
444                 at allow(…)
445                 at cfg(…)
446                 at cfg_attr(…)
447                 at deny(…)
448                 at forbid(…)
449                 at warn(…)
450                 at deprecated
451                 at doc = "…"
452                 at doc(hidden)
453                 at doc(alias = "…")
454                 at must_use
455                 at no_mangle
456                 at macro_export
457                 at macro_use
458             "#]],
459         );
460     }
461
462     #[test]
463     fn complete_attribute_on_macro_def() {
464         check(
465             r#"#[$0] macro foo {}"#,
466             expect![[r#"
467                 at allow(…)
468                 at cfg(…)
469                 at cfg_attr(…)
470                 at deny(…)
471                 at forbid(…)
472                 at warn(…)
473                 at deprecated
474                 at doc = "…"
475                 at doc(hidden)
476                 at doc(alias = "…")
477                 at must_use
478                 at no_mangle
479             "#]],
480         );
481     }
482
483     #[test]
484     fn complete_attribute_on_extern_crate() {
485         check(
486             r#"#[$0] extern crate foo;"#,
487             expect![[r#"
488                 at allow(…)
489                 at cfg(…)
490                 at cfg_attr(…)
491                 at deny(…)
492                 at forbid(…)
493                 at warn(…)
494                 at deprecated
495                 at doc = "…"
496                 at doc(hidden)
497                 at doc(alias = "…")
498                 at must_use
499                 at no_mangle
500                 at macro_use
501             "#]],
502         );
503     }
504
505     #[test]
506     fn complete_attribute_on_use() {
507         check(
508             r#"#[$0] use foo;"#,
509             expect![[r#"
510                 at allow(…)
511                 at cfg(…)
512                 at cfg_attr(…)
513                 at deny(…)
514                 at forbid(…)
515                 at warn(…)
516                 at deprecated
517                 at doc = "…"
518                 at doc(hidden)
519                 at doc(alias = "…")
520                 at must_use
521                 at no_mangle
522             "#]],
523         );
524     }
525
526     #[test]
527     fn complete_attribute_on_type_alias() {
528         check(
529             r#"#[$0] type foo = ();"#,
530             expect![[r#"
531                 at allow(…)
532                 at cfg(…)
533                 at cfg_attr(…)
534                 at deny(…)
535                 at forbid(…)
536                 at warn(…)
537                 at deprecated
538                 at doc = "…"
539                 at doc(hidden)
540                 at doc(alias = "…")
541                 at must_use
542                 at no_mangle
543             "#]],
544         );
545     }
546
547     #[test]
548     fn complete_attribute_on_struct() {
549         check(
550             r#"#[$0] struct Foo;"#,
551             expect![[r#"
552                 at allow(…)
553                 at cfg(…)
554                 at cfg_attr(…)
555                 at deny(…)
556                 at forbid(…)
557                 at warn(…)
558                 at deprecated
559                 at doc = "…"
560                 at doc(hidden)
561                 at doc(alias = "…")
562                 at must_use
563                 at no_mangle
564                 at derive(…)
565                 at repr(…)
566                 at non_exhaustive
567             "#]],
568         );
569     }
570
571     #[test]
572     fn complete_attribute_on_enum() {
573         check(
574             r#"#[$0] enum Foo {}"#,
575             expect![[r#"
576                 at allow(…)
577                 at cfg(…)
578                 at cfg_attr(…)
579                 at deny(…)
580                 at forbid(…)
581                 at warn(…)
582                 at deprecated
583                 at doc = "…"
584                 at doc(hidden)
585                 at doc(alias = "…")
586                 at must_use
587                 at no_mangle
588                 at derive(…)
589                 at repr(…)
590                 at non_exhaustive
591             "#]],
592         );
593     }
594
595     #[test]
596     fn complete_attribute_on_const() {
597         check(
598             r#"#[$0] const FOO: () = ();"#,
599             expect![[r#"
600                 at allow(…)
601                 at cfg(…)
602                 at cfg_attr(…)
603                 at deny(…)
604                 at forbid(…)
605                 at warn(…)
606                 at deprecated
607                 at doc = "…"
608                 at doc(hidden)
609                 at doc(alias = "…")
610                 at must_use
611                 at no_mangle
612             "#]],
613         );
614     }
615
616     #[test]
617     fn complete_attribute_on_static() {
618         check(
619             r#"#[$0] static FOO: () = ()"#,
620             expect![[r#"
621                 at allow(…)
622                 at cfg(…)
623                 at cfg_attr(…)
624                 at deny(…)
625                 at forbid(…)
626                 at warn(…)
627                 at deprecated
628                 at doc = "…"
629                 at doc(hidden)
630                 at doc(alias = "…")
631                 at must_use
632                 at no_mangle
633                 at export_name = "…"
634                 at link_name = "…"
635                 at link_section = "…"
636                 at global_allocator
637                 at used
638             "#]],
639         );
640     }
641
642     #[test]
643     fn complete_attribute_on_trait() {
644         check(
645             r#"#[$0] trait Foo {}"#,
646             expect![[r#"
647                 at allow(…)
648                 at cfg(…)
649                 at cfg_attr(…)
650                 at deny(…)
651                 at forbid(…)
652                 at warn(…)
653                 at deprecated
654                 at doc = "…"
655                 at doc(hidden)
656                 at doc(alias = "…")
657                 at must_use
658                 at no_mangle
659                 at must_use
660             "#]],
661         );
662     }
663
664     #[test]
665     fn complete_attribute_on_impl() {
666         check(
667             r#"#[$0] impl () {}"#,
668             expect![[r#"
669                 at allow(…)
670                 at cfg(…)
671                 at cfg_attr(…)
672                 at deny(…)
673                 at forbid(…)
674                 at warn(…)
675                 at deprecated
676                 at doc = "…"
677                 at doc(hidden)
678                 at doc(alias = "…")
679                 at must_use
680                 at no_mangle
681                 at automatically_derived
682             "#]],
683         );
684         check(
685             r#"impl () {#![$0]}"#,
686             expect![[r#"
687                 at allow(…)
688                 at cfg(…)
689                 at cfg_attr(…)
690                 at deny(…)
691                 at forbid(…)
692                 at warn(…)
693                 at deprecated
694                 at doc = "…"
695                 at doc(hidden)
696                 at doc(alias = "…")
697                 at must_use
698                 at no_mangle
699             "#]],
700         );
701     }
702
703     #[test]
704     fn complete_attribute_on_extern_block() {
705         check(
706             r#"#[$0] extern {}"#,
707             expect![[r#"
708                 at allow(…)
709                 at cfg(…)
710                 at cfg_attr(…)
711                 at deny(…)
712                 at forbid(…)
713                 at warn(…)
714                 at deprecated
715                 at doc = "…"
716                 at doc(hidden)
717                 at doc(alias = "…")
718                 at must_use
719                 at no_mangle
720                 at link
721             "#]],
722         );
723         check(
724             r#"extern {#![$0]}"#,
725             expect![[r#"
726                 at allow(…)
727                 at cfg(…)
728                 at cfg_attr(…)
729                 at deny(…)
730                 at forbid(…)
731                 at warn(…)
732                 at deprecated
733                 at doc = "…"
734                 at doc(hidden)
735                 at doc(alias = "…")
736                 at must_use
737                 at no_mangle
738                 at link
739             "#]],
740         );
741     }
742
743     #[test]
744     fn complete_attribute_on_variant() {
745         check(
746             r#"enum Foo { #[$0] Bar }"#,
747             expect![[r#"
748                 at allow(…)
749                 at cfg(…)
750                 at cfg_attr(…)
751                 at deny(…)
752                 at forbid(…)
753                 at warn(…)
754                 at non_exhaustive
755             "#]],
756         );
757     }
758
759     #[test]
760     fn complete_attribute_on_fn() {
761         check(
762             r#"#[$0] fn main() {}"#,
763             expect![[r#"
764                 at allow(…)
765                 at cfg(…)
766                 at cfg_attr(…)
767                 at deny(…)
768                 at forbid(…)
769                 at warn(…)
770                 at deprecated
771                 at doc = "…"
772                 at doc(hidden)
773                 at doc(alias = "…")
774                 at must_use
775                 at no_mangle
776                 at export_name = "…"
777                 at link_name = "…"
778                 at link_section = "…"
779                 at cold
780                 at ignore = "…"
781                 at inline
782                 at must_use
783                 at panic_handler
784                 at proc_macro
785                 at proc_macro_derive(…)
786                 at proc_macro_attribute
787                 at should_panic
788                 at target_feature = "…"
789                 at test
790                 at track_caller
791                 kw return
792             "#]],
793         );
794     }
795
796     #[test]
797     fn complete_attribute_on_expr() {
798         check(
799             r#"fn main() { #[$0] foo() }"#,
800             expect![[r#"
801                 at allow(…)
802                 at cfg(…)
803                 at cfg_attr(…)
804                 at deny(…)
805                 at forbid(…)
806                 at warn(…)
807                 kw return
808             "#]],
809         );
810     }
811
812     #[test]
813     fn complete_attribute_in_source_file_end() {
814         check(
815             r#"#[$0]"#,
816             expect![[r#"
817                 at allow(…)
818                 at automatically_derived
819                 at cfg(…)
820                 at cfg_attr(…)
821                 at cold
822                 at deny(…)
823                 at deprecated
824                 at derive(…)
825                 at doc = "…"
826                 at doc(alias = "…")
827                 at doc(hidden)
828                 at export_name = "…"
829                 at forbid(…)
830                 at global_allocator
831                 at ignore = "…"
832                 at inline
833                 at link
834                 at link_name = "…"
835                 at link_section = "…"
836                 at macro_export
837                 at macro_use
838                 at must_use
839                 at no_mangle
840                 at non_exhaustive
841                 at panic_handler
842                 at path = "…"
843                 at proc_macro
844                 at proc_macro_attribute
845                 at proc_macro_derive(…)
846                 at repr(…)
847                 at should_panic
848                 at target_feature = "…"
849                 at test
850                 at track_caller
851                 at used
852                 at warn(…)
853             "#]],
854         );
855     }
856 }