Use a copy-on-write to avoid extra contents clone (#2508)

This commit is contained in:
Charlie Marsh
2023-02-02 20:19:16 -05:00
committed by GitHub
parent 3e6fe46bc4
commit a95474f2b1
2 changed files with 8 additions and 7 deletions

View File

@@ -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<Vec<Message>>, String, usize)> {
let mut transformed = contents.to_string();
) -> Result<(LinterResult<Vec<Message>>, 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;