@@ -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.
155174pub 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.
162200pub fn span_lint_hir_and_then (
163201 cx : & LateContext < ' _ > ,
164202 lint : & ' static Lint ,
0 commit comments