Skip to content

Commit fa4e3aa

Browse files
committed
add documentation to the span_lint_hir functions
1 parent 0b4b684 commit fa4e3aa

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

clippy.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
avoid-breaking-exported-api = false
22

3-
# use the various `span_lint_*` methods instead, which also add a link to the docs
3+
# use the various `clippy_utils::diagnostics::span_lint_*` functions instead, which also add a link to the docs
44
disallowed-methods = [
55
"rustc_lint::context::LintContext::span_lint",
6-
"rustc_middle::ty::context::TyCtxt::node_span_lint"
6+
"rustc_middle::ty::context::TyCtxt::node_span_lint",
77
]

clippy_utils/src/diagnostics.rs

+38
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,51 @@ where
152152
});
153153
}
154154

155+
/// Like [`span_lint`], but allows providing the `HirId` of the node that caused us to emit this
156+
/// lint.
157+
///
158+
/// The `HirId` is used for checking lint level attributes.
159+
///
160+
/// For example:
161+
/// ```ignore
162+
/// fn f() { /* '1 */
163+
///
164+
/// #[allow(clippy::some_lint)]
165+
/// let _x = /* '2 */;
166+
/// }
167+
/// ```
168+
/// If `some_lint` does its analysis in `LintPass::check_fn` (at `'1`) and emits a lint at `'2`
169+
/// using [`span_lint`], then allowing the lint at `'2` as attempted in the snippet will not work!
170+
/// Even though that is where the warning points at, which would be confusing to users.
171+
///
172+
/// Instead, use this function and also pass the `HirId` of the node at `'2`, which will let the
173+
/// compiler check lint level attributes at `'2` as one would expect, and the `#[allow]` will work.
155174
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
156175
#[expect(clippy::disallowed_methods)]
157176
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
158177
docs_link(diag, lint);
159178
});
160179
}
161180

181+
/// Like [`span_lint_and_then`], but allows providing the `HirId` of the node that caused us to emit
182+
/// this lint.
183+
///
184+
/// The `HirId` is used for checking lint level attributes.
185+
///
186+
/// For example:
187+
/// ```ignore
188+
/// fn f() { /* '1 */
189+
///
190+
/// #[allow(clippy::some_lint)]
191+
/// let _x = /* '2 */;
192+
/// }
193+
/// ```
194+
/// If `some_lint` does its analysis in `LintPass::check_fn` (at `'1`) and emits a lint at `'2`
195+
/// using [`span_lint_and_then`], then allowing the lint at `'2` as attempted in the snippet will
196+
/// not work! Even though that is where the warning points at, which would be confusing to users.
197+
///
198+
/// Instead, use this function and also pass the `HirId` of the node at `'2`, which will let the
199+
/// compiler check lint level attributes at `'2` as one would expect, and the `#[allow]` will work.
162200
pub fn span_lint_hir_and_then(
163201
cx: &LateContext<'_>,
164202
lint: &'static Lint,

0 commit comments

Comments
 (0)