Linter: Lint for empty statements (was Linter should catch `if (foo);`)

Created on 14 Jun 2016  Â·  7Comments  Â·  Source: dart-lang/linter

I just got caught by the following code:

  if (complicated.expression.foo());
    bar();

It would be great if the linter could catch cases of an "if" block doing nothing.

enhancement lint request

Most helpful comment

Personally, I think it's always questionable to have an empty statement, so I would just flag empty statements where ever they occur. I'm kind of surprised that we don't already have that somewhere.

All 7 comments

Some code styles prevent this by requiring if statements to always have bodies. Don't you like that approach?

How would that stop this case?

if (complicated.expression.foo()); {
    bar();
}

...has the same issue.

If I'm not missing something I think both of those cases are essentially the same at the AST level: the rub is that the if has an empty then statement.

Incidentally, using a formatter really helps make these cases more obvious as dartftmt wrestles your examples into these respectively.

  if (complicated.expression.foo()) ;
  bar();
if (complicated.expression.foo()) ; 
{
    bar();
}

But anyway, I think a rule checking for empty then statement is totally reasonable.

Curious if @bwilkerson cares to chime in? It's early. First cup of coffee, so I'm sure I'm overlooking something!

☕

FWIW: I'm playing with an implementation. (We can always toss it if we come to another conclusion on it's value.)

Personally, I think it's always questionable to have an empty statement, so I would just flag empty statements where ever they occur. I'm kind of surprised that we don't already have that somewhere.

I'm with Brian.

Totally agree that we should just catch empty statements.

(And yeah, a formatter would help here too. But that assumes you look at the formatted code...)

Was this page helpful?
0 / 5 - 0 ratings