dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.28k stars 1.58k forks source link

Self-assigning should trigger `unnecessary_statements` #59578

Open FMorschel opened 5 hours ago

FMorschel commented 5 hours ago

If you have a code like the following we should be receiving _unnecessarystatements triggers:

void f(int a) {
  a = a;  // <-- No lint
}

The background for this is that while working on https://github.com/dart-lang/sdk/issues/57090 I ended with a code similar to:

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.

dart-github-bot commented 5 hours ago

Summary: Self-assignment (a = a) doesn't trigger unnecessary_statements, but should. This impacts shadowed variables, causing confusion with null safety.