Skip to content

Commit f406052

Browse files
authored
Rollup merge of #79541 - GuillaumeGomez:doc-keyword-lint-pass, r=lcnr
Doc keyword lint pass `x.py test` doesn't seem to work locally for multiple reasons so simpler to just run CI...
2 parents 6f2fbc1 + 50eb3a8 commit f406052

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

compiler/rustc_lint/src/internal.rs

+45-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath,
1010
use rustc_middle::ty;
1111
use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass};
1212
use rustc_span::hygiene::{ExpnKind, MacroKind};
13-
use rustc_span::symbol::{sym, Ident, Symbol};
13+
use rustc_span::symbol::{kw, sym, Ident, Symbol};
1414

1515
declare_tool_lint! {
1616
pub rustc::DEFAULT_HASH_TYPES,
@@ -267,3 +267,47 @@ impl EarlyLintPass for LintPassImpl {
267267
}
268268
}
269269
}
270+
271+
declare_tool_lint! {
272+
pub rustc::EXISTING_DOC_KEYWORD,
273+
Allow,
274+
"Check that documented keywords in std and core actually exist",
275+
report_in_external_macro: true
276+
}
277+
278+
declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]);
279+
280+
fn is_doc_keyword(s: Symbol) -> bool {
281+
s <= kw::Union
282+
}
283+
284+
impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword {
285+
fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) {
286+
for attr in item.attrs {
287+
if !attr.has_name(sym::doc) {
288+
continue;
289+
}
290+
if let Some(list) = attr.meta_item_list() {
291+
for nested in list {
292+
if nested.has_name(sym::keyword) {
293+
let v = nested
294+
.value_str()
295+
.expect("#[doc(keyword = \"...\")] expected a value!");
296+
if is_doc_keyword(v) {
297+
return;
298+
}
299+
cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| {
300+
lint.build(&format!(
301+
"Found non-existing keyword `{}` used in \
302+
`#[doc(keyword = \"...\")]`",
303+
v,
304+
))
305+
.help("only existing keywords are allowed in core/std")
306+
.emit();
307+
});
308+
}
309+
}
310+
}
311+
}
312+
}
313+
}

compiler/rustc_lint/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,8 @@ fn register_internals(store: &mut LintStore) {
463463
store.register_early_pass(|| box DefaultHashTypes::new());
464464
store.register_lints(&LintPassImpl::get_lints());
465465
store.register_early_pass(|| box LintPassImpl);
466+
store.register_lints(&ExistingDocKeyword::get_lints());
467+
store.register_late_pass(|| box ExistingDocKeyword);
466468
store.register_lints(&TyTyKind::get_lints());
467469
store.register_late_pass(|| box TyTyKind);
468470
store.register_group(
@@ -475,6 +477,7 @@ fn register_internals(store: &mut LintStore) {
475477
LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO),
476478
LintId::of(TY_PASS_BY_REFERENCE),
477479
LintId::of(USAGE_OF_QUALIFIED_TY),
480+
LintId::of(EXISTING_DOC_KEYWORD),
478481
],
479482
);
480483
}

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@
212212
all(target_vendor = "fortanix", target_env = "sgx"),
213213
feature(slice_index_methods, coerce_unsized, sgx_platform)
214214
)]
215+
#![deny(rustc::existing_doc_keyword)]
215216
#![cfg_attr(all(test, target_vendor = "fortanix", target_env = "sgx"), feature(fixed_size_array))]
216217
// std is implemented with unstable features, many of which are internal
217218
// compiler details that will never be stable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: -Z unstable-options
2+
3+
#![feature(rustc_private)]
4+
#![feature(doc_keyword)]
5+
6+
#![crate_type = "lib"]
7+
8+
#![deny(rustc::existing_doc_keyword)]
9+
10+
#[doc(keyword = "tadam")] //~ ERROR
11+
mod tadam {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Found non-existing keyword `tadam` used in `#[doc(keyword = "...")]`
2+
--> $DIR/existing_doc_keyword.rs:10:1
3+
|
4+
LL | #[doc(keyword = "tadam")]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/existing_doc_keyword.rs:8:9
9+
|
10+
LL | #![deny(rustc::existing_doc_keyword)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
= help: only existing keywords are allowed in core/std
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)