aboutsummaryrefslogtreecommitdiff
path: root/crates/ide/src/completion/complete_attribute.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide/src/completion/complete_attribute.rs')
-rw-r--r--crates/ide/src/completion/complete_attribute.rs657
1 files changed, 0 insertions, 657 deletions
diff --git a/crates/ide/src/completion/complete_attribute.rs b/crates/ide/src/completion/complete_attribute.rs
deleted file mode 100644
index f4a9864d1..000000000
--- a/crates/ide/src/completion/complete_attribute.rs
+++ /dev/null
@@ -1,657 +0,0 @@
1//! Completion for attributes
2//!
3//! This module uses a bit of static metadata to provide completions
4//! for built-in attributes.
5
6use rustc_hash::FxHashSet;
7use syntax::{ast, AstNode, SyntaxKind};
8
9use crate::completion::{
10 completion_context::CompletionContext,
11 completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions},
12 generated_features::FEATURES,
13};
14
15pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
16 if ctx.mod_declaration_under_caret.is_some() {
17 return None;
18 }
19
20 let attribute = ctx.attribute_under_caret.as_ref()?;
21 match (attribute.path(), attribute.token_tree()) {
22 (Some(path), Some(token_tree)) if path.to_string() == "derive" => {
23 complete_derive(acc, ctx, token_tree)
24 }
25 (Some(path), Some(token_tree)) if path.to_string() == "feature" => {
26 complete_lint(acc, ctx, token_tree, FEATURES)
27 }
28 (Some(path), Some(token_tree))
29 if ["allow", "warn", "deny", "forbid"]
30 .iter()
31 .any(|lint_level| lint_level == &path.to_string()) =>
32 {
33 complete_lint(acc, ctx, token_tree, DEFAULT_LINT_COMPLETIONS)
34 }
35 (_, Some(_token_tree)) => {}
36 _ => complete_attribute_start(acc, ctx, attribute),
37 }
38 Some(())
39}
40
41fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attribute: &ast::Attr) {
42 for attr_completion in ATTRIBUTES {
43 let mut item = CompletionItem::new(
44 CompletionKind::Attribute,
45 ctx.source_range(),
46 attr_completion.label,
47 )
48 .kind(CompletionItemKind::Attribute);
49
50 if let Some(lookup) = attr_completion.lookup {
51 item = item.lookup_by(lookup);
52 }
53
54 match (attr_completion.snippet, ctx.config.snippet_cap) {
55 (Some(snippet), Some(cap)) => {
56 item = item.insert_snippet(cap, snippet);
57 }
58 _ => {}
59 }
60
61 if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner {
62 acc.add(item);
63 }
64 }
65}
66
67struct AttrCompletion {
68 label: &'static str,
69 lookup: Option<&'static str>,
70 snippet: Option<&'static str>,
71 prefer_inner: bool,
72}
73
74impl AttrCompletion {
75 const fn prefer_inner(self) -> AttrCompletion {
76 AttrCompletion { prefer_inner: true, ..self }
77 }
78}
79
80const fn attr(
81 label: &'static str,
82 lookup: Option<&'static str>,
83 snippet: Option<&'static str>,
84) -> AttrCompletion {
85 AttrCompletion { label, lookup, snippet, prefer_inner: false }
86}
87
88const ATTRIBUTES: &[AttrCompletion] = &[
89 attr("allow(…)", Some("allow"), Some("allow(${0:lint})")),
90 attr("cfg_attr(…)", Some("cfg_attr"), Some("cfg_attr(${1:predicate}, ${0:attr})")),
91 attr("cfg(…)", Some("cfg"), Some("cfg(${0:predicate})")),
92 attr("deny(…)", Some("deny"), Some("deny(${0:lint})")),
93 attr(r#"deprecated = "…""#, Some("deprecated"), Some(r#"deprecated = "${0:reason}""#)),
94 attr("derive(…)", Some("derive"), Some(r#"derive(${0:Debug})"#)),
95 attr(r#"doc = "…""#, Some("doc"), Some(r#"doc = "${0:docs}""#)),
96 attr("feature(…)", Some("feature"), Some("feature(${0:flag})")).prefer_inner(),
97 attr("forbid(…)", Some("forbid"), Some("forbid(${0:lint})")),
98 // FIXME: resolve through macro resolution?
99 attr("global_allocator", None, None).prefer_inner(),
100 attr(r#"ignore = "…""#, Some("ignore"), Some(r#"ignore = "${0:reason}""#)),
101 attr("inline(…)", Some("inline"), Some("inline(${0:lint})")),
102 attr(r#"link_name = "…""#, Some("link_name"), Some(r#"link_name = "${0:symbol_name}""#)),
103 attr("link", None, None),
104 attr("macro_export", None, None),
105 attr("macro_use", None, None),
106 attr(r#"must_use = "…""#, Some("must_use"), Some(r#"must_use = "${0:reason}""#)),
107 attr("no_mangle", None, None),
108 attr("no_std", None, None).prefer_inner(),
109 attr("non_exhaustive", None, None),
110 attr("panic_handler", None, None).prefer_inner(),
111 attr("path = \"…\"", Some("path"), Some("path =\"${0:path}\"")),
112 attr("proc_macro", None, None),
113 attr("proc_macro_attribute", None, None),
114 attr("proc_macro_derive(…)", Some("proc_macro_derive"), Some("proc_macro_derive(${0:Trait})")),
115 attr("recursion_limit = …", Some("recursion_limit"), Some("recursion_limit = ${0:128}"))
116 .prefer_inner(),
117 attr("repr(…)", Some("repr"), Some("repr(${0:C})")),
118 attr(
119 "should_panic(…)",
120 Some("should_panic"),
121 Some(r#"should_panic(expected = "${0:reason}")"#),
122 ),
123 attr(
124 r#"target_feature = "…""#,
125 Some("target_feature"),
126 Some("target_feature = \"${0:feature}\""),
127 ),
128 attr("test", None, None),
129 attr("used", None, None),
130 attr("warn(…)", Some("warn"), Some("warn(${0:lint})")),
131 attr(
132 r#"windows_subsystem = "…""#,
133 Some("windows_subsystem"),
134 Some(r#"windows_subsystem = "${0:subsystem}""#),
135 )
136 .prefer_inner(),
137];
138
139fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input: ast::TokenTree) {
140 if let Ok(existing_derives) = parse_comma_sep_input(derive_input) {
141 for derive_completion in DEFAULT_DERIVE_COMPLETIONS
142 .into_iter()
143 .filter(|completion| !existing_derives.contains(completion.label))
144 {
145 let mut label = derive_completion.label.to_owned();
146 for dependency in derive_completion
147 .dependencies
148 .into_iter()
149 .filter(|&&dependency| !existing_derives.contains(dependency))
150 {
151 label.push_str(", ");
152 label.push_str(dependency);
153 }
154 acc.add(
155 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
156 .kind(CompletionItemKind::Attribute),
157 );
158 }
159
160 for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
161 acc.add(
162 CompletionItem::new(
163 CompletionKind::Attribute,
164 ctx.source_range(),
165 custom_derive_name,
166 )
167 .kind(CompletionItemKind::Attribute),
168 );
169 }
170 }
171}
172
173fn complete_lint(
174 acc: &mut Completions,
175 ctx: &CompletionContext,
176 derive_input: ast::TokenTree,
177 lints_completions: &[LintCompletion],
178) {
179 if let Ok(existing_lints) = parse_comma_sep_input(derive_input) {
180 for lint_completion in lints_completions
181 .into_iter()
182 .filter(|completion| !existing_lints.contains(completion.label))
183 {
184 acc.add(
185 CompletionItem::new(
186 CompletionKind::Attribute,
187 ctx.source_range(),
188 lint_completion.label,
189 )
190 .kind(CompletionItemKind::Attribute)
191 .detail(lint_completion.description),
192 );
193 }
194 }
195}
196
197fn parse_comma_sep_input(derive_input: ast::TokenTree) -> Result<FxHashSet<String>, ()> {
198 match (derive_input.left_delimiter_token(), derive_input.right_delimiter_token()) {
199 (Some(left_paren), Some(right_paren))
200 if left_paren.kind() == SyntaxKind::L_PAREN
201 && right_paren.kind() == SyntaxKind::R_PAREN =>
202 {
203 let mut input_derives = FxHashSet::default();
204 let mut current_derive = String::new();
205 for token in derive_input
206 .syntax()
207 .children_with_tokens()
208 .filter_map(|token| token.into_token())
209 .skip_while(|token| token != &left_paren)
210 .skip(1)
211 .take_while(|token| token != &right_paren)
212 {
213 if SyntaxKind::COMMA == token.kind() {
214 if !current_derive.is_empty() {
215 input_derives.insert(current_derive);
216 current_derive = String::new();
217 }
218 } else {
219 current_derive.push_str(token.to_string().trim());
220 }
221 }
222
223 if !current_derive.is_empty() {
224 input_derives.insert(current_derive);
225 }
226 Ok(input_derives)
227 }
228 _ => Err(()),
229 }
230}
231
232fn get_derive_names_in_scope(ctx: &CompletionContext) -> FxHashSet<String> {
233 let mut result = FxHashSet::default();
234 ctx.scope.process_all_names(&mut |name, scope_def| {
235 if let hir::ScopeDef::MacroDef(mac) = scope_def {
236 if mac.is_derive_macro() {
237 result.insert(name.to_string());
238 }
239 }
240 });
241 result
242}
243
244struct DeriveCompletion {
245 label: &'static str,
246 dependencies: &'static [&'static str],
247}
248
249/// Standard Rust derives and the information about their dependencies
250/// (the dependencies are needed so that the main derive don't break the compilation when added)
251#[rustfmt::skip]
252const DEFAULT_DERIVE_COMPLETIONS: &[DeriveCompletion] = &[
253 DeriveCompletion { label: "Clone", dependencies: &[] },
254 DeriveCompletion { label: "Copy", dependencies: &["Clone"] },
255 DeriveCompletion { label: "Debug", dependencies: &[] },
256 DeriveCompletion { label: "Default", dependencies: &[] },
257 DeriveCompletion { label: "Hash", dependencies: &[] },
258 DeriveCompletion { label: "PartialEq", dependencies: &[] },
259 DeriveCompletion { label: "Eq", dependencies: &["PartialEq"] },
260 DeriveCompletion { label: "PartialOrd", dependencies: &["PartialEq"] },
261 DeriveCompletion { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
262];
263
264pub(super) struct LintCompletion {
265 pub(super) label: &'static str,
266 pub(super) description: &'static str,
267}
268
269#[rustfmt::skip]
270const DEFAULT_LINT_COMPLETIONS: &[LintCompletion] = &[
271 LintCompletion { label: "absolute_paths_not_starting_with_crate", description: r#"fully qualified paths that start with a module name instead of `crate`, `self`, or an extern crate name"# },
272 LintCompletion { label: "anonymous_parameters", description: r#"detects anonymous parameters"# },
273 LintCompletion { label: "box_pointers", description: r#"use of owned (Box type) heap memory"# },
274 LintCompletion { label: "deprecated_in_future", description: r#"detects use of items that will be deprecated in a future version"# },
275 LintCompletion { label: "elided_lifetimes_in_paths", description: r#"hidden lifetime parameters in types are deprecated"# },
276 LintCompletion { label: "explicit_outlives_requirements", description: r#"outlives requirements can be inferred"# },
277 LintCompletion { label: "indirect_structural_match", description: r#"pattern with const indirectly referencing non-structural-match type"# },
278 LintCompletion { label: "keyword_idents", description: r#"detects edition keywords being used as an identifier"# },
279 LintCompletion { label: "macro_use_extern_crate", description: r#"the `#[macro_use]` attribute is now deprecated in favor of using macros via the module system"# },
280 LintCompletion { label: "meta_variable_misuse", description: r#"possible meta-variable misuse at macro definition"# },
281 LintCompletion { label: "missing_copy_implementations", description: r#"detects potentially-forgotten implementations of `Copy`"# },
282 LintCompletion { label: "missing_crate_level_docs", description: r#"detects crates with no crate-level documentation"# },
283 LintCompletion { label: "missing_debug_implementations", description: r#"detects missing implementations of Debug"# },
284 LintCompletion { label: "missing_docs", description: r#"detects missing documentation for public members"# },
285 LintCompletion { label: "missing_doc_code_examples", description: r#"detects publicly-exported items without code samples in their documentation"# },
286 LintCompletion { label: "non_ascii_idents", description: r#"detects non-ASCII identifiers"# },
287 LintCompletion { label: "private_doc_tests", description: r#"detects code samples in docs of private items not documented by rustdoc"# },
288 LintCompletion { label: "single_use_lifetimes", description: r#"detects lifetime parameters that are only used once"# },
289 LintCompletion { label: "trivial_casts", description: r#"detects trivial casts which could be removed"# },
290 LintCompletion { label: "trivial_numeric_casts", description: r#"detects trivial casts of numeric types which could be removed"# },
291 LintCompletion { label: "unaligned_references", description: r#"detects unaligned references to fields of packed structs"# },
292 LintCompletion { label: "unreachable_pub", description: r#"`pub` items not reachable from crate root"# },
293 LintCompletion { label: "unsafe_code", description: r#"usage of `unsafe` code"# },
294 LintCompletion { label: "unsafe_op_in_unsafe_fn", description: r#"unsafe operations in unsafe functions without an explicit unsafe block are deprecated"# },
295 LintCompletion { label: "unstable_features", description: r#"enabling unstable features (deprecated. do not use)"# },
296 LintCompletion { label: "unused_crate_dependencies", description: r#"crate dependencies that are never used"# },
297 LintCompletion { label: "unused_extern_crates", description: r#"extern crates that are never used"# },
298 LintCompletion { label: "unused_import_braces", description: r#"unnecessary braces around an imported item"# },
299 LintCompletion { label: "unused_lifetimes", description: r#"detects lifetime parameters that are never used"# },
300 LintCompletion { label: "unused_qualifications", description: r#"detects unnecessarily qualified names"# },
301 LintCompletion { label: "unused_results", description: r#"unused result of an expression in a statement"# },
302 LintCompletion { label: "variant_size_differences", description: r#"detects enums with widely varying variant sizes"# },
303 LintCompletion { label: "array_into_iter", description: r#"detects calling `into_iter` on arrays"# },
304 LintCompletion { label: "asm_sub_register", description: r#"using only a subset of a register for inline asm inputs"# },
305 LintCompletion { label: "bare_trait_objects", description: r#"suggest using `dyn Trait` for trait objects"# },
306 LintCompletion { label: "bindings_with_variant_name", description: r#"detects pattern bindings with the same name as one of the matched variants"# },
307 LintCompletion { label: "cenum_impl_drop_cast", description: r#"a C-like enum implementing Drop is cast"# },
308 LintCompletion { label: "clashing_extern_declarations", description: r#"detects when an extern fn has been declared with the same name but different types"# },
309 LintCompletion { label: "coherence_leak_check", description: r#"distinct impls distinguished only by the leak-check code"# },
310 LintCompletion { label: "confusable_idents", description: r#"detects visually confusable pairs between identifiers"# },
311 LintCompletion { label: "dead_code", description: r#"detect unused, unexported items"# },
312 LintCompletion { label: "deprecated", description: r#"detects use of deprecated items"# },
313 LintCompletion { label: "ellipsis_inclusive_range_patterns", description: r#"`...` range patterns are deprecated"# },
314 LintCompletion { label: "exported_private_dependencies", description: r#"public interface leaks type from a private dependency"# },
315 LintCompletion { label: "illegal_floating_point_literal_pattern", description: r#"floating-point literals cannot be used in patterns"# },
316 LintCompletion { label: "improper_ctypes", description: r#"proper use of libc types in foreign modules"# },
317 LintCompletion { label: "improper_ctypes_definitions", description: r#"proper use of libc types in foreign item definitions"# },
318 LintCompletion { label: "incomplete_features", description: r#"incomplete features that may function improperly in some or all cases"# },
319 LintCompletion { label: "inline_no_sanitize", description: r#"detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`"# },
320 LintCompletion { label: "intra_doc_link_resolution_failure", description: r#"failures in resolving intra-doc link targets"# },
321 LintCompletion { label: "invalid_codeblock_attributes", description: r#"codeblock attribute looks a lot like a known one"# },
322 LintCompletion { label: "invalid_value", description: r#"an invalid value is being created (such as a NULL reference)"# },
323 LintCompletion { label: "irrefutable_let_patterns", description: r#"detects irrefutable patterns in if-let and while-let statements"# },
324 LintCompletion { label: "late_bound_lifetime_arguments", description: r#"detects generic lifetime arguments in path segments with late bound lifetime parameters"# },
325 LintCompletion { label: "mixed_script_confusables", description: r#"detects Unicode scripts whose mixed script confusables codepoints are solely used"# },
326 LintCompletion { label: "mutable_borrow_reservation_conflict", description: r#"reservation of a two-phased borrow conflicts with other shared borrows"# },
327 LintCompletion { label: "non_camel_case_types", description: r#"types, variants, traits and type parameters should have camel case names"# },
328 LintCompletion { label: "non_shorthand_field_patterns", description: r#"using `Struct { x: x }` instead of `Struct { x }` in a pattern"# },
329 LintCompletion { label: "non_snake_case", description: r#"variables, methods, functions, lifetime parameters and modules should have snake case names"# },
330 LintCompletion { label: "non_upper_case_globals", description: r#"static constants should have uppercase identifiers"# },
331 LintCompletion { label: "no_mangle_generic_items", description: r#"generic items must be mangled"# },
332 LintCompletion { label: "overlapping_patterns", description: r#"detects overlapping patterns"# },
333 LintCompletion { label: "path_statements", description: r#"path statements with no effect"# },
334 LintCompletion { label: "private_in_public", description: r#"detect private items in public interfaces not caught by the old implementation"# },
335 LintCompletion { label: "proc_macro_derive_resolution_fallback", description: r#"detects proc macro derives using inaccessible names from parent modules"# },
336 LintCompletion { label: "redundant_semicolons", description: r#"detects unnecessary trailing semicolons"# },
337 LintCompletion { label: "renamed_and_removed_lints", description: r#"lints that have been renamed or removed"# },
338 LintCompletion { label: "safe_packed_borrows", description: r#"safe borrows of fields of packed structs were erroneously allowed"# },
339 LintCompletion { label: "stable_features", description: r#"stable features found in `#[feature]` directive"# },
340 LintCompletion { label: "trivial_bounds", description: r#"these bounds don't depend on an type parameters"# },
341 LintCompletion { label: "type_alias_bounds", description: r#"bounds in type aliases are not enforced"# },
342 LintCompletion { label: "tyvar_behind_raw_pointer", description: r#"raw pointer to an inference variable"# },
343 LintCompletion { label: "uncommon_codepoints", description: r#"detects uncommon Unicode codepoints in identifiers"# },
344 LintCompletion { label: "unconditional_recursion", description: r#"functions that cannot return without calling themselves"# },
345 LintCompletion { label: "unknown_lints", description: r#"unrecognized lint attribute"# },
346 LintCompletion { label: "unnameable_test_items", description: r#"detects an item that cannot be named being marked as `#[test_case]`"# },
347 LintCompletion { label: "unreachable_code", description: r#"detects unreachable code paths"# },
348 LintCompletion { label: "unreachable_patterns", description: r#"detects unreachable patterns"# },
349 LintCompletion { label: "unstable_name_collisions", description: r#"detects name collision with an existing but unstable method"# },
350 LintCompletion { label: "unused_allocation", description: r#"detects unnecessary allocations that can be eliminated"# },
351 LintCompletion { label: "unused_assignments", description: r#"detect assignments that will never be read"# },
352 LintCompletion { label: "unused_attributes", description: r#"detects attributes that were not used by the compiler"# },
353 LintCompletion { label: "unused_braces", description: r#"unnecessary braces around an expression"# },
354 LintCompletion { label: "unused_comparisons", description: r#"comparisons made useless by limits of the types involved"# },
355 LintCompletion { label: "unused_doc_comments", description: r#"detects doc comments that aren't used by rustdoc"# },
356 LintCompletion { label: "unused_features", description: r#"unused features found in crate-level `#[feature]` directives"# },
357 LintCompletion { label: "unused_imports", description: r#"imports that are never used"# },
358 LintCompletion { label: "unused_labels", description: r#"detects labels that are never used"# },
359 LintCompletion { label: "unused_macros", description: r#"detects macros that were not used"# },
360 LintCompletion { label: "unused_must_use", description: r#"unused result of a type flagged as `#[must_use]`"# },
361 LintCompletion { label: "unused_mut", description: r#"detect mut variables which don't need to be mutable"# },
362 LintCompletion { label: "unused_parens", description: r#"`if`, `match`, `while` and `return` do not need parentheses"# },
363 LintCompletion { label: "unused_unsafe", description: r#"unnecessary use of an `unsafe` block"# },
364 LintCompletion { label: "unused_variables", description: r#"detect variables which are not used in any way"# },
365 LintCompletion { label: "warnings", description: r#"mass-change the level for lints which produce warnings"# },
366 LintCompletion { label: "where_clauses_object_safety", description: r#"checks the object safety of where clauses"# },
367 LintCompletion { label: "while_true", description: r#"suggest using `loop { }` instead of `while true { }`"# },
368 LintCompletion { label: "ambiguous_associated_items", description: r#"ambiguous associated items"# },
369 LintCompletion { label: "arithmetic_overflow", description: r#"arithmetic operation overflows"# },
370 LintCompletion { label: "conflicting_repr_hints", description: r#"conflicts between `#[repr(..)]` hints that were previously accepted and used in practice"# },
371 LintCompletion { label: "const_err", description: r#"constant evaluation detected erroneous expression"# },
372 LintCompletion { label: "ill_formed_attribute_input", description: r#"ill-formed attribute inputs that were previously accepted and used in practice"# },
373 LintCompletion { label: "incomplete_include", description: r#"trailing content in included file"# },
374 LintCompletion { label: "invalid_type_param_default", description: r#"type parameter default erroneously allowed in invalid location"# },
375 LintCompletion { label: "macro_expanded_macro_exports_accessed_by_absolute_paths", description: r#"macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths"# },
376 LintCompletion { label: "missing_fragment_specifier", description: r#"detects missing fragment specifiers in unused `macro_rules!` patterns"# },
377 LintCompletion { label: "mutable_transmutes", description: r#"mutating transmuted &mut T from &T may cause undefined behavior"# },
378 LintCompletion { label: "no_mangle_const_items", description: r#"const items will not have their symbols exported"# },
379 LintCompletion { label: "order_dependent_trait_objects", description: r#"trait-object types were treated as different depending on marker-trait order"# },
380 LintCompletion { label: "overflowing_literals", description: r#"literal out of range for its type"# },
381 LintCompletion { label: "patterns_in_fns_without_body", description: r#"patterns in functions without body were erroneously allowed"# },
382 LintCompletion { label: "pub_use_of_private_extern_crate", description: r#"detect public re-exports of private extern crates"# },
383 LintCompletion { label: "soft_unstable", description: r#"a feature gate that doesn't break dependent crates"# },
384 LintCompletion { label: "unconditional_panic", description: r#"operation will cause a panic at runtime"# },
385 LintCompletion { label: "unknown_crate_types", description: r#"unknown crate type found in `#[crate_type]` directive"# },
386];
387
388#[cfg(test)]
389mod tests {
390 use expect_test::{expect, Expect};
391
392 use crate::completion::{test_utils::completion_list, CompletionKind};
393
394 fn check(ra_fixture: &str, expect: Expect) {
395 let actual = completion_list(ra_fixture, CompletionKind::Attribute);
396 expect.assert_eq(&actual);
397 }
398
399 #[test]
400 fn empty_derive_completion() {
401 check(
402 r#"
403#[derive(<|>)]
404struct Test {}
405 "#,
406 expect![[r#"
407 at Clone
408 at Copy, Clone
409 at Debug
410 at Default
411 at Eq, PartialEq
412 at Hash
413 at Ord, PartialOrd, Eq, PartialEq
414 at PartialEq
415 at PartialOrd, PartialEq
416 "#]],
417 );
418 }
419
420 #[test]
421 fn empty_lint_completion() {
422 check(
423 r#"#[allow(<|>)]"#,
424 expect![[r#"
425 at absolute_paths_not_starting_with_crate fully qualified paths that start with a module name instead of `crate`, `self`, or an extern crate name
426 at ambiguous_associated_items ambiguous associated items
427 at anonymous_parameters detects anonymous parameters
428 at arithmetic_overflow arithmetic operation overflows
429 at array_into_iter detects calling `into_iter` on arrays
430 at asm_sub_register using only a subset of a register for inline asm inputs
431 at bare_trait_objects suggest using `dyn Trait` for trait objects
432 at bindings_with_variant_name detects pattern bindings with the same name as one of the matched variants
433 at box_pointers use of owned (Box type) heap memory
434 at cenum_impl_drop_cast a C-like enum implementing Drop is cast
435 at clashing_extern_declarations detects when an extern fn has been declared with the same name but different types
436 at coherence_leak_check distinct impls distinguished only by the leak-check code
437 at conflicting_repr_hints conflicts between `#[repr(..)]` hints that were previously accepted and used in practice
438 at confusable_idents detects visually confusable pairs between identifiers
439 at const_err constant evaluation detected erroneous expression
440 at dead_code detect unused, unexported items
441 at deprecated detects use of deprecated items
442 at deprecated_in_future detects use of items that will be deprecated in a future version
443 at elided_lifetimes_in_paths hidden lifetime parameters in types are deprecated
444 at ellipsis_inclusive_range_patterns `...` range patterns are deprecated
445 at explicit_outlives_requirements outlives requirements can be inferred
446 at exported_private_dependencies public interface leaks type from a private dependency
447 at ill_formed_attribute_input ill-formed attribute inputs that were previously accepted and used in practice
448 at illegal_floating_point_literal_pattern floating-point literals cannot be used in patterns
449 at improper_ctypes proper use of libc types in foreign modules
450 at improper_ctypes_definitions proper use of libc types in foreign item definitions
451 at incomplete_features incomplete features that may function improperly in some or all cases
452 at incomplete_include trailing content in included file
453 at indirect_structural_match pattern with const indirectly referencing non-structural-match type
454 at inline_no_sanitize detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`
455 at intra_doc_link_resolution_failure failures in resolving intra-doc link targets
456 at invalid_codeblock_attributes codeblock attribute looks a lot like a known one
457 at invalid_type_param_default type parameter default erroneously allowed in invalid location
458 at invalid_value an invalid value is being created (such as a NULL reference)
459 at irrefutable_let_patterns detects irrefutable patterns in if-let and while-let statements
460 at keyword_idents detects edition keywords being used as an identifier
461 at late_bound_lifetime_arguments detects generic lifetime arguments in path segments with late bound lifetime parameters
462 at macro_expanded_macro_exports_accessed_by_absolute_paths macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths
463 at macro_use_extern_crate the `#[macro_use]` attribute is now deprecated in favor of using macros via the module system
464 at meta_variable_misuse possible meta-variable misuse at macro definition
465 at missing_copy_implementations detects potentially-forgotten implementations of `Copy`
466 at missing_crate_level_docs detects crates with no crate-level documentation
467 at missing_debug_implementations detects missing implementations of Debug
468 at missing_doc_code_examples detects publicly-exported items without code samples in their documentation
469 at missing_docs detects missing documentation for public members
470 at missing_fragment_specifier detects missing fragment specifiers in unused `macro_rules!` patterns
471 at mixed_script_confusables detects Unicode scripts whose mixed script confusables codepoints are solely used
472 at mutable_borrow_reservation_conflict reservation of a two-phased borrow conflicts with other shared borrows
473 at mutable_transmutes mutating transmuted &mut T from &T may cause undefined behavior
474 at no_mangle_const_items const items will not have their symbols exported
475 at no_mangle_generic_items generic items must be mangled
476 at non_ascii_idents detects non-ASCII identifiers
477 at non_camel_case_types types, variants, traits and type parameters should have camel case names
478 at non_shorthand_field_patterns using `Struct { x: x }` instead of `Struct { x }` in a pattern
479 at non_snake_case variables, methods, functions, lifetime parameters and modules should have snake case names
480 at non_upper_case_globals static constants should have uppercase identifiers
481 at order_dependent_trait_objects trait-object types were treated as different depending on marker-trait order
482 at overflowing_literals literal out of range for its type
483 at overlapping_patterns detects overlapping patterns
484 at path_statements path statements with no effect
485 at patterns_in_fns_without_body patterns in functions without body were erroneously allowed
486 at private_doc_tests detects code samples in docs of private items not documented by rustdoc
487 at private_in_public detect private items in public interfaces not caught by the old implementation
488 at proc_macro_derive_resolution_fallback detects proc macro derives using inaccessible names from parent modules
489 at pub_use_of_private_extern_crate detect public re-exports of private extern crates
490 at redundant_semicolons detects unnecessary trailing semicolons
491 at renamed_and_removed_lints lints that have been renamed or removed
492 at safe_packed_borrows safe borrows of fields of packed structs were erroneously allowed
493 at single_use_lifetimes detects lifetime parameters that are only used once
494 at soft_unstable a feature gate that doesn't break dependent crates
495 at stable_features stable features found in `#[feature]` directive
496 at trivial_bounds these bounds don't depend on an type parameters
497 at trivial_casts detects trivial casts which could be removed
498 at trivial_numeric_casts detects trivial casts of numeric types which could be removed
499 at type_alias_bounds bounds in type aliases are not enforced
500 at tyvar_behind_raw_pointer raw pointer to an inference variable
501 at unaligned_references detects unaligned references to fields of packed structs
502 at uncommon_codepoints detects uncommon Unicode codepoints in identifiers
503 at unconditional_panic operation will cause a panic at runtime
504 at unconditional_recursion functions that cannot return without calling themselves
505 at unknown_crate_types unknown crate type found in `#[crate_type]` directive
506 at unknown_lints unrecognized lint attribute
507 at unnameable_test_items detects an item that cannot be named being marked as `#[test_case]`
508 at unreachable_code detects unreachable code paths
509 at unreachable_patterns detects unreachable patterns
510 at unreachable_pub `pub` items not reachable from crate root
511 at unsafe_code usage of `unsafe` code
512 at unsafe_op_in_unsafe_fn unsafe operations in unsafe functions without an explicit unsafe block are deprecated
513 at unstable_features enabling unstable features (deprecated. do not use)
514 at unstable_name_collisions detects name collision with an existing but unstable method
515 at unused_allocation detects unnecessary allocations that can be eliminated
516 at unused_assignments detect assignments that will never be read
517 at unused_attributes detects attributes that were not used by the compiler
518 at unused_braces unnecessary braces around an expression
519 at unused_comparisons comparisons made useless by limits of the types involved
520 at unused_crate_dependencies crate dependencies that are never used
521 at unused_doc_comments detects doc comments that aren't used by rustdoc
522 at unused_extern_crates extern crates that are never used
523 at unused_features unused features found in crate-level `#[feature]` directives
524 at unused_import_braces unnecessary braces around an imported item
525 at unused_imports imports that are never used
526 at unused_labels detects labels that are never used
527 at unused_lifetimes detects lifetime parameters that are never used
528 at unused_macros detects macros that were not used
529 at unused_must_use unused result of a type flagged as `#[must_use]`
530 at unused_mut detect mut variables which don't need to be mutable
531 at unused_parens `if`, `match`, `while` and `return` do not need parentheses
532 at unused_qualifications detects unnecessarily qualified names
533 at unused_results unused result of an expression in a statement
534 at unused_unsafe unnecessary use of an `unsafe` block
535 at unused_variables detect variables which are not used in any way
536 at variant_size_differences detects enums with widely varying variant sizes
537 at warnings mass-change the level for lints which produce warnings
538 at where_clauses_object_safety checks the object safety of where clauses
539 at while_true suggest using `loop { }` instead of `while true { }`
540 "#]],
541 )
542 }
543
544 #[test]
545 fn no_completion_for_incorrect_derive() {
546 check(
547 r#"
548#[derive{<|>)]
549struct Test {}
550"#,
551 expect![[r#""#]],
552 )
553 }
554
555 #[test]
556 fn derive_with_input_completion() {
557 check(
558 r#"
559#[derive(serde::Serialize, PartialEq, <|>)]
560struct Test {}
561"#,
562 expect![[r#"
563 at Clone
564 at Copy, Clone
565 at Debug
566 at Default
567 at Eq
568 at Hash
569 at Ord, PartialOrd, Eq
570 at PartialOrd
571 "#]],
572 )
573 }
574
575 #[test]
576 fn test_attribute_completion() {
577 check(
578 r#"#[<|>]"#,
579 expect![[r#"
580 at allow(…)
581 at cfg(…)
582 at cfg_attr(…)
583 at deny(…)
584 at deprecated = "…"
585 at derive(…)
586 at doc = "…"
587 at forbid(…)
588 at ignore = "…"
589 at inline(…)
590 at link
591 at link_name = "…"
592 at macro_export
593 at macro_use
594 at must_use = "…"
595 at no_mangle
596 at non_exhaustive
597 at path = "…"
598 at proc_macro
599 at proc_macro_attribute
600 at proc_macro_derive(…)
601 at repr(…)
602 at should_panic(…)
603 at target_feature = "…"
604 at test
605 at used
606 at warn(…)
607 "#]],
608 )
609 }
610
611 #[test]
612 fn test_attribute_completion_inside_nested_attr() {
613 check(r#"#[cfg(<|>)]"#, expect![[]])
614 }
615
616 #[test]
617 fn test_inner_attribute_completion() {
618 check(
619 r"#![<|>]",
620 expect![[r#"
621 at allow(…)
622 at cfg(…)
623 at cfg_attr(…)
624 at deny(…)
625 at deprecated = "…"
626 at derive(…)
627 at doc = "…"
628 at feature(…)
629 at forbid(…)
630 at global_allocator
631 at ignore = "…"
632 at inline(…)
633 at link
634 at link_name = "…"
635 at macro_export
636 at macro_use
637 at must_use = "…"
638 at no_mangle
639 at no_std
640 at non_exhaustive
641 at panic_handler
642 at path = "…"
643 at proc_macro
644 at proc_macro_attribute
645 at proc_macro_derive(…)
646 at recursion_limit = …
647 at repr(…)
648 at should_panic(…)
649 at target_feature = "…"
650 at test
651 at used
652 at warn(…)
653 at windows_subsystem = "…"
654 "#]],
655 );
656 }
657}