Skip to content

Commit 818eeb9

Browse files
authored
fix(fmt): write files on disk only if they're not perfect match (#8775)
* fix(fmt): write files on disk only if they're not perfect match * Cleanup
1 parent 98ab45e commit 818eeb9

File tree

4 files changed

+13
-8
lines changed

4 files changed

+13
-8
lines changed

crates/fmt/src/formatter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ impl<'a, W: Write> Formatter<'a, W> {
12461246
})?;
12471247

12481248
write_chunk!(self, "}}")?;
1249-
return Ok(true)
1249+
return Ok(false)
12501250
}
12511251

12521252
// Determine writable statements by excluding statements from disabled start / end lines.

crates/fmt/testdata/Repros/fmt.sol

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ contract IfElseTest {
139139
number = 1;
140140
} else if (newNumber = 2) {
141141
// number = 2;
142-
}
143-
else {
142+
} else {
144143
newNumber = 3;
145144
}
146145
}

crates/forge/bin/cmd/fmt.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,22 @@ impl FmtArgs {
125125
)
126126
})?;
127127

128+
let diff = TextDiff::from_lines(&source, &output);
129+
let new_format = diff.ratio() < 1.0;
128130
if self.check || path.is_none() {
129131
if self.raw {
130132
print!("{output}");
131133
}
132134

133-
let diff = TextDiff::from_lines(&source, &output);
134-
if diff.ratio() < 1.0 {
135+
// If new format then compute diff summary.
136+
if new_format {
135137
return Ok(Some(format_diff_summary(&name, &diff)))
136138
}
137139
} else if let Some(path) = path {
138-
fs::write(path, output)?;
140+
// If new format then write it on disk.
141+
if new_format {
142+
fs::write(path, output)?;
143+
}
139144
}
140145
Ok(None)
141146
};

testdata/default/fuzz/invariant/common/InvariantPreserveState.t.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ contract Handler is DSTest {
1515
Vm constant vm = Vm(HEVM_ADDRESS);
1616

1717
function thisFunctionReverts() external {
18-
if (block.number < 10) {}
19-
else revert();
18+
if (block.number < 10) {} else {
19+
revert();
20+
}
2021
}
2122

2223
function advanceTime(uint256 blocks) external {

0 commit comments

Comments
 (0)