Skip to content

Commit c73aa7a

Browse files
bors[bot]Veykril
andauthored
Merge #10594
10594: fix: Generate and complete rustdoc lints r=Veykril a=Veykril Fixes #10572, #8349 bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 5051717 + bed6eae commit c73aa7a

File tree

6 files changed

+246
-158
lines changed

6 files changed

+246
-158
lines changed

crates/ide_completion/src/completions/attribute.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! for built-in attributes.
55
66
use hir::HasAttrs;
7-
use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES};
7+
use ide_db::helpers::generated_lints::{CLIPPY_LINTS, DEFAULT_LINTS, FEATURES, RUSTDOC_LINTS};
88
use itertools::Itertools;
99
use once_cell::sync::Lazy;
1010
use rustc_hash::FxHashMap;
@@ -29,12 +29,16 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
2929
};
3030
match (name_ref, attribute.token_tree()) {
3131
(Some(path), Some(token_tree)) => match path.text().as_str() {
32-
"derive" => derive::complete_derive(acc, ctx, token_tree),
3332
"repr" => repr::complete_repr(acc, ctx, token_tree),
34-
"feature" => lint::complete_lint(acc, ctx, token_tree, FEATURES),
33+
"derive" => derive::complete_derive(acc, ctx, &parse_comma_sep_paths(token_tree)?),
34+
"feature" => {
35+
lint::complete_lint(acc, ctx, &parse_comma_sep_paths(token_tree)?, FEATURES)
36+
}
3537
"allow" | "warn" | "deny" | "forbid" => {
36-
lint::complete_lint(acc, ctx, token_tree.clone(), DEFAULT_LINTS);
37-
lint::complete_lint(acc, ctx, token_tree, CLIPPY_LINTS);
38+
let existing_lints = parse_comma_sep_paths(token_tree)?;
39+
lint::complete_lint(acc, ctx, &existing_lints, DEFAULT_LINTS);
40+
lint::complete_lint(acc, ctx, &existing_lints, CLIPPY_LINTS);
41+
lint::complete_lint(acc, ctx, &existing_lints, RUSTDOC_LINTS);
3842
}
3943
"cfg" => {
4044
cfg::complete_cfg(acc, ctx);

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

+45-48
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,57 @@ use crate::{
1414
pub(super) fn complete_derive(
1515
acc: &mut Completions,
1616
ctx: &CompletionContext,
17-
derive_input: ast::TokenTree,
17+
existing_derives: &[ast::Path],
1818
) {
19-
if let Some(existing_derives) = super::parse_comma_sep_paths(derive_input.clone()) {
20-
let core = FamousDefs(&ctx.sema, ctx.krate).core();
21-
let existing_derives: FxHashSet<_> = existing_derives
22-
.into_iter()
23-
.filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path))
24-
.filter(|mac| mac.kind() == MacroKind::Derive)
25-
.collect();
19+
let core = FamousDefs(&ctx.sema, ctx.krate).core();
20+
let existing_derives: FxHashSet<_> = existing_derives
21+
.into_iter()
22+
.filter_map(|path| ctx.scope.speculative_resolve_as_mac(&path))
23+
.filter(|mac| mac.kind() == MacroKind::Derive)
24+
.collect();
2625

27-
for (name, mac) in get_derives_in_scope(ctx) {
28-
if existing_derives.contains(&mac) {
29-
continue;
30-
}
26+
for (name, mac) in get_derives_in_scope(ctx) {
27+
if existing_derives.contains(&mac) {
28+
continue;
29+
}
3130

32-
let name = name.to_smol_str();
33-
let label;
34-
let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
35-
// show derive dependencies for `core`/`std` derives
36-
Some((core, mac_krate)) if core == mac_krate => {
37-
if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
38-
.iter()
39-
.find(|derive_completion| derive_completion.label == name)
40-
{
41-
let mut components = vec![derive_completion.label];
42-
components.extend(derive_completion.dependencies.iter().filter(
43-
|&&dependency| {
44-
!existing_derives
45-
.iter()
46-
.filter_map(|it| it.name(ctx.db))
47-
.any(|it| it.to_smol_str() == dependency)
48-
},
49-
));
50-
let lookup = components.join(", ");
51-
label = components.iter().rev().join(", ");
52-
(label.as_str(), Some(lookup))
53-
} else {
54-
(&*name, None)
55-
}
31+
let name = name.to_smol_str();
32+
let label;
33+
let (label, lookup) = match core.zip(mac.module(ctx.db).map(|it| it.krate())) {
34+
// show derive dependencies for `core`/`std` derives
35+
Some((core, mac_krate)) if core == mac_krate => {
36+
if let Some(derive_completion) = DEFAULT_DERIVE_DEPENDENCIES
37+
.iter()
38+
.find(|derive_completion| derive_completion.label == name)
39+
{
40+
let mut components = vec![derive_completion.label];
41+
components.extend(derive_completion.dependencies.iter().filter(
42+
|&&dependency| {
43+
!existing_derives
44+
.iter()
45+
.filter_map(|it| it.name(ctx.db))
46+
.any(|it| it.to_smol_str() == dependency)
47+
},
48+
));
49+
let lookup = components.join(", ");
50+
label = components.iter().rev().join(", ");
51+
(label.as_str(), Some(lookup))
52+
} else {
53+
(&*name, None)
5654
}
57-
_ => (&*name, None),
58-
};
59-
60-
let mut item =
61-
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
62-
item.kind(CompletionItemKind::Attribute);
63-
if let Some(docs) = mac.docs(ctx.db) {
64-
item.documentation(docs);
6555
}
66-
if let Some(lookup) = lookup {
67-
item.lookup_by(lookup);
68-
}
69-
item.add_to(acc);
56+
_ => (&*name, None),
57+
};
58+
59+
let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
60+
item.kind(CompletionItemKind::Attribute);
61+
if let Some(docs) = mac.docs(ctx.db) {
62+
item.documentation(docs);
63+
}
64+
if let Some(lookup) = lookup {
65+
item.lookup_by(lookup);
7066
}
67+
item.add_to(acc);
7168
}
7269
}
7370

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

+46-50
Original file line numberDiff line numberDiff line change
@@ -11,60 +11,56 @@ use crate::{
1111
pub(super) fn complete_lint(
1212
acc: &mut Completions,
1313
ctx: &CompletionContext,
14-
derive_input: ast::TokenTree,
14+
existing_lints: &[ast::Path],
1515
lints_completions: &[Lint],
1616
) {
17-
if let Some(existing_lints) = super::parse_comma_sep_paths(derive_input) {
18-
for &Lint { label, description } in lints_completions {
19-
let (qual, name) = {
20-
// FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead?
21-
let mut parts = label.split("::");
22-
let ns_or_label = match parts.next() {
23-
Some(it) => it,
24-
None => continue,
25-
};
26-
let label = parts.next();
27-
match label {
28-
Some(label) => (Some(ns_or_label), label),
29-
None => (None, ns_or_label),
30-
}
17+
let is_qualified = ctx.previous_token_is(T![:]);
18+
for &Lint { label, description } in lints_completions {
19+
let (qual, name) = {
20+
// FIXME: change `Lint`'s label to not store a path in it but split the prefix off instead?
21+
let mut parts = label.split("::");
22+
let ns_or_label = match parts.next() {
23+
Some(it) => it,
24+
None => continue,
3125
};
32-
let lint_already_annotated = existing_lints
33-
.iter()
34-
.filter_map(|path| {
35-
let q = path.qualifier();
36-
if q.as_ref().and_then(|it| it.qualifier()).is_some() {
37-
return None;
38-
}
39-
Some((q.and_then(|it| it.as_single_name_ref()), path.segment()?.name_ref()?))
40-
})
41-
.any(|(q, name_ref)| {
42-
let qualifier_matches = match (q, qual) {
43-
(None, None) => true,
44-
(None, Some(_)) => false,
45-
(Some(_), None) => false,
46-
(Some(q), Some(ns)) => q.text() == ns,
47-
};
48-
qualifier_matches && name_ref.text() == name
49-
});
50-
if lint_already_annotated {
51-
continue;
26+
let label = parts.next();
27+
match label {
28+
Some(label) => (Some(ns_or_label), label),
29+
None => (None, ns_or_label),
5230
}
53-
let insert = match (qual, ctx.previous_token_is(T![:])) {
54-
(Some(qual), false) => format!("{}::{}", qual, name),
55-
// user is completing a qualified path but this completion has no qualifier
56-
// so discard this completion
57-
// FIXME: This is currently very hacky and will propose odd completions if
58-
// we add more qualified (tool) completions other than clippy
59-
(None, true) => continue,
60-
_ => name.to_owned(),
61-
};
62-
let mut item =
63-
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
64-
item.kind(CompletionItemKind::Attribute)
65-
.insert_text(insert)
66-
.documentation(hir::Documentation::new(description.to_owned()));
67-
item.add_to(acc)
31+
};
32+
if qual.is_none() && is_qualified {
33+
// qualified completion requested, but this lint is unqualified
34+
continue;
35+
}
36+
let lint_already_annotated = existing_lints
37+
.iter()
38+
.filter_map(|path| {
39+
let q = path.qualifier();
40+
if q.as_ref().and_then(|it| it.qualifier()).is_some() {
41+
return None;
42+
}
43+
Some((q.and_then(|it| it.as_single_name_ref()), path.segment()?.name_ref()?))
44+
})
45+
.any(|(q, name_ref)| {
46+
let qualifier_matches = match (q, qual) {
47+
(None, None) => true,
48+
(None, Some(_)) => false,
49+
(Some(_), None) => false,
50+
(Some(q), Some(ns)) => q.text() == ns,
51+
};
52+
qualifier_matches && name_ref.text() == name
53+
});
54+
if lint_already_annotated {
55+
continue;
6856
}
57+
let label = match qual {
58+
Some(qual) if !is_qualified => format!("{}::{}", qual, name),
59+
_ => name.to_owned(),
60+
};
61+
let mut item = CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
62+
item.kind(CompletionItemKind::Attribute)
63+
.documentation(hir::Documentation::new(description.to_owned()));
64+
item.add_to(acc)
6965
}
7066
}

crates/ide_completion/src/tests/attribute.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -693,11 +693,29 @@ mod lint {
693693
#[test]
694694
fn lint_clippy_qualified() {
695695
check_edit(
696-
"clippy::as_conversions",
696+
"as_conversions",
697697
r#"#[allow(clippy::$0)] struct Test;"#,
698698
r#"#[allow(clippy::as_conversions)] struct Test;"#,
699699
);
700700
}
701+
702+
#[test]
703+
fn lint_rustdoc_unqualified() {
704+
check_edit(
705+
"rustdoc::bare_urls",
706+
r#"#[allow($0)] struct Test;"#,
707+
r#"#[allow(rustdoc::bare_urls)] struct Test;"#,
708+
);
709+
}
710+
711+
#[test]
712+
fn lint_rustdoc_qualified() {
713+
check_edit(
714+
"bare_urls",
715+
r#"#[allow(rustdoc::$0)] struct Test;"#,
716+
r#"#[allow(rustdoc::bare_urls)] struct Test;"#,
717+
);
718+
}
701719
}
702720

703721
mod repr {

0 commit comments

Comments
 (0)