Skip to content

Commit

Permalink
refactor: remove .clone() for LintFix and LintResult
Browse files Browse the repository at this point in the history
  • Loading branch information
gvozdvmozgu authored and benfdking committed Jan 16, 2025
1 parent c4541b9 commit 018797e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/lib-core/src/lint_fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::parser::segments::base::ErasedSegment;
use crate::templaters::base::{RawFileSlice, TemplatedFile};

/// A potential fix to a linting violation.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct LintFix {
/// indicate the kind of fix this represents
pub edit_type: EditType,
Expand Down
34 changes: 19 additions & 15 deletions crates/lib-core/src/parser/segments/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,12 +740,11 @@ impl ErasedSegment {
}

let fixes_count = anchor_info.fixes.len();
for mut f in anchor_info.fixes {
fixes_applied.push(f.clone());

for mut lint_fix in anchor_info.fixes {
// Deletes are easy.
#[allow(unused_assignments)]
if f.edit_type == EditType::Delete {
if lint_fix.edit_type == EditType::Delete {
fixes_applied.push(lint_fix);
// We're just getting rid of this segment.
_requires_validate = true;
// NOTE: We don't add the segment in this case.
Expand All @@ -754,21 +753,24 @@ impl ErasedSegment {

// Otherwise it must be a replace or a create.
assert!(matches!(
f.edit_type,
lint_fix.edit_type,
EditType::Replace | EditType::CreateBefore | EditType::CreateAfter
));

if f.edit_type == EditType::CreateAfter && fixes_count == 1 {
if lint_fix.edit_type == EditType::CreateAfter && fixes_count == 1 {
// In the case of a creation after that is not part
// of a create_before/create_after pair, also add
// this segment before the edit.
seg_buffer.push(seg.clone());
}

let mut consumed_pos = false;
for s in std::mem::take(f.edit.as_mut().unwrap()) {
for s in std::mem::take(lint_fix.edit.as_mut().unwrap()) {
let mut s = s.deep_clone();
if f.edit_type == EditType::Replace && !consumed_pos && s.raw() == seg.raw() {
if lint_fix.edit_type == EditType::Replace
&& !consumed_pos
&& s.raw() == seg.raw()
{
consumed_pos = true;
s.get_mut()
.set_position_marker(seg.get_position_marker().cloned());
Expand All @@ -778,16 +780,18 @@ impl ErasedSegment {
}

#[allow(unused_assignments)]
if !(f.edit_type == EditType::Replace
&& f.edit.as_ref().is_some_and(|x| x.len() == 1)
&& f.edit.as_ref().unwrap()[0].class_types() == seg.class_types())
if !(lint_fix.edit_type == EditType::Replace
&& lint_fix.edit.as_ref().is_some_and(|x| x.len() == 1)
&& lint_fix.edit.as_ref().unwrap()[0].class_types() == seg.class_types())
{
_requires_validate = true;
}

if f.edit_type == EditType::CreateBefore {
if lint_fix.edit_type == EditType::CreateBefore {
seg_buffer.push(seg.clone());
}

fixes_applied.push(lint_fix);
}
}

Expand Down Expand Up @@ -1159,12 +1163,12 @@ mod tests {

// Check the first replace
assert_eq!(
anchor_info.first_replace,
Some(LintFix::replace(
anchor_info.fixes[anchor_info.first_replace.unwrap()],
LintFix::replace(
raw_segs[0].clone(),
vec![raw_segs[0].edit(tables.next_id(), Some("a".to_string()), None)],
None,
))
)
);
}
}
12 changes: 8 additions & 4 deletions crates/lib-core/src/segments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::edit_type::EditType;
use crate::lint_fix::LintFix;
use crate::parser::segments::fix::SourceFix;

type LintFixIdx = usize;

/// For a given fix anchor, count of the fix edit types and fixes for it."""
#[derive(Debug, Clone, Default)]
#[derive(Debug, Default)]
pub struct AnchorEditInfo {
pub delete: usize,
pub replace: usize,
Expand All @@ -12,7 +14,7 @@ pub struct AnchorEditInfo {
pub fixes: Vec<LintFix>,
pub source_fixes: Vec<SourceFix>,
// First fix of edit_type "replace" in "fixes"
pub first_replace: Option<LintFix>,
pub first_replace: Option<LintFixIdx>,
}

impl AnchorEditInfo {
Expand Down Expand Up @@ -65,15 +67,17 @@ impl AnchorEditInfo {
}
}

self.fixes.push(fix.clone());
if fix.edit_type == EditType::Replace && self.first_replace.is_none() {
self.first_replace = Some(fix.clone());
self.first_replace = Some(self.fixes.len());
}

match fix.edit_type {
EditType::CreateBefore => self.create_before += 1,
EditType::CreateAfter => self.create_after += 1,
EditType::Replace => self.replace += 1,
EditType::Delete => self.delete += 1,
};

self.fixes.push(fix);
}
}
1 change: 0 additions & 1 deletion crates/lib/src/core/rules/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use super::context::RuleContext;
use super::crawlers::{BaseCrawler, Crawler};
use crate::core::config::{FluffConfig, Value};

#[derive(Clone)]
pub struct LintResult {
pub anchor: Option<ErasedSegment>,
pub fixes: Vec<LintFix>,
Expand Down
10 changes: 6 additions & 4 deletions crates/lib/src/utils/reflow/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,13 @@ impl<'a> ReflowSequence<'a> {
let mut new_elements = Vec::new();

for (point, pre, post) in self.iter_points_with_constraints() {
let (new_lint_results, mut new_point) = point.respace_point(
let lint_results_len = lint_results.len();
let (mut new_lint_results, mut new_point) = point.respace_point(
tables,
pre,
post,
&self.root_segment,
lint_results.clone(),
lint_results,
strip_newlines,
"before",
);
Expand All @@ -283,10 +284,11 @@ impl<'a> ReflowSequence<'a> {

if ignore {
new_point = point.clone();
} else {
lint_results = new_lint_results;
new_lint_results.truncate(lint_results_len);
}

lint_results = new_lint_results;

if let Some(pre_value) = pre {
if new_elements.is_empty() || new_elements.last().unwrap() != pre_value {
new_elements.push(pre_value.clone().into());
Expand Down

0 comments on commit 018797e

Please sign in to comment.