sway icon indicating copy to clipboard operation
sway copied to clipboard

Should we emit warnings for storage variables that are only written?

Open mohammadfawaz opened this issue 2 years ago • 7 comments

We currently emit a warning for storage variables that are never read (even if they are written). For example, the following emits a warning:

contract;

storage {
    x: u64 = 0,
}

abi MyContract {
    #[storage(write)]
    fn set();
}

impl MyContract for Contract {
    #[storage(write)]
    fn set() {
        storage.x = 42;
    }
}

Warning we get:

2 |
3 | storage {
4 |     x: u64 = 0,
  |     - This storage declaration is never accessed and can be removed.
5 | }
  |
____

Is this expected? Is there any value to have storage variables that are only ever written (or not be written at all)? Or should we continue emitting warnings for them? At the very least, the warning above should say "... never read ..." instead of "... never accessed ...".

mohammadfawaz avatar Jul 30 '22 17:07 mohammadfawaz

When/if the SDK allows to directly access storage variable values, unaccessed variables might be useful offchain

SwayStar123 avatar Jul 31 '22 19:07 SwayStar123

That's right.. partially what prompted me to open this issue. In this case, maybe we don't need to emit any warnings here even if the storage variable is never read or written.

mohammadfawaz avatar Aug 04 '22 13:08 mohammadfawaz

I think we could add something like #[write-only] annotation for storage variables like that, so that the user can convey the mode of use explicitly. What do you think?

anton-trunov avatar Nov 28 '22 16:11 anton-trunov

In my experience with contract audits, having read-only or write-only storage vars sometimes might indicate a bug, so it's worth it to emit warnings in these cases, unless the user is explicit about that.

anton-trunov avatar Nov 28 '22 16:11 anton-trunov

I think we could add something like #[write-only] annotation for storage variables like that, so that the user can convey the mode of use explicitly. What do you think?

@anton-trunov oh so that's annotation at the storage variable level? Interesting. I think Rust has similar annotations for dead code analysis.

It's reasonable to assume that storage variables that are only written but not read are generally indicative of a bug. I just worry that we still don't have a way to silence specific warnings.

mohammadfawaz avatar Dec 02 '22 16:12 mohammadfawaz

@mohammadfawaz It seems #3453 deserves at least P: high tag (which I just added)?

anton-trunov avatar Dec 02 '22 20:12 anton-trunov

A user was also confused by this: https://forum.fuel.network/t/forc-build-linter-issues-always-says-unused/2779

It's reasonable to assume that storage variables that are only written but not read are generally indicative of a bug. I just worry that we still don't have a way to silence specific warnings.

If we're going to keep this behavior, we should consider updating the warning message to be more clear. never accessed sounds like never used. Maybe something like This storage declaration is never read which could indicate a bug. for any storage variables that aren't read.

sdankel avatar Jul 05 '23 18:07 sdankel