Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc keyword lint pass #79541

Merged
merged 3 commits into from
Dec 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion compiler/rustc_lint/src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -267,3 +267,47 @@ impl EarlyLintPass for LintPassImpl {
}
}
}

declare_tool_lint! {
pub rustc::EXISTING_DOC_KEYWORD,
Allow,
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
"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
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
}

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!");
lcnr marked this conversation as resolved.
Show resolved Hide resolved
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();
});
}
}
}
}
}
}
3 changes: 3 additions & 0 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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),
],
);
}
1 change: 1 addition & 0 deletions library/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@
all(target_vendor = "fortanix", target_env = "sgx"),
feature(slice_index_methods, coerce_unsized, sgx_platform)
)]
#![deny(rustc::existing_doc_keyword)]
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
#![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
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui-fulldeps/internal-lints/existing_doc_keyword.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: -Z unstable-options

#![feature(rustc_private)]
#![feature(doc_keyword)]

#![crate_type = "lib"]

#![deny(rustc::existing_doc_keyword)]

#[doc(keyword = "tadam")] //~ ERROR
mod tadam {}
15 changes: 15 additions & 0 deletions src/test/ui-fulldeps/internal-lints/existing_doc_keyword.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: Found non-existing keyword `tadam` used in `#[doc(keyword = "...")]`
--> $DIR/existing_doc_keyword.rs:10:1
|
LL | #[doc(keyword = "tadam")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
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