Skip to content

Commit

Permalink
Prevent rustfmt from touching inside verus!{...}
Browse files Browse the repository at this point in the history
Fixes #33
  • Loading branch information
jaybosamiya committed Feb 26, 2024
1 parent 9849171 commit e5d9e1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,11 +1431,12 @@ fn strip_whitespace(s: String) -> String {
pub const VERUS_PREFIX: &str = "verus! {\n\n";
pub const VERUS_SUFFIX: &str = "\n} // verus!\n";

/// Run rustfmt
/// Run rustfmt, only on code outside the `verus!` macro
pub fn rustfmt(value: &str) -> Option<String> {
if let Ok(mut proc) = Command::new("rustfmt")
.arg("--emit=stdout")
.arg("--edition=2021")
.arg(r#"--config=skip_macro_invocations=["verus"]"#)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
Expand Down
18 changes: 18 additions & 0 deletions tests/rustfmt-does-not-touch-verus.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#[test]
/// Regression test for https://github.com/verus-lang/verusfmt/issues/33
fn mod_macro_item_idempotent() {
let file = r#"
mod foo {
verus! {
bar! {
baz
}
}
}
"#;
let formatted1 = verusfmt::rustfmt(&verusfmt::parse_and_format(file).unwrap()).unwrap();
let formatted2 = verusfmt::rustfmt(&verusfmt::parse_and_format(&formatted1).unwrap()).unwrap();
assert_eq!(formatted1, formatted2);
}

0 comments on commit e5d9e1d

Please sign in to comment.