linter icon indicating copy to clipboard operation
linter copied to clipboard

False negative for `curly_braces_in_control_flow_structures`

Open lrhn opened this issue 1 year ago • 9 comments
trafficstars

The lint curly_braces_in_control_flow_structures should warn if an if statement body has no braces. An exception is granted if the entire if statement fits in one line, based on the corresponding Effective Dart rule.

No lint warning is given for the if statement of this code:

Object? foo() {  
  if ("long condition and other long condition" != "another long condition" &&
     "more tests".isNotEmpty) return null;
  return 42;
}

The body of this if statement fits in one line, but the entire statement does not, so the lint should trigger. It does not.

(Found this by seeing similar code in a code review.)

lrhn avatar Feb 07 '24 14:02 lrhn

But what if someone wants to enforce that curly_braces_in_control_flow_structures enforces all control flow to have braces? Due to the exception in Effective Dart, the statement that one should use braces for strictly all statements is false, because there is an exception.

IMO, the whole point of the lint was to avoid if (condition) return; ? (yes, the exception in the Style Guide is a bit unfortunate)

Personally, I'd like to keep the lint strict and keep the old behaviour. If someone wants to allow if (condition) return; then they should just disable the lint?

navaronbracke avatar Feb 20 '24 18:02 navaronbracke

But what if someone wants to enforce that curly_braces_in_control_flow_structures enforces all control flow to have braces?

I don't think that's related to this issue...

srawlins avatar Feb 20 '24 18:02 srawlins

With the change proposal above, the statement if (condition) return; would not be flagged by the lint (if it's less than the configured line length), per the rule in the style guide. However, I propose to keep the current behavior which lints on if (condition) return; regardless of the line length for the full statement.

navaronbracke avatar Feb 20 '24 21:02 navaronbracke

if (condition) return; is not flagged today though. The change proposal above would not change that.

Here's the test case.

srawlins avatar Feb 20 '24 21:02 srawlins

Ah, my mistake. Should I file a new issue for having it linted?

navaronbracke avatar Feb 20 '24 22:02 navaronbracke

Yes please :D.

Without a backing Effective Dart rule though, we are unlikely to add a style-oriented rule, or change the Effective Dart rule. You can file a request to change the style guide at https://github.com/dart-lang/site-www/issues.

srawlins avatar Feb 21 '24 01:02 srawlins

Agree. The issue here is that the expected exception seems to be implemented by checking that the ) ending the condition and the end of the statement are on the same line. It should instead check that the if and the end of the statement is on the same line, since that's what's required to check that "the entire if statement is on a single line". (I checked the code, that's precisely what happens. Fix seems easy, so I wrote that too.

lrhn avatar Feb 22 '24 15:02 lrhn

Sorry, forgot to mention I've been working on this and cleaning up the SDK. https://dart-review.googlesource.com/c/sdk/+/353140

But it will take a lot of work to land. Google internal needs a lot of cleanup.

srawlins avatar Feb 22 '24 16:02 srawlins

SGTM, ship it! (But yes, signficant clean-up will likely be needed.)

Feel free to take the extra tests I added, if you think they can be useful.

lrhn avatar Feb 23 '24 15:02 lrhn