Skip to content

Commit bb6a9a2

Browse files
committed
refactor: remove all use of unchecked increment
1 parent 70bc1c8 commit bb6a9a2

File tree

7 files changed

+21
-85
lines changed

7 files changed

+21
-85
lines changed

src/dynamic-traits/DynamicTraits.sol

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ contract DynamicTraits is IERC7496 {
6868
traitValues = new bytes32[](length);
6969

7070
// Assign each trait value to the corresopnding key.
71-
for (uint256 i = 0; i < length;) {
71+
for (uint256 i = 0; i < length; ++i) {
7272
bytes32 traitKey = traitKeys[i];
7373
traitValues[i] = getTraitValue(tokenId, traitKey);
74-
unchecked {
75-
++i;
76-
}
7774
}
7875
}
7976

@@ -147,11 +144,8 @@ contract DynamicTraits is IERC7496 {
147144

148145
// Register all trait keys.
149146
uint256 length = traitKeys.length;
150-
for (uint256 i = 0; i < length;) {
147+
for (uint256 i = 0; i < length; ++i) {
151148
layout._validTraitKeys[traitKeys[i]] = true;
152-
unchecked {
153-
++i;
154-
}
155149
}
156150

157151
// Set the new trait metadata URI.

src/dynamic-traits/OnchainTraits.sol

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,14 @@ abstract contract OnchainTraits is Ownable, DynamicTraits {
224224
string[] memory attributes = new string[](keysLength);
225225
// keep track of how many traits are actually set
226226
uint256 num;
227-
for (uint256 i = 0; i < keysLength;) {
227+
for (uint256 i = 0; i < keysLength; ++i) {
228228
bytes32 key = keys[i];
229229
bytes32 trait = getTraitValue(tokenId, key);
230230
// check that the trait is set, otherwise, skip it
231231
if (trait != bytes32(0)) {
232232
attributes[num] =
233233
TraitLabelStorageLib.toAttributeJson(OnchainTraitsStorage.layout()._traitLabelStorage, key, trait);
234-
unchecked {
235-
++num;
236-
}
237-
}
238-
unchecked {
239-
++i;
234+
++num;
240235
}
241236
}
242237
///@solidity memory-safe-assembly

src/dynamic-traits/lib/TraitLabelLib.sol

Lines changed: 9 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,12 @@ library TraitLabelStorageLib {
8888
if (traitLabel.fullTraitValues.length != 0) {
8989
// try to find matching FullTraitValue
9090
uint256 length = traitLabel.fullTraitValues.length;
91-
for (uint256 i = 0; i < length;) {
91+
for (uint256 i = 0; i < length; ++i) {
9292
FullTraitValue memory fullTraitValue = traitLabel.fullTraitValues[i];
9393
if (fullTraitValue.traitValue == traitValue) {
9494
actualTraitValue = fullTraitValue.fullTraitValue;
9595
break;
9696
}
97-
unchecked {
98-
++i;
99-
}
10097
}
10198
}
10299
// if no match, use traitValue as-is
@@ -121,13 +118,10 @@ library TraitLabelStorageLib {
121118
{
122119
string[] memory result = new string[](keys.length);
123120
uint256 i;
124-
for (i; i < keys.length;) {
121+
for (i; i < keys.length; ++i) {
125122
bytes32 key = keys[i];
126123
TraitLabel memory traitLabel = TraitLabelStorageLib.toTraitLabel(traitLabelStorage[key]); //.toTraitLabel();
127124
result[i] = TraitLabelLib.toLabelJson(traitLabel, key);
128-
unchecked {
129-
++i;
130-
}
131125
}
132126
return json.arrayOf(result);
133127
}
@@ -152,11 +146,8 @@ library FullTraitValueLib {
152146
*/
153147
function toJson(FullTraitValue[] memory fullTraitValues) internal pure returns (string memory) {
154148
string[] memory result = new string[](fullTraitValues.length);
155-
for (uint256 i = 0; i < fullTraitValues.length;) {
149+
for (uint256 i = 0; i < fullTraitValues.length; ++i) {
156150
result[i] = toJson(fullTraitValues[i]);
157-
unchecked {
158-
++i;
159-
}
160151
}
161152
return json.arrayOf(result);
162153
}
@@ -185,13 +176,10 @@ library TraitLabelLib {
185176
if (length != 0) {
186177
string memory stringValue = TraitLib.toString(traitValue, displayType);
187178
bytes32 hashedValue = keccak256(abi.encodePacked(stringValue));
188-
for (uint256 i = 0; i < length;) {
179+
for (uint256 i = 0; i < length; ++i) {
189180
if (hashedValue == keccak256(abi.encodePacked(acceptableValues[i]))) {
190181
return;
191182
}
192-
unchecked {
193-
++i;
194-
}
195183
}
196184
revert InvalidTraitValue(traitKey, traitValue);
197185
}
@@ -263,13 +251,10 @@ library TraitLib {
263251
*/
264252
function _bytes32StringLength(bytes32 str) internal pure returns (uint256) {
265253
// only meant to be called in a view context, so this optimizes for bytecode size over performance
266-
for (uint256 i; i < 32;) {
254+
for (uint256 i; i < 32; ++i) {
267255
if (str[i] == 0) {
268256
return i;
269257
}
270-
unchecked {
271-
++i;
272-
}
273258
}
274259
return 32;
275260
}
@@ -289,11 +274,8 @@ library EditorsLib {
289274
function aggregate(AllowedEditor[] memory editors) internal pure returns (Editors) {
290275
uint256 editorsLength = editors.length;
291276
uint256 result;
292-
for (uint256 i = 0; i < editorsLength;) {
277+
for (uint256 i = 0; i < editorsLength; ++i) {
293278
result |= 1 << uint8(editors[i]);
294-
unchecked {
295-
++i;
296-
}
297279
}
298280
return Editors.wrap(uint8(result));
299281
}
@@ -311,16 +293,11 @@ library EditorsLib {
311293
// optimistically allocate 4 slots
312294
AllowedEditor[] memory result = new AllowedEditor[](4);
313295
uint256 num;
314-
for (uint256 i = 1; i < 5;) {
296+
for (uint256 i = 1; i < 5; ++i) {
315297
bool set = _editors & (1 << i) != 0;
316298
if (set) {
317299
result[num] = AllowedEditor(i);
318-
unchecked {
319-
++num;
320-
}
321-
}
322-
unchecked {
323-
++i;
300+
++num;
324301
}
325302
}
326303
///@solidity memory-safe-assembly
@@ -356,11 +333,8 @@ library EditorsLib {
356333
*/
357334
function toJson(AllowedEditor[] memory editors) internal pure returns (string memory) {
358335
string[] memory result = new string[](editors.length);
359-
for (uint256 i = 0; i < editors.length;) {
336+
for (uint256 i = 0; i < editors.length; ++i) {
360337
result[i] = LibString.toString(uint8(editors[i]));
361-
unchecked {
362-
++i;
363-
}
364338
}
365339
return json.arrayOf(result);
366340
}

src/dynamic-traits/test/ERC721DynamicTraitsMultiUpdate.sol

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ contract ERC721DynamicTraitsMultiUpdate is ERC721DynamicTraits {
1111
virtual
1212
onlyOwner
1313
{
14-
for (uint256 tokenId = fromTokenId; tokenId <= toTokenId;) {
14+
for (uint256 tokenId = fromTokenId; tokenId <= toTokenId; ++tokenId) {
1515
// Revert if the token doesn't exist.
1616
_requireOwned(tokenId);
1717

1818
// Call the internal function to set the trait.
1919
_setTrait(tokenId, traitKey, value);
20-
21-
unchecked {
22-
++tokenId;
23-
}
2420
}
2521

2622
// Emit the event noting the update.
@@ -33,33 +29,25 @@ contract ERC721DynamicTraitsMultiUpdate is ERC721DynamicTraits {
3329
bytes32 traitKey,
3430
bytes32[] calldata values
3531
) public virtual onlyOwner {
36-
for (uint256 tokenId = fromTokenId; tokenId <= toTokenId;) {
32+
for (uint256 tokenId = fromTokenId; tokenId <= toTokenId; ++tokenId) {
3733
// Revert if the token doesn't exist.
3834
_requireOwned(tokenId);
3935

4036
// Call the internal function to set the trait.
4137
_setTrait(tokenId, traitKey, values[tokenId - 1]);
42-
43-
unchecked {
44-
++tokenId;
45-
}
4638
}
4739

4840
// Emit the event noting the update.
4941
emit TraitUpdatedRange(traitKey, fromTokenId, toTokenId);
5042
}
5143

5244
function setTraitsList(uint256[] calldata tokenIds, bytes32 traitKey, bytes32 value) public virtual onlyOwner {
53-
for (uint256 i = 0; i < tokenIds.length;) {
45+
for (uint256 i = 0; i < tokenIds.length; ++i) {
5446
// Revert if the token doesn't exist.
5547
_requireOwned(tokenIds[i]);
5648

5749
// Call the internal function to set the trait.
5850
_setTrait(tokenIds[i], traitKey, value);
59-
60-
unchecked {
61-
++i;
62-
}
6351
}
6452

6553
// Emit the event noting the update.
@@ -71,16 +59,12 @@ contract ERC721DynamicTraitsMultiUpdate is ERC721DynamicTraits {
7159
virtual
7260
onlyOwner
7361
{
74-
for (uint256 i = 0; i < tokenIds.length;) {
62+
for (uint256 i = 0; i < tokenIds.length; ++i) {
7563
// Revert if the token doesn't exist.
7664
_requireOwned(tokenIds[i]);
7765

7866
// Call the internal function to set the trait.
7967
_setTrait(tokenIds[i], traitKey, values[i]);
80-
81-
unchecked {
82-
++i;
83-
}
8468
}
8569

8670
// Emit the event noting the update.

src/onchain/json.sol

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,8 @@ library json {
106106
*/
107107
function quote(string[] memory strs) internal pure returns (string[] memory) {
108108
string[] memory result = new string[](strs.length);
109-
for (uint256 i = 0; i < strs.length;) {
109+
for (uint256 i = 0; i < strs.length; ++i) {
110110
result[i] = quote(strs[i]);
111-
unchecked {
112-
++i;
113-
}
114111
}
115112
return result;
116113
}

src/reference/ExampleNFT.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,7 @@ contract ExampleNFT is AbstractNFT {
7979
}
8080

8181
function mint(address to) public {
82-
unchecked {
83-
_mint(to, ++currentId);
84-
}
82+
_mint(to, ++currentId);
8583
}
8684

8785
function _isOwnerOrApproved(uint256 tokenId, address addr) internal view virtual override returns (bool) {

src/sips/lib/SIP6Decoder.sol

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,9 @@ library SIP6Decoder {
297297
function _validateFixedArrays(bytes[] memory fixedArrays, bytes32 expectedFixedDataHash) internal pure {
298298
bytes32[] memory hashes = new bytes32[](fixedArrays.length);
299299
uint256 fixedArraysLength = fixedArrays.length;
300-
for (uint256 i = 0; i < fixedArraysLength;) {
300+
for (uint256 i = 0; i < fixedArraysLength; ++i) {
301301
bytes memory fixedArray = fixedArrays[i];
302302
hashes[i] = keccak256(fixedArray);
303-
unchecked {
304-
++i;
305-
}
306303
}
307304
bytes32 compositeHash;
308305
///@solidity memory-safe-assembly
@@ -338,7 +335,7 @@ library SIP6Decoder {
338335
arrayLengthInBytes: decoded.length << 5,
339336
substandard: substandard
340337
});
341-
for (uint256 i = 0; i < decoded.length;) {
338+
for (uint256 i = 0; i < decoded.length; ++i) {
342339
uint256 subArrayOffset;
343340
uint256 subArrayLength;
344341
assembly {
@@ -378,9 +375,6 @@ library SIP6Decoder {
378375
arrayLengthInBytes: subArrayLength,
379376
substandard: substandard
380377
});
381-
unchecked {
382-
++i;
383-
}
384378
}
385379
}
386380

0 commit comments

Comments
 (0)