sdk icon indicating copy to clipboard operation
sdk copied to clipboard

Detect dead code in cases where an is check is redundant

Open jwren opened this issue 5 years ago • 3 comments

All blocks after the first if, are dead code.

bool _isStatic(Element element) {
  if (element is ClassMemberElement) {
    return element.isStatic;
  } else if (element is ClassMemberElement) {
    return element.isStatic;
  } else if (element is ClassMemberElement) {
    return element.isStatic;
  } else if (element is ClassMemberElement) {
    return element.isStatic;
  }

jwren avatar Mar 10 '20 16:03 jwren

I think the invariant_booleans lint rule should cover this. But it doesn't for now.

srawlins avatar Jun 10 '20 06:06 srawlins

I disagree. The invariant_booleans diagnostic is a lint because it has false positives, but it doesn't appear to me that this can. This seems much more akin to the kind of dead code analysis that we do for catch clauses:

class SuperException {}
class SubException extends SuperException {}
void f() {
  try {
    ...
  } on SuperException {
    ...
  } on SubException {
    // Caught as dead code because the clause above would catch anything intended to be caught here.
    ...
  }
}

bwilkerson avatar Jun 10 '20 15:06 bwilkerson

Ah, good point!

srawlins avatar Jun 10 '20 15:06 srawlins