Skip to content

Commit 31b551f

Browse files
committed
Auto merge of #120576 - nnethercote:merge-Diagnostic-DiagnosticBuilder, r=davidtwco
Overhaul `Diagnostic` and `DiagnosticBuilder` Implements the first part of rust-lang/compiler-team#722, which moves functionality and use away from `Diagnostic`, onto `DiagnosticBuilder`. Likely follow-ups: - Move things around, because this PR was written to minimize diff size, so some things end up in sub-optimal places. E.g. `DiagnosticBuilder` has impls in both `diagnostic.rs` and `diagnostic_builder.rs`. - Rename `Diagnostic` as `DiagInner` and `DiagnosticBuilder` as `Diag`. r? `@davidtwco`
2 parents ba2139a + 86cb711 commit 31b551f

11 files changed

+26
-26
lines changed

clippy_lints/src/casts/cast_possible_truncation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::expr_or_init;
44
use clippy_utils::source::snippet;
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::{get_discriminant_value, is_isize_or_usize};
7-
use rustc_errors::{Applicability, Diagnostic, SuggestionStyle};
7+
use rustc_errors::{Applicability, DiagnosticBuilder, SuggestionStyle};
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{BinOpKind, Expr, ExprKind};
1010
use rustc_lint::LateContext;
@@ -177,7 +177,7 @@ fn offer_suggestion(
177177
expr: &Expr<'_>,
178178
cast_expr: &Expr<'_>,
179179
cast_to_span: Span,
180-
diag: &mut Diagnostic,
180+
diag: &mut DiagnosticBuilder<'_, ()>,
181181
) {
182182
let cast_to_snip = snippet(cx, cast_to_span, "..");
183183
let suggestion = if cast_to_snip == "_" {

clippy_lints/src/functions/result.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::Diagnostic;
1+
use rustc_errors::DiagnosticBuilder;
22
use rustc_hir as hir;
33
use rustc_lint::{LateContext, LintContext};
44
use rustc_middle::lint::in_external_macro;
@@ -135,7 +135,7 @@ fn check_result_large_err<'tcx>(cx: &LateContext<'tcx>, err_ty: Ty<'tcx>, hir_ty
135135
RESULT_LARGE_ERR,
136136
hir_ty_span,
137137
"the `Err`-variant returned from this function is very large",
138-
|diag: &mut Diagnostic| {
138+
|diag: &mut DiagnosticBuilder<'_, ()>| {
139139
diag.span_label(hir_ty_span, format!("the `Err`-variant is at least {ty_size} bytes"));
140140
diag.help(format!("try reducing the size of `{err_ty}`, for example by boxing large elements or replacing it with `Box<{err_ty}>`"));
141141
},

clippy_lints/src/if_let_mutex.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use clippy_utils::{higher, SpanlessEq};
4-
use rustc_errors::Diagnostic;
4+
use rustc_errors::DiagnosticBuilder;
55
use rustc_hir::intravisit::{self as visit, Visitor};
66
use rustc_hir::{Expr, ExprKind};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -59,7 +59,7 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
5959
arm_visit.visit_expr(if_else);
6060

6161
if let Some(arm_mutex) = arm_visit.found_mutex_if_same_as(op_mutex) {
62-
let diag = |diag: &mut Diagnostic| {
62+
let diag = |diag: &mut DiagnosticBuilder<'_, ()>| {
6363
diag.span_label(
6464
op_mutex.span,
6565
"this Mutex will remain locked for the entire `if let`-block...",

clippy_lints/src/implicit_hasher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::borrow::Cow;
22
use std::collections::BTreeMap;
33

4-
use rustc_errors::Diagnostic;
4+
use rustc_errors::DiagnosticBuilder;
55
use rustc_hir as hir;
66
use rustc_hir::intravisit::{walk_body, walk_expr, walk_inf, walk_ty, Visitor};
77
use rustc_hir::{Body, Expr, ExprKind, GenericArg, Item, ItemKind, QPath, TyKind};
@@ -65,7 +65,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
6565

6666
fn suggestion(
6767
cx: &LateContext<'_>,
68-
diag: &mut Diagnostic,
68+
diag: &mut DiagnosticBuilder<'_, ()>,
6969
generics_span: Span,
7070
generics_suggestion_span: Span,
7171
target: &ImplicitHasherType<'_>,

clippy_lints/src/manual_clamp.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::{
99
peel_blocks_with_stmt, MaybePath,
1010
};
1111
use itertools::Itertools;
12-
use rustc_errors::{Applicability, Diagnostic};
12+
use rustc_errors::{Applicability, DiagnosticBuilder};
1313
use rustc_hir::def::Res;
1414
use rustc_hir::{Arm, BinOpKind, Block, Expr, ExprKind, HirId, PatKind, PathSegment, PrimTy, QPath, StmtKind};
1515
use rustc_lint::{LateContext, LateLintPass};
@@ -163,7 +163,7 @@ fn emit_suggestion<'tcx>(cx: &LateContext<'tcx>, suggestion: &ClampSuggestion<'t
163163
};
164164
let suggestion = format!("{assignment}{input}.clamp({min}, {max}){semicolon}");
165165
let msg = "clamp-like pattern without using clamp function";
166-
let lint_builder = |d: &mut Diagnostic| {
166+
let lint_builder = |d: &mut DiagnosticBuilder<'_, ()>| {
167167
d.span_suggestion(*span, "replace with clamp", suggestion, Applicability::MaybeIncorrect);
168168
if *is_float {
169169
d.note("clamp will panic if max < min, min.is_nan(), or max.is_nan()")

clippy_lints/src/matches/significant_drop_in_scrutinee.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::FxHashSet;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::source::{indent_of, snippet};
44
use clippy_utils::{get_attr, is_lint_allowed};
5-
use rustc_errors::{Applicability, Diagnostic};
5+
use rustc_errors::{Applicability, DiagnosticBuilder};
66
use rustc_hir::intravisit::{walk_expr, Visitor};
77
use rustc_hir::{Arm, Expr, ExprKind, MatchSource};
88
use rustc_lint::{LateContext, LintContext};
@@ -37,7 +37,7 @@ pub(super) fn check<'tcx>(
3737
}
3838
}
3939

40-
fn set_diagnostic<'tcx>(diag: &mut Diagnostic, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, found: FoundSigDrop) {
40+
fn set_diagnostic<'tcx>(diag: &mut DiagnosticBuilder<'_, ()>, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>, found: FoundSigDrop) {
4141
if found.lint_suggestion == LintSuggestion::MoveAndClone {
4242
// If our suggestion is to move and clone, then we want to leave it to the user to
4343
// decide how to address this lint, since it may be that cloning is inappropriate.

clippy_lints/src/methods/suspicious_command_arg_space.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use rustc_errors::{Applicability, Diagnostic};
3+
use rustc_errors::{Applicability, DiagnosticBuilder};
44
use rustc_lint::LateContext;
55
use rustc_span::{sym, Span};
66
use {rustc_ast as ast, rustc_hir as hir};
@@ -22,7 +22,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, recv: &'tcx hir::Expr<'_>, arg
2222
SUSPICIOUS_COMMAND_ARG_SPACE,
2323
arg.span,
2424
"single argument that looks like it should be multiple arguments",
25-
|diag: &mut Diagnostic| {
25+
|diag: &mut DiagnosticBuilder<'_, ()>| {
2626
diag.multipart_suggestion_verbose(
2727
"consider splitting the argument",
2828
vec![(span, "args".to_string()), (arg.span, format!("[{arg1:?}, {arg2:?}]"))],

clippy_lints/src/missing_asserts_for_indexing.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use clippy_utils::{eq_expr_value, hash_expr, higher};
99
use rustc_ast::{LitKind, RangeLimits};
1010
use rustc_data_structures::packed::Pu128;
1111
use rustc_data_structures::unhash::UnhashMap;
12-
use rustc_errors::{Applicability, Diagnostic};
12+
use rustc_errors::{Applicability, DiagnosticBuilder};
1313
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
1414
use rustc_lint::{LateContext, LateLintPass};
1515
use rustc_session::declare_lint_pass;
@@ -67,7 +67,7 @@ declare_lint_pass!(MissingAssertsForIndexing => [MISSING_ASSERTS_FOR_INDEXING]);
6767

6868
fn report_lint<F>(cx: &LateContext<'_>, full_span: Span, msg: &str, indexes: &[Span], f: F)
6969
where
70-
F: FnOnce(&mut Diagnostic),
70+
F: FnOnce(&mut DiagnosticBuilder<'_, ()>),
7171
{
7272
span_lint_and_then(cx, MISSING_ASSERTS_FOR_INDEXING, full_span, msg, |diag| {
7373
f(diag);

clippy_lints/src/needless_pass_by_value.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::ty::{
66
implements_trait, implements_trait_with_env_from_iter, is_copy, is_type_diagnostic_item, is_type_lang_item,
77
};
88
use rustc_ast::ast::Attribute;
9-
use rustc_errors::{Applicability, Diagnostic};
9+
use rustc_errors::{Applicability, DiagnosticBuilder};
1010
use rustc_hir::intravisit::FnKind;
1111
use rustc_hir::{
1212
BindingAnnotation, Body, FnDecl, GenericArg, HirId, HirIdSet, Impl, ItemKind, LangItem, Mutability, Node, PatKind,
@@ -196,7 +196,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
196196
&& !moved_vars.contains(&canonical_id)
197197
{
198198
// Dereference suggestion
199-
let sugg = |diag: &mut Diagnostic| {
199+
let sugg = |diag: &mut DiagnosticBuilder<'_, ()>| {
200200
if let ty::Adt(def, ..) = ty.kind() {
201201
if let Some(span) = cx.tcx.hir().span_if_local(def.did()) {
202202
if type_allowed_to_implement_copy(

clippy_utils/src/diagnostics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
//! Thank you!
99
//! ~The `INTERNAL_METADATA_COLLECTOR` lint
1010
11-
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
11+
use rustc_errors::{Applicability, DiagnosticBuilder, MultiSpan};
1212
use rustc_hir::HirId;
1313
use rustc_lint::{LateContext, Lint, LintContext};
1414
use rustc_span::Span;
1515
use std::env;
1616

17-
fn docs_link(diag: &mut Diagnostic, lint: &'static Lint) {
17+
fn docs_link(diag: &mut DiagnosticBuilder<'_, ()>, lint: &'static Lint) {
1818
if env::var("CLIPPY_DISABLE_DOCS_LINKS").is_err() {
1919
if let Some(lint) = lint.name_lower().strip_prefix("clippy::") {
2020
diag.help(format!(
@@ -143,7 +143,7 @@ pub fn span_lint_and_then<C, S, F>(cx: &C, lint: &'static Lint, sp: S, msg: &str
143143
where
144144
C: LintContext,
145145
S: Into<MultiSpan>,
146-
F: FnOnce(&mut Diagnostic),
146+
F: FnOnce(&mut DiagnosticBuilder<'_, ()>),
147147
{
148148
#[expect(clippy::disallowed_methods)]
149149
cx.span_lint(lint, sp, msg.to_string(), |diag| {
@@ -165,7 +165,7 @@ pub fn span_lint_hir_and_then(
165165
hir_id: HirId,
166166
sp: impl Into<MultiSpan>,
167167
msg: &str,
168-
f: impl FnOnce(&mut Diagnostic),
168+
f: impl FnOnce(&mut DiagnosticBuilder<'_, ()>),
169169
) {
170170
#[expect(clippy::disallowed_methods)]
171171
cx.tcx.node_span_lint(lint, hir_id, sp, msg.to_string(), |diag| {
@@ -214,7 +214,7 @@ pub fn span_lint_and_sugg<T: LintContext>(
214214
/// appear once per
215215
/// replacement. In human-readable format though, it only appears once before
216216
/// the whole suggestion.
217-
pub fn multispan_sugg<I>(diag: &mut Diagnostic, help_msg: &str, sugg: I)
217+
pub fn multispan_sugg<I>(diag: &mut DiagnosticBuilder<'_, ()>, help_msg: &str, sugg: I)
218218
where
219219
I: IntoIterator<Item = (Span, String)>,
220220
{
@@ -227,7 +227,7 @@ where
227227
/// multiple spans. This is tracked in issue [rustfix#141](https://github.com/rust-lang/rustfix/issues/141).
228228
/// Suggestions with multiple spans will be silently ignored.
229229
pub fn multispan_sugg_with_applicability<I>(
230-
diag: &mut Diagnostic,
230+
diag: &mut DiagnosticBuilder<'_, ()>,
231231
help_msg: &str,
232232
applicability: Applicability,
233233
sugg: I,

clippy_utils/src/sugg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ fn indentation<T: LintContext>(cx: &T, span: Span) -> Option<String> {
683683
})
684684
}
685685

686-
/// Convenience extension trait for `Diagnostic`.
686+
/// Convenience extension trait for `DiagnosticBuilder`.
687687
pub trait DiagnosticExt<T: LintContext> {
688688
/// Suggests to add an attribute to an item.
689689
///
@@ -731,7 +731,7 @@ pub trait DiagnosticExt<T: LintContext> {
731731
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability);
732732
}
733733

734-
impl<T: LintContext> DiagnosticExt<T> for rustc_errors::Diagnostic {
734+
impl<T: LintContext> DiagnosticExt<T> for rustc_errors::DiagnosticBuilder<'_, ()> {
735735
fn suggest_item_with_attr<D: Display + ?Sized>(
736736
&mut self,
737737
cx: &T,

0 commit comments

Comments
 (0)