Skip to content

Commit

Permalink
Flag calls to Sequenced* insertion APIs on Sorted* instances.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 683177531
  • Loading branch information
cpovirk authored and Error Prone Team committed Oct 7, 2024
1 parent a659be5 commit 150c60d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ public class DoNotCallChecker extends BugChecker
.put(
instanceMethod().onExactClass("java.sql.Time").named("toInstant"),
"sqlTime.toInstant() is not supported. Did you mean to call toLocalTime() instead?")
.put(
instanceMethod()
.onDescendantOf("java.util.SortedMap")
.namedAnyOf("putFirst", "putLast"),
"Sorted collections do not support insertions with explicit positioning")
.put(
instanceMethod()
.onDescendantOf("java.util.SortedSet")
.namedAnyOf("addFirst", "addLast"),
"Sorted collections do not support insertions with explicit positioning")
.put(
instanceMethod()
.onExactClass("java.util.concurrent.ThreadLocalRandom")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,30 @@ public void badApis() {
.doTest();
}

@Test
public void sortedCollectionSequencedCollectionMethods() {
testHelper
.addSourceLines(
"Test.java",
"""
import java.util.TreeMap;
import java.util.TreeSet;
public class Test {
public void foo(TreeMap<String, String> map, TreeSet<String> set) {
// BUG: Diagnostic contains: DoNotCall
map.putFirst("foo", "bar");
// BUG: Diagnostic contains: DoNotCall
map.putLast("foo", "bar");
// BUG: Diagnostic contains: DoNotCall
set.addFirst("foo");
// BUG: Diagnostic contains: DoNotCall
set.addLast("foo");
}
}
""")
.doTest();
}

@Test
public void readLock_newCondition() {
assertThrows(
Expand Down

0 comments on commit 150c60d

Please sign in to comment.