-
Notifications
You must be signed in to change notification settings - Fork 51
Introduce LexicographicalPermitsListing check
#2017
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
Open
mohamedsamehsalah
wants to merge
11
commits into
master
Choose a base branch
from
mohamedsamehsalah/lexico-sealed-permits-listing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
74c50b8
Introduce `LexicographicalSealedInterfacePermitsListing` check
mohamedsamehsalah d05a3a0
Improve summary
mohamedsamehsalah b7e6d39
Oopsie
mohamedsamehsalah ee2f5a2
Drop redundant checks to kill mutations
mohamedsamehsalah 49aedb2
Improve tests
mohamedsamehsalah 7f1a539
Kill one more mutation
mohamedsamehsalah 9fd0303
Kill some mutants?
rickie f1d9dc1
Suggestions
rickie 506d42a
Format
rickie e846f4e
Last fixes
rickie d2d621d
Suggestions
Stephan202 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 hidden or 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
50 changes: 50 additions & 0 deletions
50
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/LexicographicalPermitsListing.java
This file contains hidden or 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,50 @@ | ||
| package tech.picnic.errorprone.bugpatterns; | ||
|
|
||
| import static com.google.errorprone.BugPattern.LinkType.CUSTOM; | ||
| import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; | ||
| import static com.google.errorprone.BugPattern.StandardTags.STYLE; | ||
| import static java.util.Comparator.comparing; | ||
| import static tech.picnic.errorprone.utils.Documentation.BUG_PATTERNS_BASE_URL; | ||
|
|
||
| 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.bugpatterns.BugChecker.ClassTreeMatcher; | ||
| import com.google.errorprone.fixes.SuggestedFix; | ||
| import com.google.errorprone.matchers.Description; | ||
| import com.sun.source.tree.ClassTree; | ||
| import tech.picnic.errorprone.utils.SourceCode; | ||
|
|
||
| /** | ||
| * A {@link BugChecker} that flags enumerations of permitted subtypes that are not lexicographically | ||
| * sorted. | ||
| * | ||
| * <p>The idea behind this checker is that maintaining a sorted sequence simplifies conflict | ||
| * resolution. | ||
| */ | ||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| summary = "Sort permitted subtypes lexicographically where possible", | ||
| link = BUG_PATTERNS_BASE_URL + "LexicographicalPermitsListing", | ||
| linkType = CUSTOM, | ||
| severity = SUGGESTION, | ||
| tags = STYLE) | ||
| public final class LexicographicalPermitsListing extends BugChecker implements ClassTreeMatcher { | ||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| /** Instantiates a new {@link LexicographicalPermitsListing} instance. */ | ||
| public LexicographicalPermitsListing() {} | ||
|
|
||
| @Override | ||
| public Description matchClass(ClassTree tree, VisitorState state) { | ||
| SuggestedFix fix = | ||
| SourceCode.sortTrees( | ||
| tree.getPermitsClause(), | ||
| comparing(annotation -> SourceCode.treeToString(annotation, state)), | ||
| state); | ||
| return fix.isEmpty() | ||
| ? Description.NO_MATCH | ||
| : describeMatch(tree.getPermitsClause().getFirst(), fix); | ||
| } | ||
| } | ||
70 changes: 70 additions & 0 deletions
70
...b/src/test/java/tech/picnic/errorprone/bugpatterns/LexicographicalPermitsListingTest.java
This file contains hidden or 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,70 @@ | ||
| package tech.picnic.errorprone.bugpatterns; | ||
|
|
||
| import com.google.errorprone.BugCheckerRefactoringTestHelper; | ||
| import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode; | ||
| import com.google.errorprone.CompilationTestHelper; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| final class LexicographicalPermitsListingTest { | ||
| @Test | ||
| void identification() { | ||
| CompilationTestHelper.newInstance(LexicographicalPermitsListing.class, getClass()) | ||
| .addSourceLines( | ||
| "A.java", | ||
| "interface A {", | ||
| " non-sealed class X extends UnsortedPermitsClass", | ||
| " implements SinglePermits, SortedPermits, UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Y extends UnsortedPermitsClass implements SortedPermits, UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Z extends UnsortedPermitsClass {}", | ||
| "", | ||
| " sealed interface NoPermits {", | ||
| " record R() implements NoPermits {}", | ||
| " }", | ||
| "", | ||
| " sealed interface SinglePermits permits X {}", | ||
| "", | ||
| " sealed interface SortedPermits permits X, Y {}", | ||
| "", | ||
| " // BUG: Diagnostic contains:", | ||
| " sealed interface UnsortedPermits permits Y, X {}", | ||
| "", | ||
| " // BUG: Diagnostic contains:", | ||
| " sealed class UnsortedPermitsClass permits Z, X, Y {}", | ||
| "}") | ||
| .doTest(); | ||
| } | ||
|
|
||
| @Test | ||
| void replacement() { | ||
| BugCheckerRefactoringTestHelper.newInstance(LexicographicalPermitsListing.class, getClass()) | ||
| .addInputLines( | ||
| "A.java", | ||
| "interface A {", | ||
| " non-sealed class X extends UnsortedPermitsClass implements UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Y extends UnsortedPermitsClass implements UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Z extends UnsortedPermitsClass {}", | ||
| "", | ||
| " sealed interface UnsortedPermits permits Y, X {}", | ||
| "", | ||
| " sealed class UnsortedPermitsClass permits Z, X, Y {}", | ||
| "}") | ||
| .addOutputLines( | ||
| "A.java", | ||
| "interface A {", | ||
| " non-sealed class X extends UnsortedPermitsClass implements UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Y extends UnsortedPermitsClass implements UnsortedPermits {}", | ||
| "", | ||
| " non-sealed class Z extends UnsortedPermitsClass {}", | ||
| "", | ||
| " sealed interface UnsortedPermits permits X, Y {}", | ||
| "", | ||
| " sealed class UnsortedPermitsClass permits X, Y, Z {}", | ||
| "}") | ||
| .doTest(TestMode.TEXT_MATCH); | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
ClassTrees are way less common thanModifierTrees, so I omitted theif (tree.getPermitsClause().size() < 2)case here.