int i;
if (other case int? i?) { // It was an actual case but this is not the point
i = i; // shadowed i self-assigning
} else {
return;
}
i; // <-- The non-nullable local variable 'i' must be assigned before it can be used.
In my case the variable in question was even the same name as a propriety for the class case I was testing. So by doing like MyClass(: var i) I completely shadowed my variable and was left wondering why I got that error bellow.
Summary: Self-assignment (a = a) doesn't trigger unnecessary_statements, but should. This impacts shadowed variables, causing confusion with null safety.
If you have a code like the following we should be receiving _unnecessarystatements triggers:
The background for this is that while working on https://github.com/dart-lang/sdk/issues/57090 I ended with a code similar to:
In my case the variable in question was even the same name as a propriety for the class
case
I was testing. So by doing likeMyClass(: var i)
I completely shadowed my variable and was left wondering why I got that error bellow.