Remove Fix::from(Edit) and add deprecated replacement methods to Diagnostics (#4275)

This commit is contained in:
Micha Reiser 2023-05-08 12:25:50 +02:00 committed by GitHub
parent 0801f14046
commit 4d5a339d9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
51 changed files with 205 additions and 144 deletions

View file

@ -5,7 +5,7 @@ use ruff_text_size::{TextRange, TextSize};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::Fix;
use crate::{Edit, Fix};
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@ -40,14 +40,21 @@ impl Diagnostic {
/// Set the [`Fix`] used to fix the diagnostic.
#[inline]
pub fn set_fix<T: Into<Fix>>(&mut self, fix: T) {
self.fix = Some(fix.into());
pub fn set_fix(&mut self, fix: Fix) {
self.fix = Some(fix);
}
/// Set the [`Fix`] used to fix the diagnostic.
#[inline]
#[deprecated(note = "Use `Diagnostic::set_fix` instead.")]
pub fn set_fix_from_edit(&mut self, edit: Edit) {
self.fix = Some(Fix::unspecified(edit));
}
/// Consumes `self` and returns a new `Diagnostic` with the given `fix`.
#[inline]
#[must_use]
pub fn with_fix<T: Into<Fix>>(mut self, fix: T) -> Self {
pub fn with_fix(mut self, fix: Fix) -> Self {
self.set_fix(fix);
self
}
@ -55,9 +62,20 @@ impl Diagnostic {
/// Set the [`Fix`] used to fix the diagnostic, if the provided function returns `Ok`.
/// Otherwise, log the error.
#[inline]
pub fn try_set_fix<T: Into<Fix>>(&mut self, func: impl FnOnce() -> Result<T>) {
pub fn try_set_fix(&mut self, func: impl FnOnce() -> Result<Fix>) {
match func() {
Ok(fix) => self.fix = Some(fix.into()),
Ok(fix) => self.fix = Some(fix),
Err(err) => error!("Failed to create fix: {}", err),
}
}
/// Sets an [`Edit`] used to fix the diagnostic, if the provided function returns `Ok`.
/// Otherwise, log the error.
#[inline]
#[deprecated(note = "Use Diagnostic::try_set_fix instead")]
pub fn try_set_fix_from_edit(&mut self, func: impl FnOnce() -> Result<Edit>) {
match func() {
Ok(edit) => self.fix = Some(Fix::unspecified(edit)),
Err(err) => error!("Failed to create fix: {}", err),
}
}

View file

@ -38,9 +38,3 @@ impl Fix {
self.edits
}
}
impl From<Edit> for Fix {
fn from(edit: Edit) -> Self {
Self { edits: vec![edit] }
}
}