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