Skip to content

Commit 56c64f8

Browse files
Add lint pass for doc keyword
1 parent c7cff21 commit 56c64f8

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

compiler/rustc_lint/src/internal.rs

Lines changed: 45 additions & 1 deletion
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+
Deny,
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

Lines changed: 3 additions & 0 deletions
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
}

0 commit comments

Comments
 (0)