Skip to content

Commit 3dedb85

Browse files
committed
Auto merge of #14747 - saites:rustfix-transactional, r=weihanglo
Add transactional semantics to `rustfix` ### What does this PR try to resolve? This PR adds transactional semantics to `rustfix::Data`, enabling `rustfix::CodeFix` to apply `Suggestion`s as atomic units and rollback partially-applied changes when they conflict with existing ones. The basic approach and goals are discussed [in a comment on issue 14699](#14699 (comment)). In that comment, I proposed a solution which extended the existing `State` enumeration, but described an alternative that simplifies the overall tracking of incoming changes; this PR implements the latter. ### How should we test and review this PR? I've added an additional test and updated the existing ones. The tests in `parse_and_replace` already cover this case, particularly thanks to #14765 added by `@weihanglo.` It's still a good idea to experiment with `cargo clippy --fix` on repos that match the cases described in these open issues. ### Additional information Fixes #14699 Fixes rust-lang/rust-clippy/issues/13549
2 parents 94c2703 + f73b1d8 commit 3dedb85

File tree

6 files changed

+184
-147
lines changed

6 files changed

+184
-147
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ rand = "0.8.5"
8181
regex = "1.10.5"
8282
rusqlite = { version = "0.32.0", features = ["bundled"] }
8383
rustc-hash = "2.0.0"
84-
rustfix = { version = "0.8.2", path = "crates/rustfix" }
84+
rustfix = { version = "0.9.0", path = "crates/rustfix" }
8585
same-file = "1.0.6"
8686
schemars = "0.8.21"
8787
security-framework = "3.0.0"

crates/rustfix/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rustfix"
3-
version = "0.8.8"
3+
version = "0.9.0"
44
authors = [
55
"Pascal Hertleif <[email protected]>",
66
"Oliver Schneider <[email protected]>",

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)