Skip to content

Commit fc4890b

Browse files
authored
Unrolled build for #143306
Rollup merge of #143306 - samueltardieu:track-clippy-lints-emission, r=petrochenkov Add `track_caller` attributes to trace origin of Clippy lints This allows the use of `-Z track-diagnostics` to see the origin of Clippy lints emission, as is already the case for lints coming from rustc.
2 parents 6677875 + b4d35fd commit fc4890b

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

compiler/rustc_lint/src/context.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ pub trait LintContext {
504504
///
505505
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
506506
#[rustc_lint_diagnostics]
507+
#[track_caller]
507508
fn opt_span_lint<S: Into<MultiSpan>>(
508509
&self,
509510
lint: &'static Lint,
@@ -542,6 +543,7 @@ pub trait LintContext {
542543
///
543544
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
544545
#[rustc_lint_diagnostics]
546+
#[track_caller]
545547
fn span_lint<S: Into<MultiSpan>>(
546548
&self,
547549
lint: &'static Lint,

src/tools/clippy/clippy_utils/src/diagnostics.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ fn validate_diag(diag: &Diag<'_, impl EmissionGuarantee>) {
9898
/// 17 | std::mem::forget(seven);
9999
/// | ^^^^^^^^^^^^^^^^^^^^^^^
100100
/// ```
101+
#[track_caller]
101102
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: impl Into<DiagMessage>) {
102103
#[expect(clippy::disallowed_methods)]
103104
cx.span_lint(lint, sp, |diag| {
@@ -143,6 +144,7 @@ pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<Mult
143144
/// |
144145
/// = help: consider using `f64::NAN` if you would like a constant representing NaN
145146
/// ```
147+
#[track_caller]
146148
pub fn span_lint_and_help<T: LintContext>(
147149
cx: &T,
148150
lint: &'static Lint,
@@ -203,6 +205,7 @@ pub fn span_lint_and_help<T: LintContext>(
203205
/// 10 | forget(&SomeStruct);
204206
/// | ^^^^^^^^^^^
205207
/// ```
208+
#[track_caller]
206209
pub fn span_lint_and_note<T: LintContext>(
207210
cx: &T,
208211
lint: &'static Lint,
@@ -244,6 +247,7 @@ pub fn span_lint_and_note<T: LintContext>(
244247
/// If you're unsure which function you should use, you can test if the `#[expect]` attribute works
245248
/// where you would expect it to.
246249
/// If it doesn't, you likely need to use [`span_lint_hir_and_then`] instead.
250+
#[track_caller]
247251
pub fn span_lint_and_then<C, S, M, F>(cx: &C, lint: &'static Lint, sp: S, msg: M, f: F)
248252
where
249253
C: LintContext,
@@ -286,6 +290,7 @@ where
286290
/// Instead, use this function and also pass the `HirId` of `<expr_1>`, which will let
287291
/// the compiler check lint level attributes at the place of the expression and
288292
/// the `#[allow]` will work.
293+
#[track_caller]
289294
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: impl Into<DiagMessage>) {
290295
#[expect(clippy::disallowed_methods)]
291296
cx.tcx.node_span_lint(lint, hir_id, sp, |diag| {
@@ -321,6 +326,7 @@ pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, s
321326
/// Instead, use this function and also pass the `HirId` of `<expr_1>`, which will let
322327
/// the compiler check lint level attributes at the place of the expression and
323328
/// the `#[allow]` will work.
329+
#[track_caller]
324330
pub fn span_lint_hir_and_then(
325331
cx: &LateContext<'_>,
326332
lint: &'static Lint,
@@ -374,6 +380,7 @@ pub fn span_lint_hir_and_then(
374380
/// = note: `-D fold-any` implied by `-D warnings`
375381
/// ```
376382
#[cfg_attr(not(debug_assertions), expect(clippy::collapsible_span_lint_calls))]
383+
#[track_caller]
377384
pub fn span_lint_and_sugg<T: LintContext>(
378385
cx: &T,
379386
lint: &'static Lint,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@compile-flags: -Z track-diagnostics
2+
//@no-rustfix
3+
4+
// Normalize the emitted location so this doesn't need
5+
// updating everytime someone adds or removes a line.
6+
//@normalize-stderr-test: ".rs:\d+:\d+" -> ".rs:LL:CC"
7+
8+
#![warn(clippy::let_and_return, clippy::unnecessary_cast)]
9+
10+
fn main() {
11+
// Check the provenance of a lint sent through `LintContext::span_lint()`
12+
let a = 3u32;
13+
let b = a as u32;
14+
//~^ unnecessary_cast
15+
16+
// Check the provenance of a lint sent through `TyCtxt::node_span_lint()`
17+
let c = {
18+
let d = 42;
19+
d
20+
//~^ let_and_return
21+
};
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: casting to the same type is unnecessary (`u32` -> `u32`)
2+
--> tests/ui/track-diagnostics-clippy.rs:LL:CC
3+
|
4+
LL | let b = a as u32;
5+
| ^^^^^^^^ help: try: `a`
6+
-Ztrack-diagnostics: created at src/tools/clippy/clippy_lints/src/casts/unnecessary_cast.rs:LL:CC
7+
|
8+
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_cast)]`
10+
11+
error: returning the result of a `let` binding from a block
12+
--> tests/ui/track-diagnostics-clippy.rs:LL:CC
13+
|
14+
LL | let d = 42;
15+
| ----------- unnecessary `let` binding
16+
LL | d
17+
| ^
18+
-Ztrack-diagnostics: created at src/tools/clippy/clippy_lints/src/returns.rs:LL:CC
19+
|
20+
= note: `-D clippy::let-and-return` implied by `-D warnings`
21+
= help: to override `-D warnings` add `#[allow(clippy::let_and_return)]`
22+
help: return the expression directly
23+
|
24+
LL ~
25+
LL ~ 42
26+
|
27+
28+
error: aborting due to 2 previous errors
29+

0 commit comments

Comments
 (0)