Skip to content

Commit

Permalink
fix: don't let nargo fmt produce multiple trailing newlines (#7444)
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite authored Feb 19, 2025
1 parent fdfe2bf commit 093a8ec
Show file tree
Hide file tree
Showing 50 changed files with 15 additions and 47 deletions.
1 change: 0 additions & 1 deletion noir_stdlib/src/ecdsa_secp256k1.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,3 @@ pub fn verify_signature_slice(
) -> bool
// docs:end:ecdsa_secp256k1_slice
{}

1 change: 0 additions & 1 deletion noir_stdlib/src/meta/op.nr
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,3 @@ impl BinaryOp {
}
}
}

1 change: 0 additions & 1 deletion noir_stdlib/src/ops/arith.nr
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,3 @@ impl Neg for i64 {
}
}
// docs:end:neg-trait-impls

1 change: 0 additions & 1 deletion noir_stdlib/src/ops/bit.nr
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,3 @@ impl Shr for i64 {
self >> other
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,3 @@ unconstrained fn shr(x: u32, y: u8) -> u32 {
unconstrained fn shl(x: u32, y: u8) -> u32 {
x << y
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ fn main() {
let _: U128 = U128::from_integer(1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,3 @@ fn hello_world() {}
comptime fn call(x: Quoted) -> Quoted {
quote { $x() }
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ fn main() {
let _: Field = U128::from_hex("0x0").to_integer();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@ pub fn neg_at_comptime() {
let _result = -value;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ fn main() {
}
assert(c1 == 1);
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ fn main() {
let bar = Foo { a: [0; 10], b: 28 };
assert(foo != bar);
}

Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,3 @@ fn main(x: Field) {
assert(bar_mut.f4() == 4, "14");
assert(bar_mut.f5() == 50, "15");
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ comptime fn foo(_: FunctionDefinition) -> Quoted {
}
}
}

1 change: 0 additions & 1 deletion test_programs/execution_success/bit_and/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ fn main(x: Field, y: Field, a: Field, b: Field) {
let b_as_u8 = b as u8;
assert((a_as_u8 & b_as_u8) == a_as_u8);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/bool_not/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
fn main(x: u1) {
assert(!x == 0);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/bool_or/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ fn main(x: u1, y: u1) {

assert(x | y | x == 1);
}

Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,3 @@ unconstrained fn entry_point(x: u32) -> u32 {

result
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ fn main(x: Field, y: Field) {
create_and_assert_inside_brillig(x, y);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ unconstrained fn main(
let hash = std::hash::pedersen_commitment_with_separator([state], 0);
assert(std::hash::pedersen_commitment_with_separator([43], 0).x == hash.x);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/cast_bool/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ fn main(x: Field, y: Field) {
let t = z as u8;
assert(t == 1);
}

Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,3 @@ fn issue_661_bar(a: [u32; 4]) -> [u32; 4] {
b[0] = a[0] + 1;
b
}

1 change: 0 additions & 1 deletion test_programs/execution_success/databus/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ fn main(mut x: u32, y: call_data(0) u32, z: call_data(0) [u32; 4]) -> return_dat
unconstrained fn foo(x: u32) -> u32 {
x + 1
}

Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,3 @@ fn map_fields(mut input: ParentStruct) -> ParentStruct {

input
}

Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ unconstrained fn entry_point_one_diff_global(x: Field, y: Field) {
let z = THREE + x + y;
assert(z == 4);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/if_else_chain/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ fn main(a: u32, mut c: [u32; 4]) {
assert(c[0] == 10);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,3 @@ fn main(mut x: [Foo; 4], y: pub Field) {
foo_parents[y - 2].foos[y - 3].b = foo_parents[y - 2].foos[y - 2].b;
assert(foo_parents[1].foos[0].b == [20, 19, 5000]);
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ fn main(values: [Field; 6]) {
let notes = unsafe { create_inside_brillig(values) };
assert(access_nested(notes) == (2 + 4 + 3 + 1));
}

1 change: 0 additions & 1 deletion test_programs/execution_success/pedersen_check/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ fn main(x: Field, y: Field, salt: Field, out_x: Field, out_y: Field, out_hash: F
let hash = std::hash::pedersen_commitment([state]);
assert(std::hash::pedersen_commitment([43]).x == hash.x);
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ fn main(x: Field, y: Field, expected_commitment: std::embedded_curve_ops::Embedd
assert_eq(commitment.y, expected_commitment.y);
}
// docs:end:pedersen-commitment

1 change: 0 additions & 1 deletion test_programs/execution_success/pedersen_hash/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@ fn main(x: Field, y: Field, expected_hash: Field) {
assert_eq(hash, expected_hash);
}
// docs:end:pedersen-hash

Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,3 @@ fn main(input: [Field; 4], randomness: Field, context_input: u32) {
let event0 = ExampleEvent0 { value0: input[0], value1: input[1] };
event0.emit(encode_event_with_randomness(&mut context, randomness));
}

Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,3 @@ fn dynamic_slice_merge_push_then_pop(mut slice: [Field], x: Field, y: Field) {
let (_, elem) = slice.pop_back();
assert(elem == y);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/slice_regex/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,3 @@ fn main() {
// assert_eq(result.leftover.len, 0);
// // adapted URL parser: (https?:\/\/)?([\da-c.\-]+)\.([a-c.]+)([\/\w \.\-]*)*\/?
// }

Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@ fn main(y: pub myStruct) {
assert(y.foo == 5);
assert(y.bar == 7);
}

1 change: 0 additions & 1 deletion test_programs/execution_success/submodules/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ mod my_submodule {

pub fn my_helper() {}
}

1 change: 0 additions & 1 deletion test_programs/execution_success/u128/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@ fn main(mut x: u32, y: u32, z: u32, big_int: U128, hexa: str<7>) {
assert(shift << 127 == U128::from_integer(0));
assert(U128::from_integer(3).to_integer() == 3);
}

Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ fn main(x: u8, y: u8) {
assert(std::wrapping_add(y, 1) == x);
assert(std::wrapping_mul(y, y) == 1);
}

1 change: 0 additions & 1 deletion test_programs/noir_test_success/bounded_vec/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,3 @@ fn test_vec_any_not_default() {
vec.extend_from_array([2, 4]);
assert(!vec.any(|v| v == default_value));
}

Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ unconstrained fn test_overflow_mul() {
let b: u8 = 2;
assert_eq(a * b, 0);
}

1 change: 0 additions & 1 deletion test_programs/noir_test_success/mock_oracle/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,3 @@ fn test_mock_struct_field() {
assert_eq(20, struct_field(point, another_array));
}
}

1 change: 1 addition & 0 deletions tooling/nargo_fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl<'a> Formatter<'a> {
);

self.format_parsed_module(parsed_module, self.ignore_next);
self.buffer.trim_multiple_newlines();
}

pub(crate) fn format_parsed_module(&mut self, parsed_module: ParsedModule, ignore_next: bool) {
Expand Down
7 changes: 7 additions & 0 deletions tooling/nargo_fmt/src/formatter/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ impl Buffer {
}
}

/// Trim multiple newlines from the end of the buffer, keeping at most one.
pub(crate) fn trim_multiple_newlines(&mut self) {
while self.buffer.ends_with("\n\n") {
self.buffer.truncate(self.buffer.len() - 1);
}
}

pub(crate) fn contents(self) -> String {
self.buffer
}
Expand Down
7 changes: 7 additions & 0 deletions tooling/nargo_fmt/src/formatter/comments_and_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,11 @@ global x = 1;
";
assert_format(src, expected);
}

#[test]
fn trims_newlines_from_the_end_of_the_file() {
let src = "global x: Field = 1;\n\n\n";
let expected = "global x: Field = 1;\n";
assert_format(src, expected);
}
}
1 change: 0 additions & 1 deletion tooling/nargo_fmt/src/formatter/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ fn bar() {
// noir-fmt:ignore
fn baz() { let z = 3 ;
}
";
assert_format(src, expected);
}
Expand Down
1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/databus.nr
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
fn main(x: pub u8, y: call_data(0) u8) -> return_data u32 {}

1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/fn.nr
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,3 @@ fn four() {}

#[test(should_fail_with = "oops")]
fn five() {}

1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/ignore.nr
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ fn mk_array() {
];
let array = [1];
}

1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/trait.nr
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
trait Foo: Bar + Baz {}

1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/trait_alias.nr
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,3 @@ fn main() {

assert(0.foo_3().bar_3() == baz_3(0));
}

1 change: 0 additions & 1 deletion tooling/nargo_fmt/tests/expected/unsafe.nr
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ fn main(x: pub u8, y: u8) {
assert_eq(x, y);
}
}

0 comments on commit 093a8ec

Please sign in to comment.