sdk icon indicating copy to clipboard operation
sdk copied to clipboard

`unused_local_variable` flags parts of patterns that aren't used

Open Hixie opened this issue 1 year ago • 3 comments

Consider the following snippet, where results is a List of records that statically match the pattern:

          for (final (Team? team, Award? otherAward, int rank, tied: bool tied) in results) {
            if (team == null && otherAward == null) {
              continue award;
            }
          }

because "rank" and "tied" aren't used, they get flagged as unused_local_variable. However, much as I would never want to replace an unused parameter name with _ in a closure (because it makes the code more opaque), I would never want to replace "int rank" and "bool tied" with _ here either.

I think this particular case of unused variables should be move to a separate lint so it can be enabled/disabled based on personal preference.

Hixie avatar Feb 08 '24 01:02 Hixie

Not necessarily disagreeing with you (I haven't actually made up my mind yet), but I'll note that this lint could be satisfied without using underscores:

  for (final (Team? team, Award? otherAward, int(), tied: bool())
      in results) {
    if (team == null && otherAward == null) {
      continue award;
    }
  }

bwilkerson avatar Feb 08 '24 17:02 bwilkerson

True. I guess the thing I'm looking to keep is the self-documenting name, rather than avoiding underscores specifically.

Hixie avatar Feb 08 '24 20:02 Hixie

(Not a big deal for the named field, more of an issue for the positional ones.)

Hixie avatar Feb 08 '24 20:02 Hixie

I think this is a pretty valid pragmatic issue. A valid false positive.

srawlins avatar Apr 19 '24 01:04 srawlins

I propose this behavior for pattern variable declarations: if every variable declared is unused, report them. Otherwise, report none of them. This allows one to specify pattern matches in a consistent format, and allows for names, for documentation purposes.

srawlins avatar Apr 19 '24 14:04 srawlins

Are you proposing this for all pattern variable declarations, or just fields in a record pattern? If the latter, just for positional fields, or for both positional and named fields?

bwilkerson avatar Apr 19 '24 15:04 bwilkerson

For all pattern variable declarations.

srawlins avatar Apr 19 '24 15:04 srawlins