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 3ac00ec
Show file tree
Hide file tree
Showing 2 changed files with 36 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 @@ -16,6 +16,7 @@

package com.google.errorprone.bugpatterns;

import static com.google.common.truth.TruthJUnit.assume;
import static org.junit.Assert.assertThrows;

import com.google.errorprone.CompilationTestHelper;
Expand Down Expand Up @@ -516,6 +517,31 @@ public void badApis() {
.doTest();
}

@Test
public void sortedCollectionSequencedCollectionMethods() {
assume().that(Runtime.version().feature()).isAtLeast(21);
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 3ac00ec

Please sign in to comment.