From 56c64f871ec25468c55345645713032b615326b1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 29 Nov 2020 22:34:41 +0100 Subject: [PATCH 1/3] Add lint pass for doc keyword --- compiler/rustc_lint/src/internal.rs | 46 ++++++++++++++++++++++++++++- compiler/rustc_lint/src/lib.rs | 3 ++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index c2d98b8e4ad37..b226572703256 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -10,7 +10,7 @@ use rustc_hir::{GenericArg, HirId, MutTy, Mutability, Path, PathSegment, QPath, use rustc_middle::ty; use rustc_session::{declare_lint_pass, declare_tool_lint, impl_lint_pass}; use rustc_span::hygiene::{ExpnKind, MacroKind}; -use rustc_span::symbol::{sym, Ident, Symbol}; +use rustc_span::symbol::{kw, sym, Ident, Symbol}; declare_tool_lint! { pub rustc::DEFAULT_HASH_TYPES, @@ -267,3 +267,47 @@ impl EarlyLintPass for LintPassImpl { } } } + +declare_tool_lint! { + pub rustc::EXISTING_DOC_KEYWORD, + Deny, + "Check that documented keywords in std and core actually exist", + report_in_external_macro: true +} + +declare_lint_pass!(ExistingDocKeyword => [EXISTING_DOC_KEYWORD]); + +fn is_doc_keyword(s: Symbol) -> bool { + s <= kw::Union +} + +impl<'tcx> LateLintPass<'tcx> for ExistingDocKeyword { + fn check_item(&mut self, cx: &LateContext<'_>, item: &rustc_hir::Item<'_>) { + for attr in item.attrs { + if !attr.has_name(sym::doc) { + continue; + } + if let Some(list) = attr.meta_item_list() { + for nested in list { + if nested.has_name(sym::keyword) { + let v = nested + .value_str() + .expect("#[doc(keyword = \"...\")] expected a value!"); + if is_doc_keyword(v) { + return; + } + cx.struct_span_lint(EXISTING_DOC_KEYWORD, attr.span, |lint| { + lint.build(&format!( + "Found non-existing keyword `{}` used in \ + `#[doc(keyword = \"...\")]`", + v, + )) + .help("only existing keywords are allowed in core/std") + .emit(); + }); + } + } + } + } + } +} diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 81549be4b0915..80ef855c3859e 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -463,6 +463,8 @@ fn register_internals(store: &mut LintStore) { store.register_early_pass(|| box DefaultHashTypes::new()); store.register_lints(&LintPassImpl::get_lints()); store.register_early_pass(|| box LintPassImpl); + store.register_lints(&ExistingDocKeyword::get_lints()); + store.register_late_pass(|| box ExistingDocKeyword); store.register_lints(&TyTyKind::get_lints()); store.register_late_pass(|| box TyTyKind); store.register_group( @@ -475,6 +477,7 @@ fn register_internals(store: &mut LintStore) { LintId::of(LINT_PASS_IMPL_WITHOUT_MACRO), LintId::of(TY_PASS_BY_REFERENCE), LintId::of(USAGE_OF_QUALIFIED_TY), + LintId::of(EXISTING_DOC_KEYWORD), ], ); } From 0105e4a54b2ccbfbd62b0d07f636520b39094f5e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 3 Dec 2020 14:05:58 +0100 Subject: [PATCH 2/3] Add test for EXISTING_DOC_KEYWORD internal lint --- .../internal-lints/existing_doc_keyword.rs | 9 +++++++++ .../internal-lints/existing_doc_keyword.stderr | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs create mode 100644 src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr diff --git a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs new file mode 100644 index 0000000000000..2b4a65b4a4c50 --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs @@ -0,0 +1,9 @@ +// compile-flags: -Z unstable-options + +#![feature(rustc_private)] +#![feature(doc_keyword)] + +#![crate_type = "lib"] + +#[doc(keyword = "tadam")] //~ ERROR +mod tadam {} diff --git a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr new file mode 100644 index 0000000000000..6fc288ae43453 --- /dev/null +++ b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr @@ -0,0 +1,11 @@ +error: Found non-existing keyword `tadam` used in `#[doc(keyword = "...")]` + --> $DIR/existing_doc_keyword.rs:8:1 + | +LL | #[doc(keyword = "tadam")] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `#[deny(rustc::existing_doc_keyword)]` on by default + = help: only existing keywords are allowed in core/std + +error: aborting due to previous error + From 50eb3a89f8e2a4c1d809d2b0170b0063b9338d6d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 3 Dec 2020 15:32:41 +0100 Subject: [PATCH 3/3] Only deny doc_keyword in std and set it as "allow" by default --- compiler/rustc_lint/src/internal.rs | 2 +- library/std/src/lib.rs | 1 + .../ui-fulldeps/internal-lints/existing_doc_keyword.rs | 2 ++ .../internal-lints/existing_doc_keyword.stderr | 8 ++++++-- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_lint/src/internal.rs b/compiler/rustc_lint/src/internal.rs index b226572703256..af5972c6c81c7 100644 --- a/compiler/rustc_lint/src/internal.rs +++ b/compiler/rustc_lint/src/internal.rs @@ -270,7 +270,7 @@ impl EarlyLintPass for LintPassImpl { declare_tool_lint! { pub rustc::EXISTING_DOC_KEYWORD, - Deny, + Allow, "Check that documented keywords in std and core actually exist", report_in_external_macro: true } diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 83eb847697dd4..6c240cb4c3ed9 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -212,6 +212,7 @@ all(target_vendor = "fortanix", target_env = "sgx"), feature(slice_index_methods, coerce_unsized, sgx_platform) )] +#![deny(rustc::existing_doc_keyword)] #![cfg_attr(all(test, target_vendor = "fortanix", target_env = "sgx"), feature(fixed_size_array))] // std is implemented with unstable features, many of which are internal // compiler details that will never be stable diff --git a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs index 2b4a65b4a4c50..053712a4b4ee6 100644 --- a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs +++ b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs @@ -5,5 +5,7 @@ #![crate_type = "lib"] +#![deny(rustc::existing_doc_keyword)] + #[doc(keyword = "tadam")] //~ ERROR mod tadam {} diff --git a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr index 6fc288ae43453..bac44f338b74c 100644 --- a/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr +++ b/src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr @@ -1,10 +1,14 @@ error: Found non-existing keyword `tadam` used in `#[doc(keyword = "...")]` - --> $DIR/existing_doc_keyword.rs:8:1 + --> $DIR/existing_doc_keyword.rs:10:1 | LL | #[doc(keyword = "tadam")] | ^^^^^^^^^^^^^^^^^^^^^^^^^ | - = note: `#[deny(rustc::existing_doc_keyword)]` on by default +note: the lint level is defined here + --> $DIR/existing_doc_keyword.rs:8:9 + | +LL | #![deny(rustc::existing_doc_keyword)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ = help: only existing keywords are allowed in core/std error: aborting due to previous error