From 0f5753f67b89d77c0fa1eac209625b953059c7bb Mon Sep 17 00:00:00 2001 From: ghm Date: Fri, 6 Jan 2023 04:28:56 -0800 Subject: [PATCH] Reverse Yoda conditions in EP. PiperOrigin-RevId: 500146362 --- .../java/com/google/errorprone/matchers/Matchers.java | 4 ++-- .../errorprone/bugpatterns/LambdaFunctionalInterface.java | 2 +- .../google/errorprone/bugpatterns/MutablePublicArray.java | 2 +- .../com/google/errorprone/bugpatterns/OutlineNone.java | 3 ++- .../bugpatterns/StatementSwitchToExpressionSwitch.java | 8 ++++---- .../google/errorprone/bugpatterns/inlineme/Inliner.java | 4 ++-- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java b/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java index ed9b20523cd..d644540503e 100644 --- a/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java +++ b/check_api/src/main/java/com/google/errorprone/matchers/Matchers.java @@ -298,7 +298,7 @@ public static Matcher 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; @@ -990,7 +990,7 @@ public static Matcher variableInitializer( * constant, parameter to a method, etc. */ public static Matcher 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. */ diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/LambdaFunctionalInterface.java b/core/src/main/java/com/google/errorprone/bugpatterns/LambdaFunctionalInterface.java index c3528cbae7a..af7fca37beb 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/LambdaFunctionalInterface.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/LambdaFunctionalInterface.java @@ -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 -> diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MutablePublicArray.java b/core/src/main/java/com/google/errorprone/bugpatterns/MutablePublicArray.java index 164fb6cefe3..79d719f76a1 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MutablePublicArray.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MutablePublicArray.java @@ -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(); diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/OutlineNone.java b/core/src/main/java/com/google/errorprone/bugpatterns/OutlineNone.java index 06e0465ed45..abd1e2f8eb6 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/OutlineNone.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/OutlineNone.java @@ -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. */ @@ -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); } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java index fd490cbc95e..9099a20f0d7 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/StatementSwitchToExpressionSwitch.java @@ -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); } } @@ -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}"); @@ -319,7 +319,7 @@ private static Optional extractCommentsBeforeRemovedBreak( private static ImmutableList 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 diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java index 002d9a0b752..ddca748d1e4 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/inlineme/Inliner.java @@ -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; } }