Skip to content

Commit 662ea3f

Browse files
committed
Auto merge of rust-lang#11811 - y21:avoid_cx_struct_span_lint, r=Manishearth
disallow calls to `LintContext::struct_span_lint` and `TyCtxt::struct_span_lint_hir` `LintContext::struct_span_lint` and `TyCtxt::struct_span_lint_hir` don't show the link to the clippy documentation, see: rust-lang#11805 In rust-lang#11810, the last few calls to those methods were replaced with `span_lint_*`. It seems like we should just disallow them altogether so that no new code tries to use them. The existing `disallowed_methods` lint makes this easy. changelog: none
2 parents 783b914 + 19eec0b commit 662ea3f

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

clippy.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
11
avoid-breaking-exported-api = false
2+
3+
# use the various `span_lint_*` methods instead, which also add a link to the docs
4+
disallowed-methods = [
5+
"rustc_lint::context::LintContext::struct_span_lint",
6+
"rustc_middle::ty::context::TyCtxt::struct_span_lint_hir"
7+
]

clippy_utils/src/diagnostics.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) {
4646
/// | ^^^^^^^^^^^^^^^^^^^^^^^
4747
/// ```
4848
pub fn span_lint<T: LintContext>(cx: &T, lint: &'static Lint, sp: impl Into<MultiSpan>, msg: &str) {
49+
#[expect(clippy::disallowed_methods)]
4950
cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
5051
docs_link(diag, lint);
5152
diag
@@ -80,6 +81,7 @@ pub fn span_lint_and_help<T: LintContext>(
8081
help_span: Option<Span>,
8182
help: &str,
8283
) {
84+
#[expect(clippy::disallowed_methods)]
8385
cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
8486
let help = help.to_string();
8587
if let Some(help_span) = help_span {
@@ -123,6 +125,7 @@ pub fn span_lint_and_note<T: LintContext>(
123125
note_span: Option<Span>,
124126
note: &str,
125127
) {
128+
#[expect(clippy::disallowed_methods)]
126129
cx.struct_span_lint(lint, span, msg.to_string(), |diag| {
127130
let note = note.to_string();
128131
if let Some(note_span) = note_span {
@@ -145,6 +148,7 @@ where
145148
S: Into<MultiSpan>,
146149
F: FnOnce(&mut Diagnostic),
147150
{
151+
#[expect(clippy::disallowed_methods)]
148152
cx.struct_span_lint(lint, sp, msg.to_string(), |diag| {
149153
f(diag);
150154
docs_link(diag, lint);
@@ -153,6 +157,7 @@ where
153157
}
154158

155159
pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) {
160+
#[expect(clippy::disallowed_methods)]
156161
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
157162
docs_link(diag, lint);
158163
diag
@@ -167,6 +172,7 @@ pub fn span_lint_hir_and_then(
167172
msg: &str,
168173
f: impl FnOnce(&mut Diagnostic),
169174
) {
175+
#[expect(clippy::disallowed_methods)]
170176
cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| {
171177
f(diag);
172178
docs_link(diag, lint);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![feature(rustc_private)]
2+
3+
extern crate rustc_errors;
4+
extern crate rustc_hir;
5+
extern crate rustc_lint;
6+
extern crate rustc_middle;
7+
8+
use rustc_errors::{DiagnosticMessage, MultiSpan};
9+
use rustc_hir::hir_id::HirId;
10+
use rustc_lint::{Lint, LintContext};
11+
use rustc_middle::ty::TyCtxt;
12+
13+
pub fn a(cx: impl LintContext, lint: &'static Lint, span: impl Into<MultiSpan>, msg: impl Into<DiagnosticMessage>) {
14+
cx.struct_span_lint(lint, span, msg, |b| b);
15+
}
16+
17+
pub fn b(
18+
tcx: TyCtxt<'_>,
19+
lint: &'static Lint,
20+
hir_id: HirId,
21+
span: impl Into<MultiSpan>,
22+
msg: impl Into<DiagnosticMessage>,
23+
) {
24+
tcx.struct_span_lint_hir(lint, hir_id, span, msg, |b| b);
25+
}
26+
27+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: use of a disallowed method `rustc_lint::context::LintContext::struct_span_lint`
2+
--> $DIR/disallow_struct_span_lint.rs:14:5
3+
|
4+
LL | cx.struct_span_lint(lint, span, msg, |b| b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::disallowed-methods` implied by `-D warnings`
8+
= help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]`
9+
10+
error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::struct_span_lint_hir`
11+
--> $DIR/disallow_struct_span_lint.rs:24:5
12+
|
13+
LL | tcx.struct_span_lint_hir(lint, hir_id, span, msg, |b| b);
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: aborting due to 2 previous errors
17+

0 commit comments

Comments
 (0)