-
Notifications
You must be signed in to change notification settings - Fork 135
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
Implement BracesRequired error-prone check with suggested fixes #1130
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/BracesRequired.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.matchers.Description; | ||
import com.sun.source.tree.DoWhileLoopTree; | ||
import com.sun.source.tree.EnhancedForLoopTree; | ||
import com.sun.source.tree.ForLoopTree; | ||
import com.sun.source.tree.IfTree; | ||
import com.sun.source.tree.StatementTree; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.WhileLoopTree; | ||
import javax.annotation.Nullable; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "BracesRequired", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = BugPattern.LinkType.CUSTOM, | ||
providesFix = BugPattern.ProvidesFix.REQUIRES_HUMAN_ATTENTION, | ||
severity = BugPattern.SeverityLevel.WARNING, | ||
summary = "Braces are required for readability") | ||
public final class BracesRequired extends BugChecker implements | ||
BugChecker.DoWhileLoopTreeMatcher, | ||
BugChecker.ForLoopTreeMatcher, | ||
BugChecker.EnhancedForLoopTreeMatcher, | ||
BugChecker.IfTreeMatcher, | ||
BugChecker.WhileLoopTreeMatcher { | ||
|
||
@Override | ||
public Description matchIf(IfTree tree, VisitorState state) { | ||
check(tree.getThenStatement(), state); | ||
check(tree.getElseStatement(), state); | ||
return Description.NO_MATCH; | ||
} | ||
|
||
@Override | ||
public Description matchWhileLoop(WhileLoopTree tree, VisitorState state) { | ||
check(tree.getStatement(), state); | ||
return Description.NO_MATCH; | ||
} | ||
|
||
@Override | ||
public Description matchDoWhileLoop(DoWhileLoopTree tree, VisitorState state) { | ||
check(tree.getStatement(), state); | ||
return Description.NO_MATCH; | ||
} | ||
|
||
@Override | ||
public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState state) { | ||
check(tree.getStatement(), state); | ||
return Description.NO_MATCH; | ||
} | ||
|
||
@Override | ||
public Description matchForLoop(ForLoopTree tree, VisitorState state) { | ||
check(tree.getStatement(), state); | ||
return Description.NO_MATCH; | ||
} | ||
|
||
private void check(@Nullable StatementTree tree, VisitorState state) { | ||
if (tree != null && tree.getKind() == Tree.Kind.EXPRESSION_STATEMENT) { | ||
state.reportMatch(buildDescription(tree) | ||
.addFix(SuggestedFix.replace(tree, "{" + state.getSourceForNode(tree) + "}")) | ||
.build()); | ||
} | ||
} | ||
} |
200 changes: 200 additions & 0 deletions
200
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/BracesRequiredTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.errorprone; | ||
|
||
import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class BracesRequiredTest { | ||
|
||
@Test | ||
void testFix_if_then() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param) System.out.println();", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param) {", | ||
" System.out.println();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_if_else() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param) {", | ||
" System.out.println(\"if\");", | ||
" } else", | ||
" System.out.println(\"else\");", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param) {", | ||
" System.out.println(\"if\");", | ||
" } else {", | ||
" System.out.println(\"else\");", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_if_both() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param)", | ||
" System.out.println(\"if\");", | ||
" else", | ||
" System.out.println(\"else\");", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param) {", | ||
" System.out.println(\"if\");", | ||
" } else {", | ||
carterkozak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
" System.out.println(\"else\");", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_if_emptyThen() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param); else", | ||
" System.out.println(\"else\");", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" if (param); else {", | ||
" System.out.println(\"else\");", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_while() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" while (param) System.out.println();", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" while (param) {", | ||
" System.out.println();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_doWhile() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" do System.out.println(); while (param);", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" do {", | ||
" System.out.println();", | ||
" } while (param);", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_for() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" for (int i = 0; i < 5; i++) System.out.println();", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"class Test {", | ||
" void f(boolean param) {", | ||
" for (int i = 0; i < 5; i++) {", | ||
" System.out.println();", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
@Test | ||
void testFix_enhancedFor() { | ||
fix() | ||
.addInputLines("Test.java", | ||
"import java.util.List;", | ||
"class Test {", | ||
" void f(List<String> list) {", | ||
" for (String item : list) System.out.println(item);", | ||
" }", | ||
"}") | ||
.addOutputLines("Test.java", | ||
"import java.util.List;", | ||
"class Test {", | ||
" void f(List<String> list) {", | ||
" for (String item : list) {", | ||
" System.out.println(item);", | ||
" }", | ||
" }", | ||
"}") | ||
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); | ||
} | ||
|
||
private RefactoringValidator fix() { | ||
return RefactoringValidator.of(new BracesRequired(), getClass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
type: improvement | ||
improvement: | ||
description: |- | ||
Implement BracesRequired error-prone check with suggested fixes | ||
```diff | ||
- if (condition) statement; | ||
+ if (condition) { | ||
+ statement; | ||
+ } | ||
``` | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1130 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems a little odd to always return NO_MATCH and rely on a side effect of check to actually do the fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it's a bit odd. It allows us to keep the code cleaner and reusable without creating potentially unnecessary SuggestedFix.Builder objects for every conditional and loop. Several of the upstream checks are structured similarly.