Skip to content

Commit

Permalink
Reverse Yoda conditions in EP.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 500146362
  • Loading branch information
graememorgan authored and Error Prone Team committed Jan 6, 2023
1 parent f36a502 commit 0f5753f
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public static Matcher<? super MethodInvocationTree> receiverSameAsArgument(int a
return ASTHelpers.sameVariable(fieldAccess.getExpression(), arg);
} else if (methodSelect instanceof JCIdent) {
// A bare method call: "equals(foo)". Receiver is implicitly "this".
return "this".equals(arg.toString());
return arg.toString().equals("this");
}

return false;
Expand Down Expand Up @@ -990,7 +990,7 @@ public static Matcher<VariableTree> variableInitializer(
* constant, parameter to a method, etc.
*/
public static Matcher<VariableTree> isField() {
return (variableTree, state) -> ElementKind.FIELD == getSymbol(variableTree).getKind();
return (variableTree, state) -> getSymbol(variableTree).getKind() == ElementKind.FIELD;
}

/** Matches if a {@link ClassTree} is an enum declaration. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ private boolean methodCallsMeetConditions(Symbol sym, VisitorState state) {

for (MethodInvocationTree methodInvocationTree : methodCallMap.values()) {
if (methodInvocationTree.getArguments().stream()
.filter(a -> Kind.LAMBDA_EXPRESSION.equals(a.getKind()))
.filter(a -> a.getKind().equals(Kind.LAMBDA_EXPRESSION))
.filter(a -> hasFunctionAsArg(a, state))
.noneMatch(
a ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static boolean nonEmptyArrayMatcher(VariableTree arrayExpression, Visito
JCNewArray newArray = (JCNewArray) initializer;
if (!newArray.getDimensions().isEmpty()) {
return !newArray.getDimensions().stream()
.allMatch(e -> Objects.equals(0, ASTHelpers.constValue(e, Integer.class)));
.allMatch(e -> Objects.equals(ASTHelpers.constValue(e, Integer.class), 0));
}
// For in line array initializer.
return newArray.getInitializers() != null && !newArray.getInitializers().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

/** Check for the a11y antipattern of setting CSS outline attributes to none or 0. */
Expand Down Expand Up @@ -92,7 +93,7 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
*/
if (GWT_SET_PROPERTY.matches(tree, state)
&& args.size() >= 2
&& "outline".equals(constValue(args.get(0), String.class))
&& Objects.equals(constValue(args.get(0), String.class), "outline")
&& constantNoneOrZero(args.get(1))) {
return describeMatch(tree);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ private static AnalysisResult analyzeSwitchTree(SwitchTree switchTree) {
} else {
allCasesHaveDefiniteControlFlow &=
!fallsIntoDefaultCase
&& CaseFallThru.DEFINITELY_DOES_NOT_FALL_THRU.equals(caseFallThru);
&& caseFallThru.equals(CaseFallThru.DEFINITELY_DOES_NOT_FALL_THRU);
}
} else {
// Cases other than default
allCasesHaveDefiniteControlFlow &= !CaseFallThru.MAYBE_FALLS_THRU.equals(caseFallThru);
allCasesHaveDefiniteControlFlow &= !caseFallThru.equals(CaseFallThru.MAYBE_FALLS_THRU);
}
}

Expand Down Expand Up @@ -248,7 +248,7 @@ private Description convertDirectlyToExpressionSwitch(
// If block is just space or a single "break;" with no explanatory comments, then remove
// it to eliminate redundancy and improve readability
if (trimmedTransformedBlockSource.isEmpty()
|| "break;".equals(trimmedTransformedBlockSource)) {
|| trimmedTransformedBlockSource.equals("break;")) {
replacementCodeBuilder.append("{}");
} else {
replacementCodeBuilder.append("{").append(transformedBlockSource).append("\n}");
Expand Down Expand Up @@ -319,7 +319,7 @@ private static Optional<String> extractCommentsBeforeRemovedBreak(
private static ImmutableList<StatementTree> filterOutRedundantBreak(CaseTree caseTree) {
boolean caseEndsWithUnlabelledBreak =
Streams.findLast(caseTree.getStatements().stream())
.filter(statement -> BREAK.equals(statement.getKind()))
.filter(statement -> statement.getKind().equals(BREAK))
.filter(breakTree -> ((BreakTree) breakTree).getLabel() == null)
.isPresent();
return caseEndsWithUnlabelledBreak
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
ExpressionTree methodSelectTree = tree.getMethodSelect();
if (methodSelectTree != null) {
String methodSelect = state.getSourceForNode(methodSelectTree);
if ("super".equals(methodSelect)) {
if (methodSelect.equals("super")) {
receiverString = methodSelect;
}
// TODO(kak): Can we omit the `this` case? The getReceiver() call above handles `this`
if ("this".equals(methodSelect)) {
if (methodSelect.equals("this")) {
receiverString = methodSelect;
}
}
Expand Down

0 comments on commit 0f5753f

Please sign in to comment.