Function clippy_utils::diagnostics::span_lint_hir_and_then

source ·
pub fn span_lint_hir_and_then(
    cx: &LateContext<'_>,
    lint: &'static Lint,
    hir_id: HirId,
    sp: impl Into<MultiSpan>,
    msg: impl Into<DiagMessage>,
    f: impl FnOnce(&mut Diag<'_, ()>),
Expand description

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

This is in contrast to span_lint_and_then, 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.