You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Add curly braces" does not appear as a Quick-fix for always_put_control_body_on_new_line #3981
I suspect this is because "Add curly braces" seen in the menu here is an Assist (and not a Fix) - eg. it'll show up even without the lint warning. The other menus you're opening are filtered to only show Fixes.
I think it should be possible to wire it up as a fix for that lint that would solve this.
changed the title [-]Quick fix doesn't appear in all cases[/-][+]"Add curly braces" does not appear as a Quick-fix for always_put_control_body_on_new_line[/+]on May 23, 2022
Re-reading this lint, I'm now wondering whether the "Add curly braces" fix is the obvious fix here. The lint isn't about using curly braces, but rather about having the code on a new line.
If you're using the formatter, then these things are quite tied together, but if you're not using the formatter you might expect a fix for "move the body to a new line" rather than adding curly braces.
There is another lint named curly_braces_in_flow_control_structures that I believe does what you want - it generates a similar warning but it does provide "add curly braces" as a fix.
Does it make more sense for you to enable curly_braces_in_flow_control_structures is curly braces is what you really want?
(cc @bwilkerson for his thoughts on whether "Use curly braces" is a reasonable fix also for "always_put_control_body_on_new_line")
@bwilkerson is there a way to mark "Add curly braces" as a fix for always_put_control_body_on_new_linewithout it applying as an automatic fix for dart fix (and related commands)?
I think it would be reasonable to show in the fix list in VS Code (it does already show in the "Code Actions" list from being an assist, but not in places where the list is filtered to just fixes), however I'm less sure it makes sense to apply automatically for this lint because it doesn't exactly match the reported problem and there is a more specific lint for enforcing braces (curly_braces_in_flow_control_structures).
Personally, I think wanting to have control bodies on a new line without braces is a reasonable choice. Outside of Dart, it's actually what I'd do.. I use braces in Dart only because the formatter doesn't support spanning two lines without braces.
is there a way to mark "Add curly braces" as a fix for always_put_control_body_on_new_linewithout it applying as an automatic fix for dart fix (and related commands)?
Yes. Every fix knows whether it can be applied in bulk (dart fix) and whether it can be applied to a whole file (presumably the "related commands"). It can be coded to return false for that particular diagnostic.
With dart-lang/sdk@20b7de7, the "Add Curly Braces" fix will now show up as a fix for this lint:
It won't be applied by dart fix (because I don't think it's clearly the correct fix for this lint, which is about newlines and not braces).
Although, while testing this I realised that curly_braces_in_flow_control_structures doesn't cover things on a single line, so if your code is:
if (true) print('');
Then enabling both lints still won't get dart fix to do what you want. I think the solution to that is probably a new fix for always_put_control_body_on_new_line that inserts a newline, then the example above could be dart fixable, without conflating the lints (eg. not assuming people with always_put_control_body_on_new_line want braces, or people with curly_braces_in_flow_control_structures want everything split across lines).
There's already an issue covering this at dart-lang/sdk#47956, although there's a slight complication because of conflicts with the formatter.
Activity
DanTup commentedon May 23, 2022
I suspect this is because "Add curly braces" seen in the menu here is an Assist (and not a Fix) - eg. it'll show up even without the lint warning. The other menus you're opening are filtered to only show Fixes.
I think it should be possible to wire it up as a fix for that lint that would solve this.
[-]Quick fix doesn't appear in all cases[/-][+]"Add curly braces" does not appear as a Quick-fix for always_put_control_body_on_new_line[/+]DanTup commentedon May 24, 2022
Re-reading this lint, I'm now wondering whether the "Add curly braces" fix is the obvious fix here. The lint isn't about using curly braces, but rather about having the code on a new line.
If you're using the formatter, then these things are quite tied together, but if you're not using the formatter you might expect a fix for "move the body to a new line" rather than adding curly braces.
There is another lint named
curly_braces_in_flow_control_structuresthat I believe does what you want - it generates a similar warning but it does provide "add curly braces" as a fix.Does it make more sense for you to enable
curly_braces_in_flow_control_structuresis curly braces is what you really want?(cc @bwilkerson for his thoughts on whether "Use curly braces" is a reasonable fix also for "always_put_control_body_on_new_line")
guidezpl commentedon May 24, 2022
Ah thanks, using
curly_braces_in_flow_control_structuresworks.IMO I still think the issue is valid, and both options should be shown as fixes in all places.
DanTup commentedon May 26, 2022
@bwilkerson is there a way to mark "Add curly braces" as a fix for
always_put_control_body_on_new_linewithout it applying as an automatic fix fordart fix(and related commands)?I think it would be reasonable to show in the fix list in VS Code (it does already show in the "Code Actions" list from being an assist, but not in places where the list is filtered to just fixes), however I'm less sure it makes sense to apply automatically for this lint because it doesn't exactly match the reported problem and there is a more specific lint for enforcing braces (
curly_braces_in_flow_control_structures).Personally, I think wanting to have control bodies on a new line without braces is a reasonable choice. Outside of Dart, it's actually what I'd do.. I use braces in Dart only because the formatter doesn't support spanning two lines without braces.
bwilkerson commentedon May 26, 2022
Yes. Every fix knows whether it can be applied in bulk (
dart fix) and whether it can be applied to a whole file (presumably the "related commands"). It can be coded to return false for that particular diagnostic.[analysis_server] Provide UseCurlyBraces as a fix for always_put_cont…
DanTup commentedon May 30, 2022
With dart-lang/sdk@20b7de7, the "Add Curly Braces" fix will now show up as a fix for this lint:
It won't be applied by
dart fix(because I don't think it's clearly the correct fix for this lint, which is about newlines and not braces).Although, while testing this I realised that
curly_braces_in_flow_control_structuresdoesn't cover things on a single line, so if your code is:Then enabling both lints still won't get
dart fixto do what you want. I think the solution to that is probably a new fix foralways_put_control_body_on_new_linethat inserts a newline, then the example above could bedart fixable, without conflating the lints (eg. not assuming people withalways_put_control_body_on_new_linewant braces, or people withcurly_braces_in_flow_control_structureswant everything split across lines).There's already an issue covering this at dart-lang/sdk#47956, although there's a slight complication because of conflicts with the formatter.