Function clippy_utils::diagnostics::span_lint_hir

source ·
pub fn span_lint_hir(
    cx: &LateContext<'_>,
    lint: &'static Lint,
    hir_id: HirId,
    sp: Span,
    msg: impl Into<DiagMessage>,
Expand description

Like span_lint, but emits the lint at the node identified by the given HirId.

This is in contrast to span_lint, which always emits the lint at the node that was last passed to the LintPass::check_* function.

The HirId is used for checking lint level attributes and to fulfill lint expectations defined via the #[expect] attribute.

For example:

fn f() { /* <node_1> */

    let _x = /* <expr_1> */;

If some_lint does its analysis in LintPass::check_fn (at <node_1>) and emits a lint at <expr_1> using span_lint, then allowing the lint at <expr_1> as attempted in the snippet will not work! Even though that is where the warning points at, which would be confusing to users.

Instead, use this function and also pass the HirId of <expr_1>, which will let the compiler check lint level attributes at the place of the expression and the #[allow] will work.