diff --git a/contracts/facets/DiamondLoupeFacet.sol b/contracts/facets/DiamondLoupeFacet.sol index 538516f..4bc207b 100644 --- a/contracts/facets/DiamondLoupeFacet.sol +++ b/contracts/facets/DiamondLoupeFacet.sol @@ -28,10 +28,11 @@ contract DiamondLoupeFacet is IDiamondLoupe, IERC165 { LibDiamond.DiamondStorage storage ds = LibDiamond.diamondStorage(); uint256 numFacets = ds.facetAddresses.length; facets_ = new Facet[](numFacets); - for (uint256 i; i < numFacets; i++) { + for (uint256 i; i < numFacets;) { address facetAddress_ = ds.facetAddresses[i]; facets_[i].facetAddress = facetAddress_; facets_[i].functionSelectors = ds.facetFunctionSelectors[facetAddress_].functionSelectors; + unchecked { ++i; } } } diff --git a/contracts/libraries/LibDiamond.sol b/contracts/libraries/LibDiamond.sol index e8d4463..4bbde98 100644 --- a/contracts/libraries/LibDiamond.sol +++ b/contracts/libraries/LibDiamond.sol @@ -72,7 +72,7 @@ library LibDiamond { address _init, bytes memory _calldata ) internal { - for (uint256 facetIndex; facetIndex < _diamondCut.length; facetIndex++) { + for (uint256 facetIndex; facetIndex < _diamondCut.length;) { IDiamondCut.FacetCutAction action = _diamondCut[facetIndex].action; if (action == IDiamondCut.FacetCutAction.Add) { addFunctions(_diamondCut[facetIndex].facetAddress, _diamondCut[facetIndex].functionSelectors); @@ -83,6 +83,7 @@ library LibDiamond { } else { revert("LibDiamondCut: Incorrect FacetCutAction"); } + unchecked { ++facetIndex; } } emit DiamondCut(_diamondCut, _init, _calldata); initializeDiamondCut(_init, _calldata); @@ -102,7 +103,8 @@ library LibDiamond { address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; require(oldFacetAddress == address(0), "LibDiamondCut: Can't add function that already exists"); addFunction(ds, selector, selectorPosition, _facetAddress); - selectorPosition++; + ++selectorPosition; + unchecked { ++selectorIndex; } } } @@ -115,13 +117,14 @@ library LibDiamond { if (selectorPosition == 0) { addFacet(ds, _facetAddress); } - for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) { + for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) { bytes4 selector = _functionSelectors[selectorIndex]; address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; require(oldFacetAddress != _facetAddress, "LibDiamondCut: Can't replace function with same function"); removeFunction(ds, oldFacetAddress, selector); addFunction(ds, selector, selectorPosition, _facetAddress); - selectorPosition++; + ++selectorPosition; + unchecked { ++selectorIndex; } } } @@ -130,10 +133,11 @@ library LibDiamond { DiamondStorage storage ds = diamondStorage(); // if function does not exist then do nothing and return require(_facetAddress == address(0), "LibDiamondCut: Remove facet address must be address(0)"); - for (uint256 selectorIndex; selectorIndex < _functionSelectors.length; selectorIndex++) { + for (uint256 selectorIndex; selectorIndex < _functionSelectors.length;) { bytes4 selector = _functionSelectors[selectorIndex]; address oldFacetAddress = ds.selectorToFacetAndPosition[selector].facetAddress; removeFunction(ds, oldFacetAddress, selector); + unchecked { ++selectorIndex; } } }