|
| 1 | +/* |
| 2 | + * Copyright 2022 ConsenSys AG. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 5 | + * the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 10 | + * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 11 | + * specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | +package tech.pegasys.tools.epchecks; |
| 14 | + |
| 15 | +import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION; |
| 16 | +import static com.google.errorprone.matchers.Description.NO_MATCH; |
| 17 | +import static com.sun.source.tree.Tree.Kind.EQUAL_TO; |
| 18 | +import static com.sun.source.tree.Tree.Kind.IDENTIFIER; |
| 19 | +import static com.sun.source.tree.Tree.Kind.NOT_EQUAL_TO; |
| 20 | +import static com.sun.source.tree.Tree.Kind.NULL_LITERAL; |
| 21 | + |
| 22 | +import com.google.auto.service.AutoService; |
| 23 | +import com.google.errorprone.BugPattern; |
| 24 | +import com.google.errorprone.VisitorState; |
| 25 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 26 | +import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher; |
| 27 | +import com.google.errorprone.matchers.Description; |
| 28 | +import com.google.errorprone.matchers.Matcher; |
| 29 | +import com.google.errorprone.matchers.Matchers; |
| 30 | +import com.google.errorprone.util.ASTHelpers; |
| 31 | +import com.sun.source.tree.BinaryTree; |
| 32 | +import com.sun.source.tree.IdentifierTree; |
| 33 | +import com.sun.source.tree.Tree; |
| 34 | +import com.sun.tools.javac.code.Type; |
| 35 | + |
| 36 | +/** |
| 37 | + * This check is very similar to the <a |
| 38 | + * href=http://errorprone.info/bugpattern/ReferenceEquality>ReferenceEquality</a> check in Error |
| 39 | + * Prone. But this one identifies <code>Object</code> comparisons for types without an explicitly |
| 40 | + * declared <code>equals()</code> method. |
| 41 | + */ |
| 42 | +@AutoService(BugChecker.class) |
| 43 | +@BugPattern( |
| 44 | + name = "ReferenceComparison", |
| 45 | + summary = "Reference comparison should be value comparison.", |
| 46 | + severity = SUGGESTION, |
| 47 | + linkType = BugPattern.LinkType.NONE) |
| 48 | +public class ReferenceComparison extends BugChecker implements BinaryTreeMatcher { |
| 49 | + private static final Matcher<BinaryTree> REFERENCE_COMPARISON = |
| 50 | + Matchers.anyOf(Matchers.kindIs(EQUAL_TO), Matchers.kindIs(NOT_EQUAL_TO)); |
| 51 | + private static final Matcher<Tree> NULL = Matchers.kindIs(NULL_LITERAL); |
| 52 | + private static final Matcher<Tree> ENUM = Matchers.isSubtypeOf("java.lang.Enum"); |
| 53 | + private static final Matcher<Tree> CLASS = Matchers.isSubtypeOf("java.lang.Class"); |
| 54 | + |
| 55 | + @Override |
| 56 | + public Description matchBinary(BinaryTree tree, VisitorState state) { |
| 57 | + // We're looking for reference comparisons (== and !=). |
| 58 | + if (!REFERENCE_COMPARISON.matches(tree, state)) { |
| 59 | + return NO_MATCH; |
| 60 | + } |
| 61 | + |
| 62 | + final Tree left = tree.getLeftOperand(); |
| 63 | + final Tree right = tree.getRightOperand(); |
| 64 | + final Type leftType = ASTHelpers.getType(left); |
| 65 | + final Type rightType = ASTHelpers.getType(right); |
| 66 | + |
| 67 | + if (leftType == null || rightType == null) { |
| 68 | + return NO_MATCH; |
| 69 | + } |
| 70 | + |
| 71 | + // When a boxed type is compared to a primitive, it'll be unboxed. |
| 72 | + if (leftType.isPrimitive() || rightType.isPrimitive()) { |
| 73 | + return NO_MATCH; |
| 74 | + } |
| 75 | + |
| 76 | + // Ignore comparisons with "this" identifier. |
| 77 | + if (isThis(left) || isThis(right)) { |
| 78 | + return NO_MATCH; |
| 79 | + } |
| 80 | + |
| 81 | + // Ignore null comparisons. |
| 82 | + if (NULL.matches(left, state) || NULL.matches(right, state)) { |
| 83 | + return NO_MATCH; |
| 84 | + } |
| 85 | + |
| 86 | + // Ignore reference comparisons with enums. Those are a special case. |
| 87 | + if (ENUM.matches(left, state) || ENUM.matches(right, state)) { |
| 88 | + return NO_MATCH; |
| 89 | + } |
| 90 | + |
| 91 | + // Ignore class comparisons, those are generally fine. |
| 92 | + if (CLASS.matches(left, state) || CLASS.matches(right, state)) { |
| 93 | + return NO_MATCH; |
| 94 | + } |
| 95 | + |
| 96 | + return describeMatch(tree); |
| 97 | + } |
| 98 | + |
| 99 | + private static boolean isThis(Tree tree) { |
| 100 | + if (tree.getKind().equals(IDENTIFIER)) { |
| 101 | + final IdentifierTree identifier = (IdentifierTree) tree; |
| 102 | + return identifier.getName().contentEquals("this"); |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | +} |
0 commit comments