The view modifier documentation states that :
Functions can be declared view in which case they promise not to
modify the state.
Additionally, the rules of assignment between storage and memory states :
Assignments between storage and memory (or from calldata) always
create an independent copy.
So :
function updateStruct(uint256 dummyParam) public onlyOwner {
// Creates a memory COPY of dummyStructArray[dummyParam]
DummyStruct memory dummy = dummyStructArray[dummyParam];
// Modified the memory COPY
dummy.something = 1;
}
You did read the state when accessing dummyStructArray[dummyParam]
, but you only modifie a memory copy, not the state variable. Therefore, your function fits the view requirements as it does not modify the state.
The function state mutability can be restricted to view
warning is perfectly valid.
The same behavior can be seen on this code, you can try it on Remix:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Example {
uint256 public stateVariable = 0;
function modifiesInMemory() public {
uint256 memoryCopy = stateVariable;
memoryCopy = 10;
}
}
EDIT : Adding simple example as per OP's comment.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Example {
struct DummyStruct {
uint256 something;
}
DummyStruct[1] public dummyStructArray;
// Creates a memory copy and operates on it
// "Assignments between storage and memory (or from calldata) always create an independent copy."
function modifiesInMemory() public {
DummyStruct memory memoryCopy = dummyStructArray[0];
memoryCopy.something = 10;
// ASSIGN THE MEMORY COPY BACK TO THE STATE VARIABLE
dummyStructArray[0] = memoryCopy;
}
// Creates a local storage reference to the state variable
// "Assignments from storage to a local storage variable also only assign a reference."
function modifiedInLocalStorage() public {
DummyStruct storage localStorageReference = dummyStructArray[0];
localStorageReference.something = 10;
}
// Directly modifies the state variable
function modifiesInState() public {
// MODIFIES THE STATE VARIABLE IN PLACE
dummyStructArray[0].something = 10;
}
}