Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False negative for curly_braces_in_control_flow_structures #4870

Closed
lrhn opened this issue Feb 7, 2024 · 11 comments · Fixed by zulip/zulip-flutter#961
Closed

False negative for curly_braces_in_control_flow_structures #4870

lrhn opened this issue Feb 7, 2024 · 11 comments · Fixed by zulip/zulip-flutter#961
Assignees
Labels
false-negative P3 A lower priority bug or feature request set-core Affects a rule in the core Dart rule set set-recommended Affects a rule in the recommended Dart rule set type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@lrhn
Copy link
Member

lrhn commented Feb 7, 2024

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 lrhn added set-recommended Affects a rule in the recommended Dart rule set false-negative labels Feb 7, 2024
@github-actions github-actions bot added the set-core Affects a rule in the core Dart rule set label Feb 7, 2024
@bwilkerson bwilkerson added the P3 A lower priority bug or feature request label Feb 9, 2024
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Feb 16, 2024
Work towards dart-lang/linter#4870

Change-Id: I0e52227496d3ea26e498501c59e3b2af0e9c67fa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352982
Auto-Submit: Samuel Rawlins <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
@navaronbracke
Copy link

navaronbracke commented Feb 20, 2024

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?

@srawlins
Copy link
Member

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...

@navaronbracke
Copy link

navaronbracke commented Feb 20, 2024

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.

@srawlins
Copy link
Member

srawlins commented Feb 20, 2024

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

Here's the test case.

@navaronbracke
Copy link

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

@srawlins
Copy link
Member

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.

@lrhn
Copy link
Member Author

lrhn commented Feb 22, 2024

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.

@srawlins srawlins self-assigned this Feb 22, 2024
@srawlins
Copy link
Member

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.

@lrhn
Copy link
Member Author

lrhn commented Feb 23, 2024

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.

@srawlins srawlins added the type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) label Apr 3, 2024
copybara-service bot pushed a commit to dart-lang/sdk that referenced this issue Sep 24, 2024
…same line as if-keyword

Fixes dart-lang/linter#4870

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try
Change-Id: Ibd8969afe35f719a020e5aa37efdc1792addac7e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353140
Reviewed-by: Stephen Adams <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
srawlins added a commit to srawlins/zulip-flutter that referenced this issue Sep 24, 2024
To fix dart-lang/linter#4870, the analyzer is now reporting if-statement bodies without curly braces when the if-statement condition spans multiple lines.

This change prepares zulip-flutter for this lint rule change.
gnprice pushed a commit to srawlins/zulip-flutter that referenced this issue Sep 24, 2024
To fix dart-lang/linter#4870, the analyzer
is now reporting if-statement bodies without curly braces when the
if-statement condition spans multiple lines.

This change prepares zulip-flutter for this lint rule change.
FMorschel pushed a commit to FMorschel/sdk that referenced this issue Sep 25, 2024
…same line as if-keyword

Fixes dart-lang/linter#4870

Cq-Include-Trybots: luci.dart.try:flutter-analyze-try,analyzer-win-release-try
Change-Id: Ibd8969afe35f719a020e5aa37efdc1792addac7e
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/353140
Reviewed-by: Stephen Adams <[email protected]>
Reviewed-by: Phil Quitslund <[email protected]>
Reviewed-by: Johnni Winther <[email protected]>
Commit-Queue: Samuel Rawlins <[email protected]>
@srawlins
Copy link
Member

Fixed by FMorschel/sdk@2c6dec6

@FMorschel
Copy link
Contributor

Just to be clear, I've nothing to do with the fix. I'm not sure why, but some of the time when I rebase my fork on GH it keeps warning issues about changes from other people but it is simply because the link is commented on the commit description. Sorry for the confusion!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-negative P3 A lower priority bug or feature request set-core Affects a rule in the core Dart rule set set-recommended Affects a rule in the recommended Dart rule set type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants