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