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

[Effective Dart] Disallow single line if statements without braces #5580

Open
navaronbracke opened this issue Feb 21, 2024 · 1 comment
Open
Labels
a.effective-dart Relates to the best practices explained in Effective Dart e2-days Can complete in < 5 days of normal, not dedicated, work p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged t.diagnostics Relates to diagnostics, analysis, or linting of code

Comments

@navaronbracke
Copy link

Today, the curly_braces_in_control_flow_structures lint does not lint on the following statement

if (arg == null) return defaultValue;

Per the style guide, this is due to an exception to the rule:

There is one exception to this: an if statement with no else clause where the entire if statement and the then body all fit in one line. In that case, you may leave off the braces if you prefer.

However, in code review, I have had to enforce that all control flow statements (regardless of line length for the full statement) have braces.
Thus, in my opinion, the statement "DO use curly braces for all flow control structures." is not correct, due to this exception.

Perhaps we can either change the curly_braces_in_flow_control_structures lint (and remove the exception to the rule in Effective Dart),
or add a new lint that lints specifically for the single line ìf (condition) <body>;

Context: dart-lang/linter#4870 (comment)

@huycozy huycozy added st.triage.triage-team Triage team reviewing and categorizing the issue p2-medium Necessary but not urgent concern. Resolve when possible. e2-days Can complete in < 5 days of normal, not dedicated, work t.diagnostics Relates to diagnostics, analysis, or linting of code and removed st.triage.triage-team Triage team reviewing and categorizing the issue labels Feb 21, 2024
@munificent munificent added the a.effective-dart Relates to the best practices explained in Effective Dart label Feb 26, 2024
@atsansone atsansone added the st.triage.ltw Indicates Lead Tech Writer has triaged label Feb 26, 2024
@gnprice
Copy link

gnprice commented Sep 24, 2024

Thus, in my opinion, the statement "DO use curly braces for all flow control structures." is not correct, due to this exception.

I agree the description is a bit misleading and would be good to clarify. As is, the summary line is quite definitive but in reality there's an important exception.

I think this would do it:

DO use curly braces for all multi-line flow control structures.

If you want the other behavior, there's an existing lint for that: always_put_control_body_on_new_line. That's what the Flutter tree uses, for example. Combining that lint and this one gets you a requirement that all control-flow structures use curly braces.

In addition to clarifying what this lint does, it would probably be useful for this lint's description to mention the other one, to help out readers who want that other behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a.effective-dart Relates to the best practices explained in Effective Dart e2-days Can complete in < 5 days of normal, not dedicated, work p2-medium Necessary but not urgent concern. Resolve when possible. st.triage.ltw Indicates Lead Tech Writer has triaged t.diagnostics Relates to diagnostics, analysis, or linting of code
Projects
None yet
Development

No branches or pull requests

5 participants