-
Notifications
You must be signed in to change notification settings - Fork 134
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
Ban the var keyword via error-prone VarUsage
#1788
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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
68 changes: 68 additions & 0 deletions
68
baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/VarUsage.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,68 @@ | ||
/* | ||
* (c) Copyright 2020 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.BugPattern.LinkType; | ||
import com.google.errorprone.BugPattern.SeverityLevel; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.fixes.SuggestedFix; | ||
import com.google.errorprone.fixes.SuggestedFixes; | ||
import com.google.errorprone.matchers.Description; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.google.errorprone.util.ErrorProneToken; | ||
import com.sun.source.tree.Tree; | ||
import com.sun.source.tree.VariableTree; | ||
import com.sun.tools.javac.parser.Tokens.TokenKind; | ||
|
||
@AutoService(BugChecker.class) | ||
@BugPattern( | ||
name = "VarUsage", | ||
link = "https://github.com/palantir/gradle-baseline#baseline-error-prone-checks", | ||
linkType = LinkType.CUSTOM, | ||
severity = SeverityLevel.ERROR, | ||
summary = "The `var` keyword results in illegible code in most cases and should not be used.") | ||
public final class VarUsage extends BugChecker implements BugChecker.VariableTreeMatcher { | ||
|
||
@Override | ||
public Description matchVariable(VariableTree tree, VisitorState state) { | ||
Tree typeTree = tree.getType(); | ||
// The AST doesn't differentiate between 'var' and a concrete type, so we check the source | ||
// prior to tokenizing the variable tree. | ||
String sourceType = state.getSourceForNode(typeTree); | ||
if (sourceType != null) { | ||
return Description.NO_MATCH; | ||
} | ||
for (ErrorProneToken token : state.getOffsetTokensForNode(tree)) { | ||
if (token.kind() == TokenKind.IDENTIFIER | ||
&& token.hasName() | ||
&& token.name().contentEquals("var")) { | ||
SuggestedFix.Builder fix = SuggestedFix.builder(); | ||
return buildDescription(typeTree) | ||
.addFix(fix.replace( | ||
token.pos(), | ||
token.endPos(), | ||
SuggestedFixes.prettyType(state, fix, ASTHelpers.getType(typeTree))) | ||
.build()) | ||
.build(); | ||
} | ||
} | ||
return Description.NO_MATCH; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/VarUsageTest.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,72 @@ | ||
/* | ||
* (c) Copyright 2021 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 org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledForJreRange; | ||
import org.junit.jupiter.api.condition.JRE; | ||
|
||
class VarUsageTest { | ||
|
||
@Test | ||
@EnabledForJreRange(min = JRE.JAVA_10) | ||
void testSimple() { | ||
fix().addInputLines( | ||
"Test.java", | ||
// format | ||
"class Test {", | ||
" void function() {", | ||
" var x = 3;", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
// format | ||
"class Test {", | ||
" void function() {", | ||
" int x = 3;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
@EnabledForJreRange(min = JRE.JAVA_10) | ||
void testWithFinalModifier() { | ||
fix().addInputLines( | ||
"Test.java", | ||
// format | ||
"class Test {", | ||
" void function() {", | ||
" final var x = 3;", | ||
" }", | ||
"}") | ||
.addOutputLines( | ||
"Test.java", | ||
// format | ||
"class Test {", | ||
" void function() {", | ||
" final int x = 3;", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
private RefactoringValidator fix() { | ||
return RefactoringValidator.of(VarUsage.class, 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,5 @@ | ||
type: improvement | ||
improvement: | ||
description: Ban the var keyword via error-prone `VarUsage` | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1788 |
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.
this will suggest using the concrete type instead of the interface which is typically prefered. It would be a ton of work to find the minimum type required of the variable though so its probably fine
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.
Maybe we just force the human to input the right type instead of a 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.
I'd prefer to give them a starting point, the reviewer is responsible for ensuring the quality of the replacement. If the replacement quality is too low we can remove the suggestion.
If we don't automate any of it, they're more likely to turn off the check instead of fixing it themselves.