Skip to content

Commit b0c0b9c

Browse files
authored
group unchecked blocks (#462)
1 parent 5d55c56 commit b0c0b9c

3 files changed

Lines changed: 33 additions & 5 deletions

File tree

src/nodes/UncheckedStatement.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
const {
22
doc: {
3-
builders: { concat }
3+
builders: { concat, group }
44
}
55
} = require('prettier/standalone');
66

77
const UncheckedStatement = {
8-
print: ({ path, print }) => concat(['unchecked ', path.call(print, 'block')])
8+
print: ({ path, print }) =>
9+
group(concat(['unchecked ', path.call(print, 'block')]))
910
};
1011

1112
module.exports = UncheckedStatement;

tests/AllSolidityFeatures/AllSolidityFeatures.sol

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,13 @@ contract WithUncheckedBlock {
520520
unchecked { x--; }
521521
return x;
522522
}
523-
}
523+
524+
function g() public pure returns (uint) {
525+
uint x = 0;
526+
unchecked { x--; return x; }
527+
}
528+
529+
function mul(uint256 x, uint256 y) internal pure returns (uint256) {
530+
unchecked { return x * y; }
531+
}
532+
}

tests/AllSolidityFeatures/__snapshots__/jsfmt.spec.js.snap

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,8 +528,16 @@ contract WithUncheckedBlock {
528528
unchecked { x--; }
529529
return x;
530530
}
531-
}
531+
532+
function g() public pure returns (uint) {
533+
uint x = 0;
534+
unchecked { x--; return x; }
535+
}
532536
537+
function mul(uint256 x, uint256 y) internal pure returns (uint256) {
538+
unchecked { return x * y; }
539+
}
540+
}
533541
=====================================output=====================================
534542
// Examples taken from the Solidity documentation online.
535543
@@ -1098,11 +1106,21 @@ uint256 constant MULTIPLIER = 2**EXPONENT;
10981106
10991107
contract WithUncheckedBlock {
11001108
function f() public pure returns (uint256) {
1109+
uint256 x = 0;
1110+
unchecked {x--;}
1111+
return x;
1112+
}
1113+
1114+
function g() public pure returns (uint256) {
11011115
uint256 x = 0;
11021116
unchecked {
11031117
x--;
1118+
return x;
11041119
}
1105-
return x;
1120+
}
1121+
1122+
function mul(uint256 x, uint256 y) internal pure returns (uint256) {
1123+
unchecked {return x * y;}
11061124
}
11071125
}
11081126

0 commit comments

Comments
 (0)