diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs index 6abc2a94c4..e139dcd40e 100644 --- a/crates/ide_completion/src/completions/attribute.rs +++ b/crates/ide_completion/src/completions/attribute.rs @@ -4,7 +4,7 @@ //! for built-in attributes. use hir::HasAttrs; -use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES}; +use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS}; use itertools::Itertools; use once_cell::sync::Lazy; use rustc_hash::FxHashMap; @@ -29,12 +29,16 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) }; match (name_ref, attribute.token_tree()) { (Some(path), Some(token_tree)) => match path.text().as_str() { - "derive" => derive::complete_derive(acc, ctx, token_tree), "repr" => repr::complete_repr(acc, ctx, token_tree), - "feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES), + "derive" => derive::complete_derive(acc, ctx, &parse_comma_sep_paths(token_tree)?), + "feature" => { + lint::complete_lint(acc, ctx, &parse_comma_sep_paths(token_tree)?, FEATURES) + } "allow" | "warn" | "deny" | "forbid" => { - lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS); - lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS); + let existing_lints = parse_comma_sep_paths(token_tree)?; + lint::complete_lint(acc, ctx, &existing_lints, DEFAULT_LINTS); + lint::complete_lint(acc, ctx, &existing_lints, CLIPPY_LINTS); + lint::complete_lint(acc, ctx, &existing_lints, RUSTDOC_LINTS); } "cfg" => { cfg::complete_cfg(acc, ctx); diff --git a/crates/ide_completion/src/completions/attribute/derive.rs b/crates/ide_completion/src/completions/attribute/derive.rs index 2824b2a2c2..e460a91102 100644 --- a/crates/ide_completion/src/completions/attribute/derive.rs +++ b/crates/ide_completion/src/completions/attribute/derive.rs @@ -14,60 +14,57 @@ use crate::{ pub(super) fn complete_derive( acc: &mut Completions, ctx: &CompletionContext, - derive_input: ast::TokenTree, + existing_derives: &[ast::Path], ) { - if let Some(existing_derives) = super::parse_comma_sep_paths(derive_input.clone()) { - let core = FamousDefs(&ctx.sema, ctx.krate).core(); - let existing_derives: FxHashSet<_> = existing_derives - .into_iter() - .filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path)) - .filter(|mac| mac.kind() == MacroKind::Derive) - .collect(); + let core = FamousDefs(&ctx.sema, ctx.krate).core(); + let existing_derives: FxHashSet<_> = existing_derives + .into_iter() + .filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path)) + .filter(|mac| mac.kind() == MacroKind::Derive) + .collect(); - for (name, mac) in get_derives_in_scope(ctx) { - if existing_derives.contains(&mac) { - continue; - } - - let name = name.to_smol_str(); - let label; - let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) { - // show derive dependencies for `core`/`std` derives - Some((core, mac_krate)) if core == mac_krate => { - if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES - .iter() - .find(|derive_completion| derive_completion.label == name) - { - let mut components = vec![derive_completion.label]; - components.extend(derive_completion.dependencies.iter().filter( - |&&dependency| { - !existing_derives - .iter() - .filter_map(|it| it.name(ctx.db)) - .any(|it| it.to_smol_str() == dependency) - }, - )); - let lookup = components.join(", "); - label = components.iter().rev().join(", "); - (label.as_str(), Some(lookup)) - } else { - (&*name, None) - } - } - _ => (&*name, None), - }; - - let mut item = - CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label); - item.kind(CompletionItemKind::Attribute); - if let Some(docs) = mac.docs(ctx.db) { - item.documentation(docs); - } - if let Some(lookup) = lookup { - item.lookup_by(lookup); - } - item.add_to(acc); + for (name, mac) in get_derives_in_scope(ctx) { + if existing_derives.contains(&mac) { + continue; } + + let name = name.to_smol_str(); + let label; + let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) { + // show derive dependencies for `core`/`std` derives + Some((core, mac_krate)) if core == mac_krate => { + if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES + .iter() + .find(|derive_completion| derive_completion.label == name) + { + let mut components = vec![derive_completion.label]; + components.extend(derive_completion.dependencies.iter().filter( + |&&dependency| { + !existing_derives + .iter() + .filter_map(|it| it.name(ctx.db)) + .any(|it| it.to_smol_str() == dependency) + }, + )); + let lookup = components.join(", "); + label = components.iter().rev().join(", "); + (label.as_str(), Some(lookup)) + } else { + (&*name, None) + } + } + _ => (&*name, None), + }; + + let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label); + item.kind(CompletionItemKind::Attribute); + if let Some(docs) = mac.docs(ctx.db) { + item.documentation(docs); + } + if let Some(lookup) = lookup { + item.lookup_by(lookup); + } + item.add_to(acc); } } diff --git a/crates/ide_completion/src/completions/attribute/lint.rs b/crates/ide_completion/src/completions/attribute/lint.rs index b7ad06d2f0..18942f8beb 100644 --- a/crates/ide_completion/src/completions/attribute/lint.rs +++ b/crates/ide_completion/src/completions/attribute/lint.rs @@ -11,60 +11,56 @@ use crate::{ pub(super) fn complete_lint( acc: &mut Completions, ctx: &CompletionContext, - derive_input: ast::TokenTree, + existing_lints: &[ast::Path], lints_completions: &[Lint], ) { - if let Some(existing_lints) = super::parse_comma_sep_paths(derive_input) { - for &Lint { label, description } in lints_completions { - let (qual, name) = { - // FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead? - let mut parts = label.split("::"); - let ns_or_label = match parts.next() { - Some(it) => it, - None => continue, - }; - let label = parts.next(); - match label { - Some(label) => (Some(ns_or_label), label), - None => (None, ns_or_label), - } + let is_qualified = ctx.previous_token_is(T![:]); + for &Lint { label, description } in lints_completions { + let (qual, name) = { + // FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead? + let mut parts = label.split("::"); + let ns_or_label = match parts.next() { + Some(it) => it, + None => continue, }; - let lint_already_annotated = existing_lints - .iter() - .filter_map(|path| { - let q = path.qualifier(); - if q.as_ref().and_then(|it| it.qualifier()).is_some() { - return None; - } - Some((q.and_then(|it| it.as_single_name_ref()), path.segment()?.name_ref()?)) - }) - .any(|(q, name_ref)| { - let qualifier_matches = match (q, qual) { - (None, None) => true, - (None, Some(_)) => false, - (Some(_), None) => false, - (Some(q), Some(ns)) => q.text() == ns, - }; - qualifier_matches && name_ref.text() == name - }); - if lint_already_annotated { - continue; + let label = parts.next(); + match label { + Some(label) => (Some(ns_or_label), label), + None => (None, ns_or_label), } - let insert = match (qual, ctx.previous_token_is(T![:])) { - (Some(qual), false) => format!("{}::{}", qual, name), - // user is completing a qualified path but this completion has no qualifier - // so discard this completion - // FIXME: This is currently very hacky and will propose odd completions if - // we add more qualified (tool) completions other than clippy - (None, true) => continue, - _ => name.to_owned(), - }; - let mut item = - CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label); - item.kind(CompletionItemKind::Attribute) - .insert_text(insert) - .documentation(hir::Documentation::new(description.to_owned())); - item.add_to(acc) + }; + if qual.is_none() && is_qualified { + // qualified completion requested, but this lint is unqualified + continue; } + let lint_already_annotated = existing_lints + .iter() + .filter_map(|path| { + let q = path.qualifier(); + if q.as_ref().and_then(|it| it.qualifier()).is_some() { + return None; + } + Some((q.and_then(|it| it.as_single_name_ref()), path.segment()?.name_ref()?)) + }) + .any(|(q, name_ref)| { + let qualifier_matches = match (q, qual) { + (None, None) => true, + (None, Some(_)) => false, + (Some(_), None) => false, + (Some(q), Some(ns)) => q.text() == ns, + }; + qualifier_matches && name_ref.text() == name + }); + if lint_already_annotated { + continue; + } + let label = match qual { + Some(qual) if !is_qualified => format!("{}::{}", qual, name), + _ => name.to_owned(), + }; + let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label); + item.kind(CompletionItemKind::Attribute) + .documentation(hir::Documentation::new(description.to_owned())); + item.add_to(acc) } } diff --git a/crates/ide_completion/src/tests/attribute.rs b/crates/ide_completion/src/tests/attribute.rs index 6a37b53cf5..9f86fc50a2 100644 --- a/crates/ide_completion/src/tests/attribute.rs +++ b/crates/ide_completion/src/tests/attribute.rs @@ -693,11 +693,29 @@ mod lint { #[test] fn lint_clippy_qualified() { check_edit( - "clippy::as_conversions", + "as_conversions", r#"#[allow(clippy::$0)] struct Test;"#, r#"#[allow(clippy::as_conversions)] struct Test;"#, ); } + + #[test] + fn lint_rustdoc_unqualified() { + check_edit( + "rustdoc::bare_urls", + r#"#[allow($0)] struct Test;"#, + r#"#[allow(rustdoc::bare_urls)] struct Test;"#, + ); + } + + #[test] + fn lint_rustdoc_qualified() { + check_edit( + "bare_urls", + r#"#[allow(rustdoc::$0)] struct Test;"#, + r#"#[allow(rustdoc::bare_urls)] struct Test;"#, + ); + } } mod repr { diff --git a/crates/ide_db/src/helpers/generated_lints.rs b/crates/ide_db/src/helpers/generated_lints.rs index 5abd6d360e..e69b3fbdf8 100644 --- a/crates/ide_db/src/helpers/generated_lints.rs +++ b/crates/ide_db/src/helpers/generated_lints.rs @@ -502,6 +502,46 @@ pub const DEFAULT_LINTS: &[Lint] = &[ }, ]; +pub const RUSTDOC_LINTS: &[Lint] = &[ + Lint { + label: "rustdoc::all", + description: r##"lint group for: rustdoc::broken-intra-doc-links, rustdoc::private-intra-doc-links, rustdoc::missing-doc-code-examples, rustdoc::private-doc-tests, rustdoc::invalid-codeblock-attributes, rustdoc::invalid-rust-codeblocks, rustdoc::invalid-html-tags, rustdoc::bare-urls, rustdoc::missing-crate-level-docs"##, + }, + Lint { label: "rustdoc::bare_urls", description: r##"detects URLs that are not hyperlinks"## }, + Lint { + label: "rustdoc::broken_intra_doc_links", + description: r##"failures in resolving intra-doc link targets"##, + }, + Lint { + label: "rustdoc::invalid_codeblock_attributes", + description: r##"codeblock attribute looks a lot like a known one"##, + }, + Lint { + label: "rustdoc::invalid_html_tags", + description: r##"detects invalid HTML tags in doc comments"##, + }, + Lint { + label: "rustdoc::invalid_rust_codeblocks", + description: r##"codeblock could not be parsed as valid Rust or is empty"##, + }, + Lint { + label: "rustdoc::missing_crate_level_docs", + description: r##"detects crates with no crate-level documentation"##, + }, + Lint { + label: "rustdoc::missing_doc_code_examples", + description: r##"detects publicly-exported items without code samples in their documentation"##, + }, + Lint { + label: "rustdoc::private_doc_tests", + description: r##"detects code samples in docs of private items not documented by rustdoc"##, + }, + Lint { + label: "rustdoc::private_intra_doc_links", + description: r##"linking from a public item to a private one"##, + }, +]; + pub const FEATURES: &[Lint] = &[ Lint { label: "abi_c_cmse_nonsecure_call", @@ -5572,11 +5612,9 @@ outside ticks in documentation."##, }, Lint { label: "clippy::double_must_use", - description: r##"Checks for a [`#[must_use]`] attribute without + description: r##"Checks for a `#[must_use]` attribute without further information on functions and methods that return a type already -marked as `#[must_use]`. - -[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, +marked as `#[must_use]`."##, }, Lint { label: "clippy::double_neg", @@ -5821,6 +5859,12 @@ derives the Copy trait"##, label: "clippy::forget_ref", description: r##"Checks for calls to `std::mem::forget` with a reference instead of an owned value."##, + }, + Lint { + label: "clippy::format_in_format_args", + description: r##"Detects `format!` within the arguments of another macro that does +formatting such as `format!` itself, `write!` or `println!`. Suggests +inlining the `format!` call."##, }, Lint { label: "clippy::from_iter_instead_of_collect", @@ -6120,8 +6164,7 @@ where expr has a type that implements `Drop`"##, }, Lint { label: "clippy::let_underscore_must_use", - description: r##"Checks for `let _ = ` -where expr is #[must_use]"##, + description: r##"Checks for `let _ = ` where expr is `#[must_use]`"##, }, Lint { label: "clippy::let_unit_value", description: r##"Checks for binding a unit value."## }, Lint { @@ -6194,23 +6237,7 @@ be more readably expressed as `(3..8).contains(x)`."##, }, Lint { label: "clippy::manual_split_once", - description: r##"**What it does:** Checks for usages of `str::splitn(2, _)` - -**Why is this bad?** `split_once` is both clearer in intent and slightly more efficient. - -**Known problems:** None. - -**Example:** - -```rust -// Bad - let (key, value) = _.splitn(2, '=').next_tuple()?; - let value = _.splitn(2, '=').nth(1)?; - -// Good -let (key, value) = _.split_once('=')?; -let value = _.split_once('=')?.1; -```"##, + description: r##"Checks for usages of `str::splitn(2, _)`"##, }, Lint { label: "clippy::manual_str_repeat", @@ -6304,6 +6331,10 @@ instead. It also checks for `if let &foo = bar` blocks."##, label: "clippy::match_single_binding", description: r##"Checks for useless match that binds to only one value."##, }, + Lint { + label: "clippy::match_str_case_mismatch", + description: r##"Checks for `match` expressions modifying the case of a string with non-compliant arms"##, + }, Lint { label: "clippy::match_wild_err_arm", description: r##"Checks for arm which matches all errors with `Err(_)` @@ -6433,17 +6464,13 @@ used."##, Lint { label: "clippy::must_use_candidate", description: r##"Checks for public functions that have no -[`#[must_use]`] attribute, but return something not already marked -must-use, have no mutable arg and mutate no statics. - -[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, +`#[must_use]` attribute, but return something not already marked +must-use, have no mutable arg and mutate no statics."##, }, Lint { label: "clippy::must_use_unit", - description: r##"Checks for a [`#[must_use]`] attribute on -unit-returning functions and methods. - -[`#[must_use]`]: https://doc.rust-lang.org/reference/attributes/diagnostics.html#the-must_use-attribute"##, + description: r##"Checks for a `#[must_use]` attribute on +unit-returning functions and methods."##, }, Lint { label: "clippy::mut_from_ref", @@ -6591,6 +6618,10 @@ implementation of label: "clippy::no_effect", description: r##"Checks for statements which have no effect."##, }, + Lint { + label: "clippy::no_effect_underscore_binding", + description: r##"Checks for binding to underscore prefixed variable without side-effects."##, + }, Lint { label: "clippy::non_ascii_literal", description: r##"Checks for non-ASCII characters in string literals."##, @@ -7155,6 +7186,12 @@ assign a value in it."##, label: "clippy::to_string_in_display", description: r##"Checks for uses of `to_string()` in `Display` traits."##, }, + Lint { + label: "clippy::to_string_in_format_args", + description: r##"Checks for [`ToString::to_string`](https://doc.rust-lang.org/std/string/trait.ToString.html#tymethod.to_string) +applied to a type that implements [`Display`](https://doc.rust-lang.org/std/fmt/trait.Display.html) +in a macro that does formatting."##, + }, Lint { label: "clippy::todo", description: r##"Checks for usage of `todo!`."## }, Lint { label: "clippy::too_many_arguments", @@ -7194,6 +7231,7 @@ syntax specifications for trait bounds are used simultaneously."##, label: "clippy::transmute_int_to_float", description: r##"Checks for transmutes from an integer to a float."##, }, + Lint { label: "clippy::transmute_num_to_bytes", description: r##""## }, Lint { label: "clippy::transmute_ptr_to_ptr", description: r##"Checks for transmutes from a pointer to a pointer, or @@ -7256,6 +7294,12 @@ that is not equal to its label: "clippy::uninit_assumed_init", description: r##"Checks for `MaybeUninit::uninit().assume_init()`."##, }, + Lint { + label: "clippy::uninit_vec", + description: r##"Checks for `set_len()` call that creates `Vec` with uninitialized elements. +This is commonly caused by calling `set_len()` right after allocating or +reserving a buffer with `new()`, `default()`, `with_capacity()`, or `reserve()`."##, + }, Lint { label: "clippy::unit_arg", description: r##"Checks for passing a unit value as an argument to a function without using a diff --git a/crates/ide_db/src/tests/sourcegen_lints.rs b/crates/ide_db/src/tests/sourcegen_lints.rs index 96ddce54ba..3c37aa6cb9 100644 --- a/crates/ide_db/src/tests/sourcegen_lints.rs +++ b/crates/ide_db/src/tests/sourcegen_lints.rs @@ -1,6 +1,7 @@ //! Generates descriptors structure for unstable feature from Unstable Book use std::{borrow::Cow, fs, path::Path}; +use itertools::Itertools; use stdx::format_to; use test_utils::project_root; use xshell::cmd; @@ -43,39 +44,62 @@ pub struct Lint { } fn generate_lint_descriptor(buf: &mut String) { - let stdout = cmd!("rustc -W help").read().unwrap(); + // FIXME: rustdoc currently requires an input file for -Whelp cc https://github.com/rust-lang/rust/pull/88831 + let file = project_root().join(file!()); + let stdout = cmd!("rustdoc -W help {file}").read().unwrap(); let start_lints = stdout.find("---- ------- -------").unwrap(); let start_lint_groups = stdout.find("---- ---------").unwrap(); - let end_lints = stdout.find("Lint groups provided by rustc:").unwrap(); - let end_lint_groups = stdout - .find("Lint tools like Clippy can provide additional lints and lint groups.") - .unwrap(); + let start_lints_rustdoc = + stdout.find("Lint checks provided by plugins loaded by this crate:").unwrap(); + let start_lint_groups_rustdoc = + stdout.find("Lint groups provided by plugins loaded by this crate:").unwrap(); + buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#); buf.push('\n'); - let mut lints = stdout[start_lints..end_lints] - .lines() - .skip(1) - .filter(|l| !l.is_empty()) - .map(|line| { + + let lints = stdout[start_lints..].lines().skip(1).take_while(|l| !l.is_empty()).map(|line| { + let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap(); + let (_default_level, description) = rest.trim().split_once(char::is_whitespace).unwrap(); + (name.trim(), Cow::Borrowed(description.trim())) + }); + let lint_groups = + stdout[start_lint_groups..].lines().skip(1).take_while(|l| !l.is_empty()).map(|line| { + let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap(); + (name.trim(), format!("lint group for: {}", lints.trim()).into()) + }); + + lints.chain(lint_groups).sorted_by(|(ident, _), (ident2, _)| ident.cmp(ident2)).for_each( + |(name, description)| push_lint_completion(buf, &name.replace("-", "_"), &description), + ); + buf.push_str("];\n"); + + // rustdoc + + buf.push('\n'); + buf.push_str(r#"pub const RUSTDOC_LINTS: &[Lint] = &["#); + buf.push('\n'); + + let lints_rustdoc = + stdout[start_lints_rustdoc..].lines().skip(2).take_while(|l| !l.is_empty()).map(|line| { let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap(); let (_default_level, description) = rest.trim().split_once(char::is_whitespace).unwrap(); (name.trim(), Cow::Borrowed(description.trim())) - }) - .collect::>(); - lints.extend( - stdout[start_lint_groups..end_lint_groups].lines().skip(1).filter(|l| !l.is_empty()).map( + }); + let lint_groups_rustdoc = + stdout[start_lint_groups_rustdoc..].lines().skip(2).take_while(|l| !l.is_empty()).map( |line| { let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap(); (name.trim(), format!("lint group for: {}", lints.trim()).into()) }, - ), - ); + ); - lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2)); - lints.into_iter().for_each(|(name, description)| { - push_lint_completion(buf, &name.replace("-", "_"), &description) - }); + lints_rustdoc + .chain(lint_groups_rustdoc) + .sorted_by(|(ident, _), (ident2, _)| ident.cmp(ident2)) + .for_each(|(name, description)| { + push_lint_completion(buf, &name.replace("-", "_"), &description) + }); buf.push_str("];\n"); } @@ -126,8 +150,13 @@ fn generate_descriptor_clippy(buf: &mut String, path: &Path) { clippy_lints.push(clippy_lint) } else if let Some(line) = line.strip_prefix(r#""docs": ""#) { let prefix_to_strip = r#" ### What it does"#; - // FIXME: replace unwrap_or with expect again, currently there is one lint that uses a different format in the json... - let line = line.strip_prefix(prefix_to_strip).unwrap_or(line); + let line = match line.strip_prefix(prefix_to_strip) { + Some(line) => line, + None => { + eprintln!("unexpected clippy prefix for {}", clippy_lints.last().unwrap().id); + continue; + } + }; // Only take the description, any more than this is a lot of additional data we would embed into the exe // which seems unnecessary let up_to = line.find(r#"###"#).expect("no second section found?");