Skip to content

Commit 957d713

Browse files
committed
add transactional semantics to rustfix
1 parent 06e0ef4 commit 957d713

File tree

3 files changed

+181
-144
lines changed

3 files changed

+181
-144
lines changed

crates/rustfix/src/error.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::ops::Range;
44

5+
#[non_exhaustive]
56
#[derive(Debug, thiserror::Error)]
67
pub enum Error {
78
#[error("invalid range {0:?}, start is larger than end")]
@@ -10,9 +11,7 @@ pub enum Error {
1011
#[error("invalid range {0:?}, original data is only {1} byte long")]
1112
DataLengthExceeded(Range<usize>, usize),
1213

13-
#[error("could not replace range {0:?}, maybe parts of it were already replaced?")]
14-
MaybeAlreadyReplaced(Range<usize>),
15-
14+
#[non_exhaustive] // There are plans to add fields to this variant at a later time.
1615
#[error("cannot replace slice of data that was already replaced")]
1716
AlreadyReplaced,
1817

crates/rustfix/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,11 @@ impl CodeFix {
243243
pub fn apply_solution(&mut self, solution: &Solution) -> Result<(), Error> {
244244
for r in &solution.replacements {
245245
self.data
246-
.replace_range(r.snippet.range.clone(), r.replacement.as_bytes())?;
247-
self.modified = true;
246+
.replace_range(r.snippet.range.clone(), r.replacement.as_bytes())
247+
.inspect_err(|_| self.data.restore())?;
248248
}
249+
self.data.commit();
250+
self.modified = true;
249251
Ok(())
250252
}
251253

0 commit comments

Comments
 (0)