Skip to content

Commit

Permalink
WrongOneOf: don't assume the expression in a label is an identifier.
Browse files Browse the repository at this point in the history
It's no longer required to be (!).

PiperOrigin-RevId: 696457278
  • Loading branch information
graememorgan authored and Error Prone Team committed Nov 14, 2024
1 parent 2124ebf commit 72cf2f1
Show file tree
Hide file tree
Showing 149 changed files with 814 additions and 409 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.errorprone.predicates.TypePredicates.isDescendantOf;
import static com.google.errorprone.util.ASTHelpers.enumValues;
import static com.google.errorprone.util.ASTHelpers.getReceiver;
import static com.google.errorprone.util.ASTHelpers.getSymbol;
import static com.google.errorprone.util.ASTHelpers.getType;
import static com.google.errorprone.util.ASTHelpers.stripParentheses;
import static com.google.errorprone.util.Reachability.canCompleteNormally;
Expand All @@ -38,7 +39,6 @@
import com.google.errorprone.predicates.TypePredicate;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.StatementTree;
Expand Down Expand Up @@ -97,8 +97,7 @@ private void processSwitch(
if (caseTree.getExpression() == null) {
break;
}
allowableGetters.add(
getter(((IdentifierTree) caseTree.getExpression()).getName().toString()));
allowableGetters.add(getter(getSymbol(caseTree.getExpression()).getSimpleName().toString()));

scanForInvalidGetters(getters, allowableGetters, caseTree, constantReceiver, state);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ public void bothMethodCalls() {
System.out.println("arrays are not equal!");
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -162,7 +163,8 @@ public void objectArray() {
System.out.println("arrays are not equal!");
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -230,7 +232,8 @@ public void secondArray() {
System.out.println("Objects are not equal!");
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -282,7 +285,8 @@ public void secondArray() {
System.out.println("arrays are not equal!");
}
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public void varargsHashCodeOnMoreThanOneArg() {
// Arrays.deepHashCode(multidimensionalStringArray))
hashCode = Objects.hashCode(obj1, obj2, multidimensionalStringArray);
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -147,7 +148,8 @@ public void varagsHashCodeOnObjectOrStringArray() {
hashCode = Objects.hashCode(objArray);
hashCode = Objects.hashCode((Object[]) stringArray);
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -193,7 +195,8 @@ public void varagsHashCodeOnObjectOrStringArray() {
hashCode = Objects.hash(objArray);
hashCode = Objects.hash((Object[]) stringArray);
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public void arrayOfArrays() {
// BUG: Diagnostic contains: Arrays.deepToString(a)
System.out.println(a);
}
}""")
}\
""")
.doTest();
}

Expand All @@ -129,7 +130,8 @@ public void objectEquals() {
System.out.println("string is not empty!");
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -305,7 +307,8 @@ public void stringVariableAddsArrayAndAssigns() {
// BUG: Diagnostic contains: += Arrays.toString(a)
b += a;
}
}""")
}\
""")
.doTest();
}

Expand All @@ -332,7 +335,8 @@ public void concatenateCompoundAssign_int() {
String b = " a string ";
b += a;
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -362,7 +366,8 @@ public void stringLiteralRightOperandIsArray() {
// BUG: Diagnostic contains: + Arrays.toString(a)
String b = "a string" + a;
}
}""")
}\
""")
.doTest();
}

Expand All @@ -388,7 +393,8 @@ public void notArray_refactored() {
String b = " a string";
String c = a + b;
}
}""")
}\
""")
.doTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void assertTrue() {
public void assertFalseFromCondition() {
assert 0 == 1;
}
}""")
}\
""")
.doTest();
}

Expand All @@ -70,7 +71,8 @@ public void assertFalse() {
// BUG: Diagnostic contains: throw new AssertionError()
assert false;
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public ListenableFuture<String> apply(Object input) throws Exception {
return immediateFuture(input.toString());
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -139,7 +140,8 @@ public ListenableFuture<Object> apply(String input) throws Exception {
interface MyNonAsyncFunction<I, O> {
ListenableFuture<O> apply(@Nullable I input) throws Exception;
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public int compare(File lhs, File rhs) {
return (int) (rhs.lastModified() - lhs.lastModified());
}
};
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -231,7 +232,8 @@ public char compare(byte n1, byte n2) {
return (char) (n1 - n2);
}
};
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ Builder returnRaw() {
void classLiteral() {
System.out.println(Builder.class);
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -354,7 +355,8 @@ void useNestedBuilder() {
new Builder();
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -438,7 +440,8 @@ Builder returnRaw() {
void classLiteral() {
System.out.println(Builder.class);
}
}""")
}\
""")
.addOutputLines(
"BadImportPositiveCases_expected.java",
"""
Expand Down Expand Up @@ -498,7 +501,8 @@ ImmutableList.Builder returnRaw() {
void classLiteral() {
System.out.println(ImmutableList.Builder.class);
}
}""")
}\
""")
.doTest(TestMode.AST_MATCH);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public void foo() {
// BUG: Diagnostic contains: (long) c >>> 32
result += c >>> 32;
}
}""")
}\
""")
.doTest();
}

Expand All @@ -104,7 +105,8 @@ public void foo() {
result += x >>> 3;
result += (long) (x & 0xff) >> 40;
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ public static final Class<?> methodHandlesDefineClass(byte[] bytes)
// BUG: Diagnostic contains: BanClassLoader
return MethodHandles.lookup().defineClass(bytes);
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -113,7 +114,8 @@ public final Class<?> overrideClassLoader() throws ClassNotFoundException {
private class NotClassLoader {
protected void loadClass() {}
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ private Object subclassesJavaNamingcontext() throws NamingException {
// BUG: Diagnostic contains: BanJNDI
return c.lookup("hello");
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -197,7 +198,8 @@ class BanJNDIPositiveCases {
private void callsList() throws NamingException {
FakeDirContext.list(((Name) new Object()));
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -225,7 +227,8 @@ class BanJNDIPositiveCases {
private void callsList() throws NamingException {
FakeDirContext.list(((Name) new Object()));
}
}""")
}\
""")
.expectUnchanged()
.setArgs("-XepCompilingTestOnlyCode")
.doTest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ void array_precededByNotAValidMethod_isFlagged() {
// BUG: Diagnostic contains: ByteBuffer.array()
buff.array();
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -248,7 +249,8 @@ void array_inLambdaExpression_precededByByteBufferAllocate_isNotFlagged() {
return null;
};
}
}""")
}\
""")
.doTest();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public void method() {
// BUG: Diagnostic contains: Mockito cannot mock
FinalClass local = Mockito.mock(FinalClass.class);
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -105,7 +106,8 @@ static class NonFinalClass {}
public void method() {
NonFinalClass local = Mockito.mock(NonFinalClass.class);
}
}""")
}\
""")
.doTest();
}

Expand All @@ -127,7 +129,8 @@ static final class FinalClass {}
public void method() {
FinalClass local = Mockito.mock(FinalClass.class);
}
}""")
}\
""")
.doTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ Foo otherFoo() {
return this;
}
}
}""")
}\
""")
.doTest();
}

Expand Down Expand Up @@ -190,7 +191,8 @@ Foo otherFoo() {
return this;
}
}
}""")
}\
""")
.doTest();
}
}
Loading

0 comments on commit 72cf2f1

Please sign in to comment.