diff --git a/ruff_cli/src/diagnostics.rs b/ruff_cli/src/diagnostics.rs index 14d39e8170..616f262c6e 100644 --- a/ruff_cli/src/diagnostics.rs +++ b/ruff_cli/src/diagnostics.rs @@ -79,10 +79,10 @@ pub fn lint_path( { if fixed > 0 { if matches!(autofix, fix::FixMode::Apply) { - write(path, transformed)?; + write(path, transformed.as_bytes())?; } else if matches!(autofix, fix::FixMode::Diff) { let mut stdout = io::stdout().lock(); - TextDiff::from_lines(&contents, &transformed) + TextDiff::from_lines(contents.as_str(), &transformed) .unified_diff() .header(&fs::relativize_path(path), &fs::relativize_path(path)) .to_writer(&mut stdout)?; diff --git a/src/linter.rs b/src/linter.rs index 8829fd957d..e7f0066bd4 100644 --- a/src/linter.rs +++ b/src/linter.rs @@ -1,3 +1,4 @@ +use std::borrow::Cow; use std::path::Path; use anyhow::{anyhow, Result}; @@ -314,13 +315,13 @@ pub fn lint_only( /// Generate `Diagnostic`s from source code content, iteratively autofixing /// until stable. -pub fn lint_fix( - contents: &str, +pub fn lint_fix<'a>( + contents: &'a str, path: &Path, package: Option<&Path>, settings: &Settings, -) -> Result<(LinterResult>, String, usize)> { - let mut transformed = contents.to_string(); +) -> Result<(LinterResult>, Cow<'a, str>, usize)> { + let mut transformed = Cow::Borrowed(contents); // Track the number of fixed errors across iterations. let mut fixed = 0; @@ -397,7 +398,7 @@ This indicates a bug in `{}`. If you could open an issue at: fixed += applied; // Store the fixed contents. - transformed = fixed_contents.to_string(); + transformed = Cow::Owned(fixed_contents); // Increment the iteration count. iterations += 1;