]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/attribute.rs
Improve completion of cfg attributes
[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, "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 path = "…"
421         "#]],
422         );
423         check(
424             r#"mod foo {#![$0]}"#,
425             expect![[r#"
426                 at allow(…)
427                 at cfg(…)
428                 at cfg_attr(…)
429                 at deny(…)
430                 at forbid(…)
431                 at warn(…)
432                 at deprecated
433                 at doc = "…"
434                 at doc(hidden)
435                 at doc(alias = "…")
436                 at must_use
437                 at no_mangle
438                 at no_implicit_prelude
439             "#]],
440         );
441     }
442
443     #[test]
444     fn complete_attribute_on_macro_rules() {
445         check(
446             r#"#[$0] macro_rules! foo {}"#,
447             expect![[r#"
448                 at allow(…)
449                 at cfg(…)
450                 at cfg_attr(…)
451                 at deny(…)
452                 at forbid(…)
453                 at warn(…)
454                 at deprecated
455                 at doc = "…"
456                 at doc(hidden)
457                 at doc(alias = "…")
458                 at must_use
459                 at no_mangle
460                 at macro_export
461                 at macro_use
462             "#]],
463         );
464     }
465
466     #[test]
467     fn complete_attribute_on_macro_def() {
468         check(
469             r#"#[$0] macro foo {}"#,
470             expect![[r#"
471                 at allow(…)
472                 at cfg(…)
473                 at cfg_attr(…)
474                 at deny(…)
475                 at forbid(…)
476                 at warn(…)
477                 at deprecated
478                 at doc = "…"
479                 at doc(hidden)
480                 at doc(alias = "…")
481                 at must_use
482                 at no_mangle
483             "#]],
484         );
485     }
486
487     #[test]
488     fn complete_attribute_on_extern_crate() {
489         check(
490             r#"#[$0] extern crate foo;"#,
491             expect![[r#"
492                 at allow(…)
493                 at cfg(…)
494                 at cfg_attr(…)
495                 at deny(…)
496                 at forbid(…)
497                 at warn(…)
498                 at deprecated
499                 at doc = "…"
500                 at doc(hidden)
501                 at doc(alias = "…")
502                 at must_use
503                 at no_mangle
504                 at macro_use
505             "#]],
506         );
507     }
508
509     #[test]
510     fn complete_attribute_on_use() {
511         check(
512             r#"#[$0] use foo;"#,
513             expect![[r#"
514                 at allow(…)
515                 at cfg(…)
516                 at cfg_attr(…)
517                 at deny(…)
518                 at forbid(…)
519                 at warn(…)
520                 at deprecated
521                 at doc = "…"
522                 at doc(hidden)
523                 at doc(alias = "…")
524                 at must_use
525                 at no_mangle
526             "#]],
527         );
528     }
529
530     #[test]
531     fn complete_attribute_on_type_alias() {
532         check(
533             r#"#[$0] type foo = ();"#,
534             expect![[r#"
535                 at allow(…)
536                 at cfg(…)
537                 at cfg_attr(…)
538                 at deny(…)
539                 at forbid(…)
540                 at warn(…)
541                 at deprecated
542                 at doc = "…"
543                 at doc(hidden)
544                 at doc(alias = "…")
545                 at must_use
546                 at no_mangle
547             "#]],
548         );
549     }
550
551     #[test]
552     fn complete_attribute_on_struct() {
553         check(
554             r#"#[$0] struct Foo;"#,
555             expect![[r#"
556                 at allow(…)
557                 at cfg(…)
558                 at cfg_attr(…)
559                 at deny(…)
560                 at forbid(…)
561                 at warn(…)
562                 at deprecated
563                 at doc = "…"
564                 at doc(hidden)
565                 at doc(alias = "…")
566                 at must_use
567                 at no_mangle
568                 at derive(…)
569                 at repr(…)
570                 at non_exhaustive
571             "#]],
572         );
573     }
574
575     #[test]
576     fn complete_attribute_on_enum() {
577         check(
578             r#"#[$0] enum Foo {}"#,
579             expect![[r#"
580                 at allow(…)
581                 at cfg(…)
582                 at cfg_attr(…)
583                 at deny(…)
584                 at forbid(…)
585                 at warn(…)
586                 at deprecated
587                 at doc = "…"
588                 at doc(hidden)
589                 at doc(alias = "…")
590                 at must_use
591                 at no_mangle
592                 at derive(…)
593                 at repr(…)
594                 at non_exhaustive
595             "#]],
596         );
597     }
598
599     #[test]
600     fn complete_attribute_on_const() {
601         check(
602             r#"#[$0] const FOO: () = ();"#,
603             expect![[r#"
604                 at allow(…)
605                 at cfg(…)
606                 at cfg_attr(…)
607                 at deny(…)
608                 at forbid(…)
609                 at warn(…)
610                 at deprecated
611                 at doc = "…"
612                 at doc(hidden)
613                 at doc(alias = "…")
614                 at must_use
615                 at no_mangle
616             "#]],
617         );
618     }
619
620     #[test]
621     fn complete_attribute_on_static() {
622         check(
623             r#"#[$0] static FOO: () = ()"#,
624             expect![[r#"
625                 at allow(…)
626                 at cfg(…)
627                 at cfg_attr(…)
628                 at deny(…)
629                 at forbid(…)
630                 at warn(…)
631                 at deprecated
632                 at doc = "…"
633                 at doc(hidden)
634                 at doc(alias = "…")
635                 at must_use
636                 at no_mangle
637                 at export_name = "…"
638                 at link_name = "…"
639                 at link_section = "…"
640                 at global_allocator
641                 at used
642             "#]],
643         );
644     }
645
646     #[test]
647     fn complete_attribute_on_trait() {
648         check(
649             r#"#[$0] trait Foo {}"#,
650             expect![[r#"
651                 at allow(…)
652                 at cfg(…)
653                 at cfg_attr(…)
654                 at deny(…)
655                 at forbid(…)
656                 at warn(…)
657                 at deprecated
658                 at doc = "…"
659                 at doc(hidden)
660                 at doc(alias = "…")
661                 at must_use
662                 at no_mangle
663                 at must_use
664             "#]],
665         );
666     }
667
668     #[test]
669     fn complete_attribute_on_impl() {
670         check(
671             r#"#[$0] impl () {}"#,
672             expect![[r#"
673                 at allow(…)
674                 at cfg(…)
675                 at cfg_attr(…)
676                 at deny(…)
677                 at forbid(…)
678                 at warn(…)
679                 at deprecated
680                 at doc = "…"
681                 at doc(hidden)
682                 at doc(alias = "…")
683                 at must_use
684                 at no_mangle
685                 at automatically_derived
686             "#]],
687         );
688         check(
689             r#"impl () {#![$0]}"#,
690             expect![[r#"
691                 at allow(…)
692                 at cfg(…)
693                 at cfg_attr(…)
694                 at deny(…)
695                 at forbid(…)
696                 at warn(…)
697                 at deprecated
698                 at doc = "…"
699                 at doc(hidden)
700                 at doc(alias = "…")
701                 at must_use
702                 at no_mangle
703             "#]],
704         );
705     }
706
707     #[test]
708     fn complete_attribute_on_extern_block() {
709         check(
710             r#"#[$0] extern {}"#,
711             expect![[r#"
712                 at allow(…)
713                 at cfg(…)
714                 at cfg_attr(…)
715                 at deny(…)
716                 at forbid(…)
717                 at warn(…)
718                 at deprecated
719                 at doc = "…"
720                 at doc(hidden)
721                 at doc(alias = "…")
722                 at must_use
723                 at no_mangle
724                 at link
725             "#]],
726         );
727         check(
728             r#"extern {#![$0]}"#,
729             expect![[r#"
730                 at allow(…)
731                 at cfg(…)
732                 at cfg_attr(…)
733                 at deny(…)
734                 at forbid(…)
735                 at warn(…)
736                 at deprecated
737                 at doc = "…"
738                 at doc(hidden)
739                 at doc(alias = "…")
740                 at must_use
741                 at no_mangle
742                 at link
743             "#]],
744         );
745     }
746
747     #[test]
748     fn complete_attribute_on_variant() {
749         check(
750             r#"enum Foo { #[$0] Bar }"#,
751             expect![[r#"
752                 at allow(…)
753                 at cfg(…)
754                 at cfg_attr(…)
755                 at deny(…)
756                 at forbid(…)
757                 at warn(…)
758                 at non_exhaustive
759             "#]],
760         );
761     }
762
763     #[test]
764     fn complete_attribute_on_fn() {
765         check(
766             r#"#[$0] fn main() {}"#,
767             expect![[r#"
768                 at allow(…)
769                 at cfg(…)
770                 at cfg_attr(…)
771                 at deny(…)
772                 at forbid(…)
773                 at warn(…)
774                 at deprecated
775                 at doc = "…"
776                 at doc(hidden)
777                 at doc(alias = "…")
778                 at must_use
779                 at no_mangle
780                 at export_name = "…"
781                 at link_name = "…"
782                 at link_section = "…"
783                 at cold
784                 at ignore = "…"
785                 at inline
786                 at must_use
787                 at panic_handler
788                 at proc_macro
789                 at proc_macro_derive(…)
790                 at proc_macro_attribute
791                 at should_panic
792                 at target_feature = "…"
793                 at test
794                 at track_caller
795             "#]],
796         );
797     }
798
799     #[test]
800     fn complete_attribute_on_expr() {
801         cov_mark::check!(no_keyword_completion_in_attr_of_expr);
802         check(
803             r#"fn main() { #[$0] foo() }"#,
804             expect![[r#"
805                 at allow(…)
806                 at cfg(…)
807                 at cfg_attr(…)
808                 at deny(…)
809                 at forbid(…)
810                 at warn(…)
811             "#]],
812         );
813     }
814
815     #[test]
816     fn complete_attribute_in_source_file_end() {
817         check(
818             r#"#[$0]"#,
819             expect![[r#"
820                 at allow(…)
821                 at automatically_derived
822                 at cfg(…)
823                 at cfg_attr(…)
824                 at cold
825                 at deny(…)
826                 at deprecated
827                 at derive(…)
828                 at doc = "…"
829                 at doc(alias = "…")
830                 at doc(hidden)
831                 at export_name = "…"
832                 at forbid(…)
833                 at global_allocator
834                 at ignore = "…"
835                 at inline
836                 at link
837                 at link_name = "…"
838                 at link_section = "…"
839                 at macro_export
840                 at macro_use
841                 at must_use
842                 at no_mangle
843                 at non_exhaustive
844                 at panic_handler
845                 at path = "…"
846                 at proc_macro
847                 at proc_macro_attribute
848                 at proc_macro_derive(…)
849                 at repr(…)
850                 at should_panic
851                 at target_feature = "…"
852                 at test
853                 at track_caller
854                 at used
855                 at warn(…)
856             "#]],
857         );
858     }
859
860     #[test]
861     fn test_cfg() {
862         check(
863             r#"#[cfg(target_endian = $0"#,
864             expect![[r#"
865                 at little
866                 at big
867 "#]],
868         );
869     }
870 }