From 0a2f025b5882df6138945e6d9ae429475bf6bc38 Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 13 Sep 2023 06:39:59 -0700 Subject: [PATCH] Don't look through primitive casts; they actually do change the underlying type! PiperOrigin-RevId: 565034385 --- .../TruthIncompatibleType.java | 2 +- .../TruthIncompatibleTypeTest.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java index e1cc34d90fc..060d1448b48 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java @@ -503,7 +503,7 @@ protected Tree defaultAction(Tree node, Void unused) { @Override public Tree visitTypeCast(TypeCastTree node, Void unused) { - return node.getExpression().accept(this, null); + return getType(node).isPrimitive() ? node : node.getExpression().accept(this, null); } @Override diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleTypeTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleTypeTest.java index 85122d1bf45..e558e88bd01 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleTypeTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleTypeTest.java @@ -571,6 +571,20 @@ public void comparingElementsUsingRawCollection_noFinding() { .doTest(); } + @Test + public void casts() { + compilationHelper + .addSourceLines( + "Test.java", + "import static com.google.common.truth.Truth.assertThat;", + "public class Test {", + " public void f(int a, long b, long c) {", + " assertThat((long) a).isAnyOf(b, c);", + " }", + "}") + .doTest(); + } + @Test public void subjectExhaustiveness( @TestParameter(valuesProvider = SubjectMethods.class) Method method) {