sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[analyzer] Incorrect `unused_local_variable` warning.

Open rakudrama opened this issue 2 years ago • 2 comments

a is used here:

main() {
  final a = foo(null);
  a!;
}

foo(x) => x;
$ dart analyze ~/play1/bug1c8i.dart 
Analyzing bug1c8i.dart...              0.6s

warning • bug1c8i.dart:2:9 • The value of the local variable 'a' isn't used. Try removing the variable or using it. •
          unused_local_variable

1 issue found.

rakudrama avatar Apr 19 '23 01:04 rakudrama

I agree. I think we have to consider a null check to be a use because removing the null check could change the semantics of the program. But only in the case where the variable's type is nullable. If it's non-nullable then the null check is invalid and shouldn't be counted as a use.

bwilkerson avatar Apr 19 '23 16:04 bwilkerson

I'm going the other way :)

A downside of calling this a "use" is that the rule will allow (some, maybe very few) unused variables to go unreported. As in, variables whose only "use" is an unintentional null-assert statement-expression.

A downside of calling this a non-use is that we will not support the pattern of checking if an otherwise-unused local variable is null, and throwing if it is, by operating on it with a null-assert statement-expression.

I think the first downside outweighs the second.

srawlins avatar Apr 24 '23 02:04 srawlins