sdk
sdk copied to clipboard
`unused_local_variable` flags parts of patterns that aren't used
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.
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;
}
}
True. I guess the thing I'm looking to keep is the self-documenting name, rather than avoiding underscores specifically.
(Not a big deal for the named field, more of an issue for the positional ones.)
I think this is a pretty valid pragmatic issue. A valid false positive.
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.
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?
For all pattern variable declarations.