Skip to content

Commit 210a1bc

Browse files
authored
Rollup merge of rust-lang#132221 - ehuss:lint-docs, r=compiler-errors
Clean up some comments on lint implementation This updates some doc comments that have gotten very out of date. Some of these macros were removed or renamed in rust-lang#57726 and rust-lang#104863 and others. Manual emitting of lints was significantly reworked when the `Diagnostic` infrastructure was added. Rather than try to replicate the high-level documentation, I added pointers to the rustc-dev-guide. I linkified some types so that if they are renamed/removed without updating the docs, it will break CI.
2 parents 0410ecc + 924ded6 commit 210a1bc

File tree

5 files changed

+18
-66
lines changed

5 files changed

+18
-66
lines changed

compiler/rustc_lint/src/builtin.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,14 @@
44
//! AST visitor. Also see `rustc_session::lint::builtin`, which contains the
55
//! definitions of lints that are emitted directly inside the main compiler.
66
//!
7-
//! To add a new lint to rustc, declare it here using `declare_lint!()`.
7+
//! To add a new lint to rustc, declare it here using [`declare_lint!`].
88
//! Then add code to emit the new lint in the appropriate circumstances.
9-
//! You can do that in an existing `LintPass` if it makes sense, or in a
10-
//! new `LintPass`, or using `Session::add_lint` elsewhere in the
11-
//! compiler. Only do the latter if the check can't be written cleanly as a
12-
//! `LintPass` (also, note that such lints will need to be defined in
13-
//! `rustc_session::lint::builtin`, not here).
149
//!
15-
//! If you define a new `EarlyLintPass`, you will also need to add it to the
16-
//! `add_early_builtin!` or `add_early_builtin_with_new!` invocation in
17-
//! `lib.rs`. Use the former for unit-like structs and the latter for structs
18-
//! with a `pub fn new()`.
10+
//! If you define a new [`EarlyLintPass`], you will also need to add it to the
11+
//! [`crate::early_lint_methods!`] invocation in `lib.rs`.
1912
//!
20-
//! If you define a new `LateLintPass`, you will also need to add it to the
21-
//! `late_lint_methods!` invocation in `lib.rs`.
13+
//! If you define a new [`LateLintPass`], you will also need to add it to the
14+
//! [`crate::late_lint_methods!`] invocation in `lib.rs`.
2215
2316
use std::fmt::Write;
2417

compiler/rustc_lint/src/context.rs

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
//! Implementation of lint checking.
1+
//! Basic types for managing and implementing lints.
22
//!
3-
//! The lint checking is mostly consolidated into one pass which runs
4-
//! after all other analyses. Throughout compilation, lint warnings
5-
//! can be added via the `add_lint` method on the Session structure. This
6-
//! requires a span and an ID of the node that the lint is being added to. The
7-
//! lint isn't actually emitted at that time because it is unknown what the
8-
//! actual lint level at that location is.
9-
//!
10-
//! To actually emit lint warnings/errors, a separate pass is used.
11-
//! A context keeps track of the current state of all lint levels.
12-
//! Upon entering a node of the ast which can modify the lint settings, the
13-
//! previous lint state is pushed onto a stack and the ast is then recursed
14-
//! upon. As the ast is traversed, this keeps track of the current lint level
15-
//! for all lint attributes.
3+
//! See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for an
4+
//! overview of how lints are implemented.
165
176
use std::cell::Cell;
187
use std::{iter, slice};
@@ -52,9 +41,6 @@ type LateLintPassFactory =
5241
dyn for<'tcx> Fn(TyCtxt<'tcx>) -> LateLintPassObject<'tcx> + sync::DynSend + sync::DynSync;
5342

5443
/// Information about the registered lints.
55-
///
56-
/// This is basically the subset of `Context` that we can
57-
/// build early in the compile pipeline.
5844
pub struct LintStore {
5945
/// Registered lints.
6046
lints: Vec<&'static Lint>,

compiler/rustc_lint/src/early.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
//! Implementation of lint checking.
1+
//! Implementation of the early lint pass.
22
//!
3-
//! The lint checking is mostly consolidated into one pass which runs
4-
//! after all other analyses. Throughout compilation, lint warnings
5-
//! can be added via the `add_lint` method on the Session structure. This
6-
//! requires a span and an ID of the node that the lint is being added to. The
7-
//! lint isn't actually emitted at that time because it is unknown what the
8-
//! actual lint level at that location is.
9-
//!
10-
//! To actually emit lint warnings/errors, a separate pass is used.
11-
//! A context keeps track of the current state of all lint levels.
12-
//! Upon entering a node of the ast which can modify the lint settings, the
13-
//! previous lint state is pushed onto a stack and the ast is then recursed
14-
//! upon. As the ast is traversed, this keeps track of the current lint level
15-
//! for all lint attributes.
3+
//! The early lint pass works on AST nodes after macro expansion and name
4+
//! resolution, just before AST lowering. These lints are for purely
5+
//! syntactical lints.
166
177
use rustc_ast::ptr::P;
188
use rustc_ast::visit::{self as ast_visit, Visitor, walk_list};

compiler/rustc_lint/src/late.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
1-
//! Implementation of lint checking.
1+
//! Implementation of the late lint pass.
22
//!
3-
//! The lint checking is mostly consolidated into one pass which runs
4-
//! after all other analyses. Throughout compilation, lint warnings
5-
//! can be added via the `add_lint` method on the Session structure. This
6-
//! requires a span and an ID of the node that the lint is being added to. The
7-
//! lint isn't actually emitted at that time because it is unknown what the
8-
//! actual lint level at that location is.
9-
//!
10-
//! To actually emit lint warnings/errors, a separate pass is used.
11-
//! A context keeps track of the current state of all lint levels.
12-
//! Upon entering a node of the ast which can modify the lint settings, the
13-
//! previous lint state is pushed onto a stack and the ast is then recursed
14-
//! upon. As the ast is traversed, this keeps track of the current lint level
15-
//! for all lint attributes.
3+
//! The late lint pass Works on HIR nodes, towards the end of analysis (after
4+
//! borrow checking, etc.). These lints have full type information available.
165
176
use std::any::Any;
187
use std::cell::Cell;

compiler/rustc_lint/src/lib.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,14 @@
66
//! other phases of the compiler, which are generally required to hold in order
77
//! to compile the program at all.
88
//!
9-
//! Most lints can be written as [LintPass] instances. These run after
9+
//! Most lints can be written as [`LintPass`] instances. These run after
1010
//! all other analyses. The `LintPass`es built into rustc are defined
1111
//! within [rustc_session::lint::builtin],
1212
//! which has further comments on how to add such a lint.
1313
//! rustc can also load external lint plugins, as is done for Clippy.
1414
//!
15-
//! Some of rustc's lints are defined elsewhere in the compiler and work by
16-
//! calling `add_lint()` on the overall `Session` object. This works when
17-
//! it happens before the main lint pass, which emits the lints stored by
18-
//! `add_lint()`. To emit lints after the main lint pass (from codegen, for
19-
//! example) requires more effort. See `emit_lint` and `GatherNodeLevels`
20-
//! in `context.rs`.
21-
//!
22-
//! Some code also exists in [rustc_session::lint], [rustc_middle::lint].
15+
//! See <https://rustc-dev-guide.rust-lang.org/diagnostics.html> for an
16+
//! overview of how lints are implemented.
2317
//!
2418
//! ## Note
2519
//!

0 commit comments

Comments
 (0)