Skip to content

Commit 343df88

Browse files
committed
Generate default lint completions
1 parent 5d17b6a commit 343df88

File tree

7 files changed

+1130
-777
lines changed

7 files changed

+1130
-777
lines changed

crates/ide/src/hover.rs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ use hir::{
33
AsAssocItem, AssocItemContainer, GenericParam, HasAttrs, HasSource, HirDisplay, InFile, Module,
44
ModuleDef, Semantics,
55
};
6-
use ide_completion::generated_lint_completions::{CLIPPY_LINTS, FEATURES};
76
use ide_db::{
87
base_db::SourceDatabase,
98
defs::{Definition, NameClass, NameRefClass},
10-
helpers::FamousDefs,
9+
helpers::{
10+
generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES},
11+
FamousDefs,
12+
},
1113
RootDatabase,
1214
};
1315
use itertools::Itertools;
@@ -206,25 +208,36 @@ fn try_hover_for_attribute(token: &SyntaxToken) -> Option<RangeInfo<HoverResult>
206208
if !tt.syntax().text_range().contains(token.text_range().start()) {
207209
return None;
208210
}
209-
let lints = match &*path {
210-
"feature" => FEATURES,
211-
"allow" | "warn" | "forbid" | "error" => {
212-
let is_clippy = algo::skip_trivia_token(token.clone(), Direction::Prev)
213-
.filter(|t| t.kind() == T![::])
214-
.and_then(|t| algo::skip_trivia_token(t, Direction::Prev))
215-
.map_or(false, |t| t.kind() == T![ident] && t.text() == "clippy");
211+
let (is_clippy, lints) = match &*path {
212+
"feature" => (false, FEATURES),
213+
"allow" | "deny" | "forbid" | "warn" => {
214+
let is_clippy = algo::non_trivia_sibling(token.clone().into(), Direction::Prev)
215+
.filter(|t| t.kind() == T![:])
216+
.and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
217+
.filter(|t| t.kind() == T![:])
218+
.and_then(|t| algo::non_trivia_sibling(t, Direction::Prev))
219+
.map_or(false, |t| {
220+
t.kind() == T![ident] && t.into_token().map_or(false, |t| t.text() == "clippy")
221+
});
216222
if is_clippy {
217-
CLIPPY_LINTS
223+
(true, CLIPPY_LINTS)
218224
} else {
219-
&[]
225+
(false, DEFAULT_LINTS)
220226
}
221227
}
222228
_ => return None,
223229
};
224-
let lint = lints
225-
.binary_search_by_key(&token.text(), |lint| lint.label)
226-
.ok()
227-
.map(|idx| &FEATURES[idx])?;
230+
231+
let tmp;
232+
let needle = if is_clippy {
233+
tmp = format!("clippy::{}", token.text());
234+
&tmp
235+
} else {
236+
&*token.text()
237+
};
238+
239+
let lint =
240+
lints.binary_search_by_key(&needle, |lint| lint.label).ok().map(|idx| &lints[idx])?;
228241
Some(RangeInfo::new(
229242
token.text_range(),
230243
HoverResult {
@@ -4055,4 +4068,36 @@ pub fn foo() {}
40554068
"##]],
40564069
)
40574070
}
4071+
4072+
#[test]
4073+
fn hover_lint() {
4074+
check(
4075+
r#"#![allow(arithmetic_overflow$0)]"#,
4076+
expect![[r#"
4077+
*arithmetic_overflow*
4078+
```
4079+
arithmetic_overflow
4080+
```
4081+
___
4082+
4083+
arithmetic operation overflows
4084+
"#]],
4085+
)
4086+
}
4087+
4088+
#[test]
4089+
fn hover_clippy_lint() {
4090+
check(
4091+
r#"#![allow(clippy::almost_swapped$0)]"#,
4092+
expect![[r#"
4093+
*almost_swapped*
4094+
```
4095+
clippy::almost_swapped
4096+
```
4097+
___
4098+
4099+
Checks for `foo = bar; bar = foo` sequences.
4100+
"#]],
4101+
)
4102+
}
40584103
}

crates/ide_completion/src/completions/attribute.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
//! This module uses a bit of static metadata to provide completions
44
//! for built-in attributes.
55
6+
use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES};
67
use once_cell::sync::Lazy;
78
use rustc_hash::{FxHashMap, FxHashSet};
89
use syntax::{algo::non_trivia_sibling, ast, AstNode, Direction, NodeOrToken, SyntaxKind, T};
910

1011
use crate::{
1112
context::CompletionContext,
12-
generated_lint_completions::{CLIPPY_LINTS, FEATURES},
1313
item::{CompletionItem, CompletionItemKind, CompletionKind},
1414
Completions,
1515
};
1616

1717
mod derive;
1818
mod lint;
19-
pub(crate) use self::lint::LintCompletion;
2019

2120
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
2221
let attribute = ctx.attribute_under_caret.as_ref()?;
@@ -25,7 +24,7 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
2524
"derive" => derive::complete_derive(acc, ctx, token_tree),
2625
"feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
2726
"allow" | "warn" | "deny" | "forbid" => {
28-
lint::complete_lint(acc, ctx, token_tree.clone(), lint::DEFAULT_LINT_COMPLETIONS);
27+
lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
2928
lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
3029
}
3130
_ => (),

crates/ide_completion/src/completions/attribute/lint.rs

Lines changed: 2 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Completion for lints
2+
use ide_db::helpers::generated_lints::Lint;
23
use syntax::ast;
34

45
use crate::{
@@ -11,7 +12,7 @@ pub(super) fn complete_lint(
1112
acc: &mut Completions,
1213
ctx: &CompletionContext,
1314
derive_input: ast::TokenTree,
14-
lints_completions: &[LintCompletion],
15+
lints_completions: &[Lint],
1516
) {
1617
if let Some(existing_lints) = super::parse_comma_sep_input(derive_input) {
1718
for lint_completion in lints_completions
@@ -29,130 +30,6 @@ pub(super) fn complete_lint(
2930
}
3031
}
3132

32-
pub struct LintCompletion {
33-
pub label: &'static str,
34-
pub description: &'static str,
35-
}
36-
37-
#[rustfmt::skip]
38-
pub const DEFAULT_LINT_COMPLETIONS: &[LintCompletion] = &[
39-
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"# },
40-
LintCompletion { label: "ambiguous_associated_items", description: r#"ambiguous associated items"# },
41-
LintCompletion { label: "anonymous_parameters", description: r#"detects anonymous parameters"# },
42-
LintCompletion { label: "arithmetic_overflow", description: r#"arithmetic operation overflows"# },
43-
LintCompletion { label: "array_into_iter", description: r#"detects calling `into_iter` on arrays"# },
44-
LintCompletion { label: "asm_sub_register", description: r#"using only a subset of a register for inline asm inputs"# },
45-
LintCompletion { label: "bare_trait_objects", description: r#"suggest using `dyn Trait` for trait objects"# },
46-
LintCompletion { label: "bindings_with_variant_name", description: r#"detects pattern bindings with the same name as one of the matched variants"# },
47-
LintCompletion { label: "box_pointers", description: r#"use of owned (Box type) heap memory"# },
48-
LintCompletion { label: "cenum_impl_drop_cast", description: r#"a C-like enum implementing Drop is cast"# },
49-
LintCompletion { label: "clashing_extern_declarations", description: r#"detects when an extern fn has been declared with the same name but different types"# },
50-
LintCompletion { label: "coherence_leak_check", description: r#"distinct impls distinguished only by the leak-check code"# },
51-
LintCompletion { label: "conflicting_repr_hints", description: r#"conflicts between `#[repr(..)]` hints that were previously accepted and used in practice"# },
52-
LintCompletion { label: "confusable_idents", description: r#"detects visually confusable pairs between identifiers"# },
53-
LintCompletion { label: "const_err", description: r#"constant evaluation detected erroneous expression"# },
54-
LintCompletion { label: "dead_code", description: r#"detect unused, unexported items"# },
55-
LintCompletion { label: "deprecated_in_future", description: r#"detects use of items that will be deprecated in a future version"# },
56-
LintCompletion { label: "deprecated", description: r#"detects use of deprecated items"# },
57-
LintCompletion { label: "elided_lifetimes_in_paths", description: r#"hidden lifetime parameters in types are deprecated"# },
58-
LintCompletion { label: "ellipsis_inclusive_range_patterns", description: r#"`...` range patterns are deprecated"# },
59-
LintCompletion { label: "explicit_outlives_requirements", description: r#"outlives requirements can be inferred"# },
60-
LintCompletion { label: "exported_private_dependencies", description: r#"public interface leaks type from a private dependency"# },
61-
LintCompletion { label: "ill_formed_attribute_input", description: r#"ill-formed attribute inputs that were previously accepted and used in practice"# },
62-
LintCompletion { label: "illegal_floating_point_literal_pattern", description: r#"floating-point literals cannot be used in patterns"# },
63-
LintCompletion { label: "improper_ctypes_definitions", description: r#"proper use of libc types in foreign item definitions"# },
64-
LintCompletion { label: "improper_ctypes", description: r#"proper use of libc types in foreign modules"# },
65-
LintCompletion { label: "incomplete_features", description: r#"incomplete features that may function improperly in some or all cases"# },
66-
LintCompletion { label: "incomplete_include", description: r#"trailing content in included file"# },
67-
LintCompletion { label: "indirect_structural_match", description: r#"pattern with const indirectly referencing non-structural-match type"# },
68-
LintCompletion { label: "inline_no_sanitize", description: r#"detects incompatible use of `#[inline(always)]` and `#[no_sanitize(...)]`"# },
69-
LintCompletion { label: "intra_doc_link_resolution_failure", description: r#"failures in resolving intra-doc link targets"# },
70-
LintCompletion { label: "invalid_codeblock_attributes", description: r#"codeblock attribute looks a lot like a known one"# },
71-
LintCompletion { label: "invalid_type_param_default", description: r#"type parameter default erroneously allowed in invalid location"# },
72-
LintCompletion { label: "invalid_value", description: r#"an invalid value is being created (such as a NULL reference)"# },
73-
LintCompletion { label: "irrefutable_let_patterns", description: r#"detects irrefutable patterns in if-let and while-let statements"# },
74-
LintCompletion { label: "keyword_idents", description: r#"detects edition keywords being used as an identifier"# },
75-
LintCompletion { label: "late_bound_lifetime_arguments", description: r#"detects generic lifetime arguments in path segments with late bound lifetime parameters"# },
76-
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"# },
77-
LintCompletion { label: "macro_use_extern_crate", description: r#"the `#[macro_use]` attribute is now deprecated in favor of using macros via the module system"# },
78-
LintCompletion { label: "meta_variable_misuse", description: r#"possible meta-variable misuse at macro definition"# },
79-
LintCompletion { label: "missing_copy_implementations", description: r#"detects potentially-forgotten implementations of `Copy`"# },
80-
LintCompletion { label: "missing_crate_level_docs", description: r#"detects crates with no crate-level documentation"# },
81-
LintCompletion { label: "missing_debug_implementations", description: r#"detects missing implementations of Debug"# },
82-
LintCompletion { label: "missing_doc_code_examples", description: r#"detects publicly-exported items without code samples in their documentation"# },
83-
LintCompletion { label: "missing_docs", description: r#"detects missing documentation for public members"# },
84-
LintCompletion { label: "missing_fragment_specifier", description: r#"detects missing fragment specifiers in unused `macro_rules!` patterns"# },
85-
LintCompletion { label: "mixed_script_confusables", description: r#"detects Unicode scripts whose mixed script confusables codepoints are solely used"# },
86-
LintCompletion { label: "mutable_borrow_reservation_conflict", description: r#"reservation of a two-phased borrow conflicts with other shared borrows"# },
87-
LintCompletion { label: "mutable_transmutes", description: r#"mutating transmuted &mut T from &T may cause undefined behavior"# },
88-
LintCompletion { label: "no_mangle_const_items", description: r#"const items will not have their symbols exported"# },
89-
LintCompletion { label: "no_mangle_generic_items", description: r#"generic items must be mangled"# },
90-
LintCompletion { label: "non_ascii_idents", description: r#"detects non-ASCII identifiers"# },
91-
LintCompletion { label: "non_camel_case_types", description: r#"types, variants, traits and type parameters should have camel case names"# },
92-
LintCompletion { label: "non_shorthand_field_patterns", description: r#"using `Struct { x: x }` instead of `Struct { x }` in a pattern"# },
93-
LintCompletion { label: "non_snake_case", description: r#"variables, methods, functions, lifetime parameters and modules should have snake case names"# },
94-
LintCompletion { label: "non_upper_case_globals", description: r#"static constants should have uppercase identifiers"# },
95-
LintCompletion { label: "order_dependent_trait_objects", description: r#"trait-object types were treated as different depending on marker-trait order"# },
96-
LintCompletion { label: "overflowing_literals", description: r#"literal out of range for its type"# },
97-
LintCompletion { label: "overlapping_patterns", description: r#"detects overlapping patterns"# },
98-
LintCompletion { label: "path_statements", description: r#"path statements with no effect"# },
99-
LintCompletion { label: "patterns_in_fns_without_body", description: r#"patterns in functions without body were erroneously allowed"# },
100-
LintCompletion { label: "private_doc_tests", description: r#"detects code samples in docs of private items not documented by rustdoc"# },
101-
LintCompletion { label: "private_in_public", description: r#"detect private items in public interfaces not caught by the old implementation"# },
102-
LintCompletion { label: "proc_macro_derive_resolution_fallback", description: r#"detects proc macro derives using inaccessible names from parent modules"# },
103-
LintCompletion { label: "pub_use_of_private_extern_crate", description: r#"detect public re-exports of private extern crates"# },
104-
LintCompletion { label: "redundant_semicolons", description: r#"detects unnecessary trailing semicolons"# },
105-
LintCompletion { label: "renamed_and_removed_lints", description: r#"lints that have been renamed or removed"# },
106-
LintCompletion { label: "safe_packed_borrows", description: r#"safe borrows of fields of packed structs were erroneously allowed"# },
107-
LintCompletion { label: "single_use_lifetimes", description: r#"detects lifetime parameters that are only used once"# },
108-
LintCompletion { label: "soft_unstable", description: r#"a feature gate that doesn't break dependent crates"# },
109-
LintCompletion { label: "stable_features", description: r#"stable features found in `#[feature]` directive"# },
110-
LintCompletion { label: "trivial_bounds", description: r#"these bounds don't depend on an type parameters"# },
111-
LintCompletion { label: "trivial_casts", description: r#"detects trivial casts which could be removed"# },
112-
LintCompletion { label: "trivial_numeric_casts", description: r#"detects trivial casts of numeric types which could be removed"# },
113-
LintCompletion { label: "type_alias_bounds", description: r#"bounds in type aliases are not enforced"# },
114-
LintCompletion { label: "tyvar_behind_raw_pointer", description: r#"raw pointer to an inference variable"# },
115-
LintCompletion { label: "unaligned_references", description: r#"detects unaligned references to fields of packed structs"# },
116-
LintCompletion { label: "uncommon_codepoints", description: r#"detects uncommon Unicode codepoints in identifiers"# },
117-
LintCompletion { label: "unconditional_panic", description: r#"operation will cause a panic at runtime"# },
118-
LintCompletion { label: "unconditional_recursion", description: r#"functions that cannot return without calling themselves"# },
119-
LintCompletion { label: "unknown_crate_types", description: r#"unknown crate type found in `#[crate_type]` directive"# },
120-
LintCompletion { label: "unknown_lints", description: r#"unrecognized lint attribute"# },
121-
LintCompletion { label: "unnameable_test_items", description: r#"detects an item that cannot be named being marked as `#[test_case]`"# },
122-
LintCompletion { label: "unreachable_code", description: r#"detects unreachable code paths"# },
123-
LintCompletion { label: "unreachable_patterns", description: r#"detects unreachable patterns"# },
124-
LintCompletion { label: "unreachable_pub", description: r#"`pub` items not reachable from crate root"# },
125-
LintCompletion { label: "unsafe_code", description: r#"usage of `unsafe` code"# },
126-
LintCompletion { label: "unsafe_op_in_unsafe_fn", description: r#"unsafe operations in unsafe functions without an explicit unsafe block are deprecated"# },
127-
LintCompletion { label: "unstable_features", description: r#"enabling unstable features (deprecated. do not use)"# },
128-
LintCompletion { label: "unstable_name_collisions", description: r#"detects name collision with an existing but unstable method"# },
129-
LintCompletion { label: "unused_allocation", description: r#"detects unnecessary allocations that can be eliminated"# },
130-
LintCompletion { label: "unused_assignments", description: r#"detect assignments that will never be read"# },
131-
LintCompletion { label: "unused_attributes", description: r#"detects attributes that were not used by the compiler"# },
132-
LintCompletion { label: "unused_braces", description: r#"unnecessary braces around an expression"# },
133-
LintCompletion { label: "unused_comparisons", description: r#"comparisons made useless by limits of the types involved"# },
134-
LintCompletion { label: "unused_crate_dependencies", description: r#"crate dependencies that are never used"# },
135-
LintCompletion { label: "unused_doc_comments", description: r#"detects doc comments that aren't used by rustdoc"# },
136-
LintCompletion { label: "unused_extern_crates", description: r#"extern crates that are never used"# },
137-
LintCompletion { label: "unused_features", description: r#"unused features found in crate-level `#[feature]` directives"# },
138-
LintCompletion { label: "unused_import_braces", description: r#"unnecessary braces around an imported item"# },
139-
LintCompletion { label: "unused_imports", description: r#"imports that are never used"# },
140-
LintCompletion { label: "unused_labels", description: r#"detects labels that are never used"# },
141-
LintCompletion { label: "unused_lifetimes", description: r#"detects lifetime parameters that are never used"# },
142-
LintCompletion { label: "unused_macros", description: r#"detects macros that were not used"# },
143-
LintCompletion { label: "unused_must_use", description: r#"unused result of a type flagged as `#[must_use]`"# },
144-
LintCompletion { label: "unused_mut", description: r#"detect mut variables which don't need to be mutable"# },
145-
LintCompletion { label: "unused_parens", description: r#"`if`, `match`, `while` and `return` do not need parentheses"# },
146-
LintCompletion { label: "unused_qualifications", description: r#"detects unnecessarily qualified names"# },
147-
LintCompletion { label: "unused_results", description: r#"unused result of an expression in a statement"# },
148-
LintCompletion { label: "unused_unsafe", description: r#"unnecessary use of an `unsafe` block"# },
149-
LintCompletion { label: "unused_variables", description: r#"detect variables which are not used in any way"# },
150-
LintCompletion { label: "variant_size_differences", description: r#"detects enums with widely varying variant sizes"# },
151-
LintCompletion { label: "warnings", description: r#"mass-change the level for lints which produce warnings"# },
152-
LintCompletion { label: "where_clauses_object_safety", description: r#"checks the object safety of where clauses"# },
153-
LintCompletion { label: "while_true", description: r#"suggest using `loop { }` instead of `while true { }`"# },
154-
];
155-
15633
#[cfg(test)]
15734
mod tests {
15835

crates/ide_completion/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ mod render;
1010

1111
mod completions;
1212

13-
pub mod generated_lint_completions;
14-
1513
use completions::flyimport::position_for_import;
1614
use ide_db::{
1715
base_db::FilePosition,

crates/ide_db/src/helpers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod import_assets;
33
pub mod insert_use;
44
pub mod merge_imports;
55
pub mod rust_doc;
6+
pub mod generated_lints;
67

78
use std::collections::VecDeque;
89

0 commit comments

Comments
 (0)