-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
New Check: Unnecessary Block Braces #14758
Comments
With <module name="NeedBraces">
<property name="tokens" value="LAMBDA"/>
<property name="allowSingleLineStatement" value="false"/>
</module> in the config file. One gets
Exactly, the opposite of what this feature request proposes ;-) |
I am ok with UnnecessaryBlockBraces @nrmancuso , @rnveach , please review. |
Name doesn't match the violations.
I am understanding this. Will this violate switches too? Examples doesn't make this clear.
Will this violate only single lines or single statements? What if we have a single statement that spans multiple lines? This will conflict with NeedBraces . |
yes, it is left over from initial proposal.
@pjljvandelaar , please share examples to make it very clear what is a target or lets remove for now from design, to not confuse on scope.
this check will be in clear conflict with NeedBraces, use need to decide what to choose as preferable. |
This doesn't have to be in conflict if we give it the appropriate properties. https://checkstyle.org/checks/blocks/needbraces.html#NeedBraces
If this is the message going with, then this check needs to be renamed to something like |
When comparing
We see that What is preferred, the top or bottom line, is up to the user of checkstyle. |
can you suggest some properties ? can we add them later on? as we see Check in in action on real code and we get real feedback from users.
I thought on this and think we can use general name if we find some other cases where block braces are not required.
But I am not strong on this, and if more people vote on more specific Check, I am ok with it. More specific Checks has more ways to be customized to do what users wants |
Indeed, having warnings for
helps to improve readability. |
ok, so in this case we will need property target to let users decide what to validate and what to skip. @rnveach , please review |
This is still not clear.
There are no examples or concrete explanation. |
@pjljvandelaar, please update issue description with more examples and configs to make it clear for all how it should work and in what cases it will raise violations. |
First configuration:
Remove unnecessary block braces to minimize the code everywhere. Second configuration
Improve readability by removing block braces when possible. Third configuration
Remove unnecessary block braces to minimize the code only for LAMBDA. |
@romani I hope my previous comment answers your question! |
Please update your comment above to have java code snippets under each config example, to show how config will highlight code with violations. If there will be violation, please put a comment on same line. |
I could only find the following documentation:
https://checkstyle.org/checks/coding/unnecessaryparentheses.html
https://checkstyle.sourceforge.io/checks/blocks/needbraces.html
I have downloaded the latest cli from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words
Running
java -jar .\checkstyle-10.xx.X-all.jar -c .\config.xml .\Example.java
With the following config file with non existing Check:
and the following java file (that correctly builds with
javac .\Example.java
)Describe the solution you'd like
I would like to get warnings on line 7
Consumer<Integer> printFiveWrong = x -> { System.out.println(x+5); };
and line 10
Function<Integer, Integer> addFiveWrong = x -> { return x+5; };
Something like
Indeed lambda function consisting out of a single statement / expression don't need block braces.
Note that typically block braces are called either "(curly) braces" or "curly brackets".
An usage outside of lambda, only if-, while-, for-, and switch-statements with a single statement in a branch come to mind.
Checkstyle already checks their presence (instead of absence): https://checkstyle.sourceforge.io/checks/blocks/needbraces.html
The text was updated successfully, but these errors were encountered: