From 19eec0b7cc55e29d413da60b72a2e0eaa72eded8 Mon Sep 17 00:00:00 2001 From: y21 <30553356+y21@users.noreply.github.com> Date: Wed, 15 Nov 2023 03:20:04 +0100 Subject: [PATCH] disallow `struct_span_lint` --- clippy.toml | 6 +++++ clippy_utils/src/diagnostics.rs | 6 +++++ .../ui-internal/disallow_struct_span_lint.rs | 27 +++++++++++++++++++ .../disallow_struct_span_lint.stderr | 17 ++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 tests/ui-internal/disallow_struct_span_lint.rs create mode 100644 tests/ui-internal/disallow_struct_span_lint.stderr diff --git a/clippy.toml b/clippy.toml index cda8d17eed4..4a1805f7523 100644 --- a/clippy.toml +++ b/clippy.toml @@ -1 +1,7 @@ avoid-breaking-exported-api = false + +# use the various `span_lint_*` methods instead, which also add a link to the docs +disallowed-methods = [ + "rustc_lint::context::LintContext::struct_span_lint", + "rustc_middle::ty::context::TyCtxt::struct_span_lint_hir" +] diff --git a/clippy_utils/src/diagnostics.rs b/clippy_utils/src/diagnostics.rs index bc51fc6e8e9..e86d7d77503 100644 --- a/clippy_utils/src/diagnostics.rs +++ b/clippy_utils/src/diagnostics.rs @@ -46,6 +46,7 @@ fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) { /// | ^^^^^^^^^^^^^^^^^^^^^^^ /// ``` pub fn span_lint(cx: &T, lint: &'static Lint, sp: impl Into, msg: &str) { + #[expect(clippy::disallowed_methods)] cx.struct_span_lint(lint, sp, msg.to_string(), |diag| { docs_link(diag, lint); diag @@ -80,6 +81,7 @@ pub fn span_lint_and_help( help_span: Option, help: &str, ) { + #[expect(clippy::disallowed_methods)] cx.struct_span_lint(lint, span, msg.to_string(), |diag| { let help = help.to_string(); if let Some(help_span) = help_span { @@ -123,6 +125,7 @@ pub fn span_lint_and_note( note_span: Option, note: &str, ) { + #[expect(clippy::disallowed_methods)] cx.struct_span_lint(lint, span, msg.to_string(), |diag| { let note = note.to_string(); if let Some(note_span) = note_span { @@ -145,6 +148,7 @@ pub fn span_lint_and_then(cx: &C, lint: &'static Lint, sp: S, msg: &str S: Into, F: FnOnce(&mut Diagnostic), { + #[expect(clippy::disallowed_methods)] cx.struct_span_lint(lint, sp, msg.to_string(), |diag| { f(diag); docs_link(diag, lint); @@ -153,6 +157,7 @@ pub fn span_lint_and_then(cx: &C, lint: &'static Lint, sp: S, msg: &str } pub fn span_lint_hir(cx: &LateContext<'_>, lint: &'static Lint, hir_id: HirId, sp: Span, msg: &str) { + #[expect(clippy::disallowed_methods)] cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| { docs_link(diag, lint); diag @@ -167,6 +172,7 @@ pub fn span_lint_hir_and_then( msg: &str, f: impl FnOnce(&mut Diagnostic), ) { + #[expect(clippy::disallowed_methods)] cx.tcx.struct_span_lint_hir(lint, hir_id, sp, msg.to_string(), |diag| { f(diag); docs_link(diag, lint); diff --git a/tests/ui-internal/disallow_struct_span_lint.rs b/tests/ui-internal/disallow_struct_span_lint.rs new file mode 100644 index 00000000000..3155c0235ff --- /dev/null +++ b/tests/ui-internal/disallow_struct_span_lint.rs @@ -0,0 +1,27 @@ +#![feature(rustc_private)] + +extern crate rustc_errors; +extern crate rustc_hir; +extern crate rustc_lint; +extern crate rustc_middle; + +use rustc_errors::{DiagnosticMessage, MultiSpan}; +use rustc_hir::hir_id::HirId; +use rustc_lint::{Lint, LintContext}; +use rustc_middle::ty::TyCtxt; + +pub fn a(cx: impl LintContext, lint: &'static Lint, span: impl Into, msg: impl Into) { + cx.struct_span_lint(lint, span, msg, |b| b); +} + +pub fn b( + tcx: TyCtxt<'_>, + lint: &'static Lint, + hir_id: HirId, + span: impl Into, + msg: impl Into, +) { + tcx.struct_span_lint_hir(lint, hir_id, span, msg, |b| b); +} + +fn main() {} diff --git a/tests/ui-internal/disallow_struct_span_lint.stderr b/tests/ui-internal/disallow_struct_span_lint.stderr new file mode 100644 index 00000000000..76c487fb135 --- /dev/null +++ b/tests/ui-internal/disallow_struct_span_lint.stderr @@ -0,0 +1,17 @@ +error: use of a disallowed method `rustc_lint::context::LintContext::struct_span_lint` + --> $DIR/disallow_struct_span_lint.rs:14:5 + | +LL | cx.struct_span_lint(lint, span, msg, |b| b); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::disallowed-methods` implied by `-D warnings` + = help: to override `-D warnings` add `#[allow(clippy::disallowed_methods)]` + +error: use of a disallowed method `rustc_middle::ty::context::TyCtxt::struct_span_lint_hir` + --> $DIR/disallow_struct_span_lint.rs:24:5 + | +LL | tcx.struct_span_lint_hir(lint, hir_id, span, msg, |b| b); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 2 previous errors +