Inline DiagnosticKind into other diagnostic types (#18074)

## Summary

This PR deletes the `DiagnosticKind` type by inlining its three fields
(`name`, `body`, and `suggestion`) into three other diagnostic types:
`Diagnostic`, `DiagnosticMessage`, and `CacheMessage`.

Instead of deferring to an internal `DiagnosticKind`, both `Diagnostic`
and `DiagnosticMessage` now have their own macro-generated `AsRule`
implementations.

This should make both https://github.com/astral-sh/ruff/pull/18051 and
another follow-up PR changing the type of `name` on `CacheMessage`
easier since its type will be able to change separately from
`Diagnostic` and `DiagnosticMessage`.

## Test Plan

Existing tests
This commit is contained in:
Brent Westbrook 2025-05-15 10:27:21 -04:00 committed by GitHub
parent b35bf8ae07
commit e2c5b83fe1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 604 additions and 621 deletions

View file

@ -9,7 +9,7 @@ use crate::{
session::DocumentQuery,
PositionEncoding, DIAGNOSTIC_NAME,
};
use ruff_diagnostics::{Applicability, DiagnosticKind, Edit, Fix};
use ruff_diagnostics::{Applicability, Edit, Fix};
use ruff_linter::{
directives::{extract_directives, Flags},
generate_noqa_edits,
@ -32,7 +32,7 @@ use ruff_text_size::{Ranged, TextRange};
/// This is serialized on the diagnostic `data` field.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub(crate) struct AssociatedDiagnosticData {
pub(crate) kind: DiagnosticKind,
pub(crate) title: String,
/// Edits to fix the diagnostic. If this is empty, a fix
/// does not exist.
pub(crate) edits: Vec<lsp_types::TextEdit>,
@ -227,10 +227,7 @@ pub(crate) fn fixes_for_diagnostics(
Ok(Some(DiagnosticFix {
fixed_diagnostic,
code: associated_data.code,
title: associated_data
.kind
.suggestion
.unwrap_or(associated_data.kind.name),
title: associated_data.title,
noqa_edit: associated_data.noqa_edit,
edits: associated_data.edits,
}))
@ -248,15 +245,16 @@ fn to_lsp_diagnostic(
index: &LineIndex,
encoding: PositionEncoding,
) -> (usize, lsp_types::Diagnostic) {
let rule = diagnostic.rule();
let DiagnosticMessage {
kind,
range: diagnostic_range,
fix,
name,
body,
suggestion,
..
} = diagnostic;
let rule = kind.rule();
let fix = fix.and_then(|fix| fix.applies(Applicability::Unsafe).then_some(fix));
let data = (fix.is_some() || noqa_edit.is_some())
@ -275,7 +273,7 @@ fn to_lsp_diagnostic(
new_text: noqa_edit.into_content().unwrap_or_default().into_string(),
});
serde_json::to_value(AssociatedDiagnosticData {
kind: kind.clone(),
title: suggestion.unwrap_or_else(|| name.to_string()),
noqa_edit,
edits,
code: rule.noqa_code().to_string(),
@ -314,7 +312,7 @@ fn to_lsp_diagnostic(
})
}),
source: Some(DIAGNOSTIC_NAME.into()),
message: kind.body,
message: body,
related_information: None,
data,
},