-
Notifications
You must be signed in to change notification settings - Fork 52
Introduce DequeRules Refaster rule collection
#1946
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb9392f
Introduce `DequeRules` Refaster rule collection
Stephan202 d4a6196
Add checkstyle patch
rickie 01c2d5f
Add metrics patch
rickie 010c750
Introduce `IsList` Matcher
rickie 2f969f5
Optimize imports
rickie 3d971ed
Tweaks
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
154 changes: 154 additions & 0 deletions
154
error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/DequeRules.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,154 @@ | ||
| package tech.picnic.errorprone.refasterrules; | ||
|
|
||
| import com.google.errorprone.refaster.Refaster; | ||
| import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
| import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
| import com.google.errorprone.refaster.annotation.NotMatches; | ||
| import java.util.Deque; | ||
| import java.util.Iterator; | ||
| import org.jspecify.annotations.Nullable; | ||
| import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; | ||
| import tech.picnic.errorprone.refaster.matchers.IsList; | ||
|
|
||
| /** Refaster rules related to expressions dealing with {@link Deque} instances. */ | ||
| // XXX: Introduce similar rules for `BlockingDeque`. | ||
| @OnlineDocumentation | ||
| final class DequeRules { | ||
| private DequeRules() {} | ||
|
|
||
| /** Prefer {@link Deque#addLast(Object)} over less clear alternatives. */ | ||
| static final class DequeAddFirst<S, T extends S> { | ||
| @BeforeTemplate | ||
| void before(Deque<S> deque, T element) { | ||
| deque.push(element); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(Deque<S> deque, T element) { | ||
| deque.addFirst(element); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Prefer {@link Deque#addLast(Object)} over less clear alternatives. | ||
| * | ||
| * <p>Note: this rule does not match instances of type {@link java.util.List} (including {@link | ||
| * java.util.LinkedList}, which also implements {@link Deque}), as {@link | ||
| * java.util.List#add(Object)} is the idiomatic method for those types. | ||
| */ | ||
| static final class DequeAddLast<S, T extends S> { | ||
| @BeforeTemplate | ||
| void before(@NotMatches(IsList.class) Deque<S> deque, T element) { | ||
| deque.add(element); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| void after(Deque<S> deque, T element) { | ||
| deque.addLast(element); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#removeFirst()} over less clear alternatives. */ | ||
| static final class DequeRemoveFirst<S, T extends S> { | ||
| @BeforeTemplate | ||
| S before(Deque<T> deque) { | ||
| return Refaster.anyOf(deque.pop(), deque.remove()); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| S after(Deque<T> deque) { | ||
| return deque.removeFirst(); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#offerLast(Object)} over less clear alternatives. */ | ||
| static final class DequeOfferLast<S, T extends S> { | ||
| @BeforeTemplate | ||
| boolean before(Deque<S> deque, T element) { | ||
| return deque.offer(element); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| boolean after(Deque<S> deque, T element) { | ||
| return deque.offerLast(element); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#pollFirst()} over less clear alternatives. */ | ||
| static final class DequePollFirst<S, T extends S> { | ||
| @BeforeTemplate | ||
| @Nullable S before(Deque<T> deque) { | ||
| return deque.poll(); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| @Nullable S after(Deque<T> deque) { | ||
| return deque.pollFirst(); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#pollFirst()} over less clear alternatives. */ | ||
| static final class DequeGetFirst<S, T extends S> { | ||
| @BeforeTemplate | ||
| S before(Deque<T> deque) { | ||
| return deque.element(); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| S after(Deque<T> deque) { | ||
| return deque.getFirst(); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#peekFirst()} over less clear alternatives. */ | ||
| static final class DequePeekFirst<S, T extends S> { | ||
| @BeforeTemplate | ||
| @Nullable S before(Deque<T> deque) { | ||
| return deque.peek(); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| @Nullable S after(Deque<T> deque) { | ||
| return deque.peekFirst(); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#removeFirstOccurrence(Object)} over less clear alternatives. */ | ||
| static final class DequeRemoveFirstOccurrence<S, T extends S> { | ||
| @BeforeTemplate | ||
| boolean before(Deque<S> deque, T element) { | ||
| return deque.remove(element); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| boolean after(Deque<S> deque, T element) { | ||
| return deque.removeFirstOccurrence(element); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#iterator()} over more contrived alternatives. */ | ||
| static final class DequeIterator<T> { | ||
| @BeforeTemplate | ||
| Iterator<T> before(Deque<T> deque) { | ||
| return deque.reversed().descendingIterator(); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| Iterator<T> after(Deque<T> deque) { | ||
| return deque.iterator(); | ||
| } | ||
| } | ||
|
|
||
| /** Prefer {@link Deque#descendingIterator()} over more contrived alternatives. */ | ||
| static final class DequeDescendingIterator<T> { | ||
| @BeforeTemplate | ||
| Iterator<T> before(Deque<T> deque) { | ||
| return deque.reversed().iterator(); | ||
| } | ||
|
|
||
| @AfterTemplate | ||
| Iterator<T> after(Deque<T> deque) { | ||
| return deque.descendingIterator(); | ||
| } | ||
| } | ||
| } | ||
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
...-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/DequeRulesTestInput.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.refasterrules; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedList; | ||
| import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
|
||
| final class DequeRulesTest implements RefasterRuleCollectionTestCase { | ||
| void testDequeAddFirst() { | ||
| new ArrayDeque<String>().push("foo"); | ||
| } | ||
|
|
||
| void testDequeAddLast() { | ||
| new ArrayDeque<String>().add("foo"); | ||
| new LinkedList<String>().add("bar"); | ||
| } | ||
|
|
||
| ImmutableSet<String> testDequeRemoveFirst() { | ||
| return ImmutableSet.of(new ArrayDeque<String>(0).pop(), new ArrayDeque<String>(1).remove()); | ||
| } | ||
|
|
||
| boolean testDequeOfferLast() { | ||
| return new ArrayDeque<String>().offer("foo"); | ||
| } | ||
|
|
||
| String testDequePollFirst() { | ||
| return new ArrayDeque<String>().poll(); | ||
| } | ||
|
|
||
| String testDequeGetFirst() { | ||
| return new ArrayDeque<String>().element(); | ||
| } | ||
|
|
||
| String testDequePeekFirst() { | ||
| return new ArrayDeque<String>().peek(); | ||
| } | ||
|
|
||
| boolean testDequeRemoveFirstOccurrence() { | ||
| return new ArrayDeque<>().remove("foo"); | ||
| } | ||
|
|
||
| Iterator<String> testDequeIterator() { | ||
| return new ArrayDeque<String>().reversed().descendingIterator(); | ||
| } | ||
|
|
||
| Iterator<String> testDequeDescendingIterator() { | ||
| return new ArrayDeque<String>().reversed().iterator(); | ||
| } | ||
| } |
51 changes: 51 additions & 0 deletions
51
...contrib/src/test/resources/tech/picnic/errorprone/refasterrules/DequeRulesTestOutput.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,51 @@ | ||
| package tech.picnic.errorprone.refasterrules; | ||
|
|
||
| import com.google.common.collect.ImmutableSet; | ||
| import java.util.ArrayDeque; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedList; | ||
| import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; | ||
|
|
||
| final class DequeRulesTest implements RefasterRuleCollectionTestCase { | ||
| void testDequeAddFirst() { | ||
| new ArrayDeque<String>().addFirst("foo"); | ||
| } | ||
|
|
||
| void testDequeAddLast() { | ||
| new ArrayDeque<String>().addLast("foo"); | ||
| new LinkedList<String>().add("bar"); | ||
| } | ||
|
|
||
| ImmutableSet<String> testDequeRemoveFirst() { | ||
| return ImmutableSet.of( | ||
| new ArrayDeque<String>(0).removeFirst(), new ArrayDeque<String>(1).removeFirst()); | ||
| } | ||
|
|
||
| boolean testDequeOfferLast() { | ||
| return new ArrayDeque<String>().offerLast("foo"); | ||
| } | ||
|
|
||
| String testDequePollFirst() { | ||
| return new ArrayDeque<String>().pollFirst(); | ||
| } | ||
|
|
||
| String testDequeGetFirst() { | ||
| return new ArrayDeque<String>().getFirst(); | ||
| } | ||
|
|
||
| String testDequePeekFirst() { | ||
| return new ArrayDeque<String>().peekFirst(); | ||
| } | ||
|
|
||
| boolean testDequeRemoveFirstOccurrence() { | ||
| return new ArrayDeque<>().removeFirstOccurrence("foo"); | ||
| } | ||
|
|
||
| Iterator<String> testDequeIterator() { | ||
| return new ArrayDeque<String>().iterator(); | ||
| } | ||
|
|
||
| Iterator<String> testDequeDescendingIterator() { | ||
| return new ArrayDeque<String>().descendingIterator(); | ||
| } | ||
| } |
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.
Some people think of
Dequeas a replacement forStackinterface; wouldn'tpush(andpop) keywords be more relevant here? 🤔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.
Yes, that's a fair concern. My thinking is that the suggestions here will make the intent of the code clear in all cases, independently from (a) the specific combination of operations applied in a given context and (b) whether the user is familiar with stack operations.
It wouldn't surprise me if this change receives some pushback... let's see. 🤞