Skip to content

Commit c115d46

Browse files
gvozdvmozgubenfdking
authored andcommitted
refactor: remove unnecessary Option wrapping for edit in LintFix
1 parent b15d3b6 commit c115d46

File tree

6 files changed

+46
-85
lines changed

6 files changed

+46
-85
lines changed

crates/lib-core/src/errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl SqlError for SQLBaseError {
7474
}
7575
}
7676

77-
#[derive(Debug, PartialEq, Clone)]
77+
#[derive(Debug, Clone)]
7878
pub struct SQLLintError {
7979
base: SQLBaseError,
8080
pub fixes: Vec<LintFix>,

crates/lib-core/src/lint_fix.rs

Lines changed: 29 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct LintFix {
1818
pub anchor: ErasedSegment,
1919
/// For `replace` and `create` fixes, this holds the iterable of segments to create or replace
2020
/// at the given `anchor` point.
21-
pub edit: Option<Vec<ErasedSegment>>,
21+
pub edit: Vec<ErasedSegment>,
2222
/// For `replace` and `create` fixes, this holds iterable of segments that provided
2323
/// code. IMPORTANT: The linter uses this to prevent copying material
2424
/// from templated areas.
@@ -29,22 +29,17 @@ impl LintFix {
2929
fn new(
3030
edit_type: EditType,
3131
anchor: ErasedSegment,
32-
edit: Option<Vec<ErasedSegment>>,
32+
mut edit: Vec<ErasedSegment>,
3333
source: Option<Vec<ErasedSegment>>,
3434
) -> Self {
3535
// If `edit` is provided, copy all elements and strip position markers.
36-
let clean_edit = if let Some(mut edit) = edit {
37-
// Developer Note: Ensure position markers are unset for all edit segments.
38-
// We rely on realignment to make position markers later in the process.
39-
for seg in &mut edit {
40-
if seg.get_position_marker().is_some() {
41-
seg.make_mut().set_position_marker(None);
42-
};
43-
}
44-
Some(edit)
45-
} else {
46-
None
47-
};
36+
// Developer Note: Ensure position markers are unset for all edit segments.
37+
// We rely on realignment to make position markers later in the process.
38+
for seg in &mut edit {
39+
if seg.get_position_marker().is_some() {
40+
seg.make_mut().set_position_marker(None);
41+
};
42+
}
4843

4944
// If `source` is provided, filter segments with position markers.
5045
let clean_source = source.map_or(Vec::new(), |source| {
@@ -57,49 +52,40 @@ impl LintFix {
5752
LintFix {
5853
edit_type,
5954
anchor,
60-
edit: clean_edit,
55+
edit,
6156
source: clean_source,
6257
}
6358
}
6459

6560
pub fn create_before(anchor: ErasedSegment, edit_segments: Vec<ErasedSegment>) -> Self {
66-
Self::new(EditType::CreateBefore, anchor, edit_segments.into(), None)
61+
Self::new(EditType::CreateBefore, anchor, edit_segments, None)
6762
}
6863

6964
pub fn create_after(
7065
anchor: ErasedSegment,
7166
edit_segments: Vec<ErasedSegment>,
7267
source: Option<Vec<ErasedSegment>>,
7368
) -> Self {
74-
Self::new(EditType::CreateAfter, anchor, edit_segments.into(), source)
69+
Self::new(EditType::CreateAfter, anchor, edit_segments, source)
7570
}
7671

7772
pub fn replace(
7873
anchor_segment: ErasedSegment,
7974
edit_segments: Vec<ErasedSegment>,
8075
source: Option<Vec<ErasedSegment>>,
8176
) -> Self {
82-
Self::new(
83-
EditType::Replace,
84-
anchor_segment,
85-
Some(edit_segments),
86-
source,
87-
)
77+
Self::new(EditType::Replace, anchor_segment, edit_segments, source)
8878
}
8979

9080
pub fn delete(anchor_segment: ErasedSegment) -> Self {
91-
Self::new(EditType::Delete, anchor_segment, None, None)
81+
Self::new(EditType::Delete, anchor_segment, Vec::new(), None)
9282
}
9383

9484
/// Return whether this a valid source only edit.
9585
pub fn is_just_source_edit(&self) -> bool {
96-
if let Some(edit) = &self.edit {
97-
self.edit_type == EditType::Replace
98-
&& edit.len() == 1
99-
&& edit[0].raw() == self.anchor.raw()
100-
} else {
101-
false
102-
}
86+
self.edit_type == EditType::Replace
87+
&& self.edit.len() == 1
88+
&& self.edit[0].raw() == self.anchor.raw()
10389
}
10490

10591
fn fix_slices(
@@ -127,15 +113,11 @@ impl LintFix {
127113
return AHashSet::new();
128114
} else if self
129115
.edit
130-
.as_deref()
131-
.unwrap_or(&[])
132116
.iter()
133117
.all(|it| it.segments().is_empty() && !it.get_source_fixes().is_empty())
134118
{
135119
let source_edit_slices: Vec<_> = self
136120
.edit
137-
.as_deref()
138-
.unwrap_or(&[])
139121
.iter()
140122
.flat_map(|edit| edit.get_source_fixes())
141123
.map(|source_fixe| source_fixe.source_slice.clone())
@@ -185,11 +167,8 @@ impl LintFix {
185167
}
186168

187169
pub fn has_template_conflicts(&self, templated_file: &TemplatedFile) -> bool {
188-
if self.edit_type == EditType::Replace
189-
&& self.edit.is_none()
190-
&& self.edit.as_ref().unwrap().len() == 1
191-
{
192-
let edit = &self.edit.as_ref().unwrap()[0];
170+
if self.edit_type == EditType::Replace && self.edit.len() == 1 {
171+
let edit = &self.edit[0];
193172
if edit.raw() == self.anchor.raw() && !edit.get_source_fixes().is_empty() {
194173
return false;
195174
}
@@ -232,29 +211,18 @@ impl PartialEq for LintFix {
232211
if self.anchor.id() != other.anchor.id() {
233212
return false;
234213
}
235-
// Compare edits if they exist
236-
if let Some(self_edit) = &self.edit {
237-
if let Some(other_edit) = &other.edit {
238-
// Check lengths
239-
if self_edit.len() != other_edit.len() {
240-
return false;
241-
}
242-
// Compare raw and source_fixes for each corresponding BaseSegment
243-
for (self_base_segment, other_base_segment) in self_edit.iter().zip(other_edit) {
244-
if self_base_segment.raw() != other_base_segment.raw()
245-
|| self_base_segment.get_source_fixes()
246-
!= other_base_segment.get_source_fixes()
247-
{
248-
return false;
249-
}
250-
}
251-
} else {
252-
// self has edit, other doesn't
214+
215+
// Check lengths
216+
if self.edit.len() != other.edit.len() {
217+
return false;
218+
}
219+
// Compare raw and source_fixes for each corresponding BaseSegment
220+
for (self_base_segment, other_base_segment) in self.edit.iter().zip(&other.edit) {
221+
if self_base_segment.raw() != other_base_segment.raw()
222+
|| self_base_segment.get_source_fixes() != other_base_segment.get_source_fixes()
223+
{
253224
return false;
254225
}
255-
} else if other.edit.is_some() {
256-
// other has edit, self doesn't
257-
return false;
258226
}
259227
// If none of the above conditions were met, objects are equal
260228
true

crates/lib-core/src/parser/segments/base.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,6 @@ impl ErasedSegment {
734734
let fixes_count = anchor_info.fixes.len();
735735
for mut lint_fix in anchor_info.fixes {
736736
// Deletes are easy.
737-
#[allow(unused_assignments)]
738737
if lint_fix.edit_type == EditType::Delete {
739738
fixes_applied.push(lint_fix);
740739
// We're just getting rid of this segment.
@@ -757,24 +756,22 @@ impl ErasedSegment {
757756
}
758757

759758
let mut consumed_pos = false;
760-
for s in std::mem::take(lint_fix.edit.as_mut().unwrap()) {
761-
let mut s = s.deep_clone();
759+
for mut s in std::mem::take(&mut lint_fix.edit) {
762760
if lint_fix.edit_type == EditType::Replace
763761
&& !consumed_pos
764762
&& s.raw() == seg.raw()
765763
{
766764
consumed_pos = true;
767-
s.get_mut()
765+
s.make_mut()
768766
.set_position_marker(seg.get_position_marker().cloned());
769767
}
770768

771769
seg_buffer.push(s);
772770
}
773771

774-
#[allow(unused_assignments)]
775772
if !(lint_fix.edit_type == EditType::Replace
776-
&& lint_fix.edit.as_ref().is_some_and(|x| x.len() == 1)
777-
&& lint_fix.edit.as_ref().unwrap()[0].class_types() == seg.class_types())
773+
&& lint_fix.edit.len() == 1
774+
&& lint_fix.edit[0].class_types() == seg.class_types())
778775
{
779776
_requires_validate = true;
780777
}
@@ -801,7 +798,6 @@ impl ErasedSegment {
801798
seg_buffer.push(s);
802799
seg_buffer.extend(post);
803800

804-
#[allow(unused_assignments)]
805801
if !validated {
806802
_requires_validate = true;
807803
}

crates/lib-core/src/segments.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ impl AnchorEditInfo {
5959
};
6060

6161
if fix.is_just_source_edit() {
62-
let edit = fix.edit.as_ref().unwrap();
63-
self.source_fixes.extend(edit[0].get_source_fixes());
62+
self.source_fixes.extend(fix.edit[0].get_source_fixes());
6463

6564
if let Some(_first_replace) = &self.first_replace {
6665
unimplemented!();

crates/lib/src/rules/layout/lt08.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ SELECT a FROM plop
193193
anchor: fix_point.unwrap(),
194194
edit: repeat(SegmentBuilder::newline(context.tables.next_id(), "\n"))
195195
.take(num_newlines)
196-
.collect_vec()
197-
.into(),
196+
.collect_vec(),
198197
source: Vec::new(),
199198
}];
200199

crates/lib/src/utils/reflow/respace.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -479,15 +479,14 @@ pub fn handle_respace_inline_without_space(
479479

480480
'outer: for (result_idx, res) in enumerate(&existing_results) {
481481
for (fix_idx, fix) in enumerate(&res.fixes) {
482-
if let Some(edits) = &fix.edit {
483-
if edits
484-
.iter()
485-
.any(|e| e.id() == insertion.as_ref().unwrap().id())
486-
{
487-
res_found = Some(result_idx);
488-
fix_found = Some(fix_idx);
489-
break 'outer;
490-
}
482+
if fix
483+
.edit
484+
.iter()
485+
.any(|e| e.id() == insertion.as_ref().unwrap().id())
486+
{
487+
res_found = Some(result_idx);
488+
fix_found = Some(fix_idx);
489+
break 'outer;
491490
}
492491
}
493492
}
@@ -500,7 +499,7 @@ pub fn handle_respace_inline_without_space(
500499
if existing_fix == "before" {
501500
unimplemented!()
502501
} else if existing_fix == "after" {
503-
fix.edit.as_mut().unwrap().push(added_whitespace);
502+
fix.edit.push(added_whitespace);
504503
}
505504

506505
return (segment_buffer, existing_results, true);
@@ -530,7 +529,7 @@ pub fn handle_respace_inline_without_space(
530529
vec![LintFix {
531530
edit_type: EditType::CreateAfter,
532531
anchor: prev_block.segment().clone(),
533-
edit: vec![added_whitespace].into(),
532+
edit: vec![added_whitespace],
534533
source: vec![],
535534
}],
536535
desc.into(),

0 commit comments

Comments
 (0)