|
| 1 | +/* |
| 2 | + * Copyright 2023 The Error Prone Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.errorprone.bugpatterns; |
| 18 | + |
| 19 | +import static com.google.common.collect.Iterables.getOnlyElement; |
| 20 | +import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; |
| 21 | +import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR; |
| 22 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 23 | +import static com.google.errorprone.util.ASTHelpers.enclosingClass; |
| 24 | +import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 25 | +import static com.sun.source.tree.Tree.Kind.BLOCK; |
| 26 | +import static com.sun.source.tree.Tree.Kind.IDENTIFIER; |
| 27 | +import static com.sun.source.tree.Tree.Kind.MEMBER_SELECT; |
| 28 | +import static com.sun.source.tree.Tree.Kind.METHOD; |
| 29 | +import static com.sun.source.tree.Tree.Kind.RETURN; |
| 30 | + |
| 31 | +import com.google.errorprone.BugPattern; |
| 32 | +import com.google.errorprone.VisitorState; |
| 33 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 34 | +import com.google.errorprone.fixes.SuggestedFix; |
| 35 | +import com.google.errorprone.matchers.Description; |
| 36 | +import com.sun.source.tree.BlockTree; |
| 37 | +import com.sun.source.tree.IdentifierTree; |
| 38 | +import com.sun.source.tree.MemberSelectTree; |
| 39 | +import com.sun.source.tree.MethodInvocationTree; |
| 40 | + |
| 41 | +/** A {@link BugChecker}; see the associated {@link BugPattern} annotation for details. */ |
| 42 | +@BugPattern( |
| 43 | + summary = "`super.equals(obj)` is equivalent to `this == obj` here", |
| 44 | + severity = WARNING, |
| 45 | + tags = LIKELY_ERROR) |
| 46 | +public class SuperEqualsIsObjectEquals extends BugChecker implements MethodInvocationTreeMatcher { |
| 47 | + @Override |
| 48 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
| 49 | + var methodSelect = tree.getMethodSelect(); |
| 50 | + if (methodSelect.getKind() != MEMBER_SELECT) { |
| 51 | + return NO_MATCH; |
| 52 | + } |
| 53 | + var memberSelect = (MemberSelectTree) methodSelect; |
| 54 | + var expression = memberSelect.getExpression(); |
| 55 | + if (expression.getKind() == IDENTIFIER |
| 56 | + && ((IdentifierTree) expression).getName().equals(state.getNames()._super) |
| 57 | + // We can't use a Matcher because onExactClass suffers from b/130658266. |
| 58 | + && enclosingClass(getSymbol(tree)) == state.getSymtab().objectType.tsym |
| 59 | + && memberSelect.getIdentifier().equals(state.getNames().equals) |
| 60 | + /* |
| 61 | + * We ignore an override that is merely `return super.equals(obj)`. Such an override is less |
| 62 | + * likely to be a bug because it may exist for the purpose of adding Javadoc. |
| 63 | + * |
| 64 | + * TODO(cpovirk): Consider flagging even that if the method does *not* have Javadoc. |
| 65 | + */ |
| 66 | + && !methodBodyIsOnlyReturnSuperEquals(state)) { |
| 67 | + /* |
| 68 | + * There will often be better fixes than this, some of which would change behavior. But let's |
| 69 | + * at least suggest the simple thing that's always equivalent. |
| 70 | + */ |
| 71 | + return describeMatch( |
| 72 | + tree, |
| 73 | + SuggestedFix.replace( |
| 74 | + tree, "this == " + state.getSourceForNode(getOnlyElement(tree.getArguments())))); |
| 75 | + } |
| 76 | + return NO_MATCH; |
| 77 | + } |
| 78 | + |
| 79 | + private static boolean methodBodyIsOnlyReturnSuperEquals(VisitorState state) { |
| 80 | + var parentPath = state.getPath().getParentPath(); |
| 81 | + if (parentPath.getLeaf().getKind() != RETURN) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + var grandparentPath = parentPath.getParentPath(); |
| 85 | + var grandparent = grandparentPath.getLeaf(); |
| 86 | + if (grandparent.getKind() != BLOCK) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + if (((BlockTree) grandparent).getStatements().size() > 1) { |
| 90 | + return false; |
| 91 | + } |
| 92 | + var greatGrandparent = grandparentPath.getParentPath().getLeaf(); |
| 93 | + if (greatGrandparent.getKind() != METHOD) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + return true; |
| 97 | + } |
| 98 | +} |
0 commit comments