|
| 1 | +/* |
| 2 | + * Copyright 2025 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.collectionincompatibletype; |
| 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.matchers.Description.NO_MATCH; |
| 22 | +import static com.google.errorprone.matchers.Matchers.instanceMethod; |
| 23 | +import static com.google.errorprone.matchers.Matchers.staticMethod; |
| 24 | +import static com.google.errorprone.util.ASTHelpers.getReceiver; |
| 25 | +import static com.google.errorprone.util.ASTHelpers.getSymbol; |
| 26 | +import static com.google.errorprone.util.ASTHelpers.getType; |
| 27 | +import static com.google.errorprone.util.Signatures.prettyType; |
| 28 | +import static java.lang.String.format; |
| 29 | + |
| 30 | +import com.google.common.collect.ImmutableSet; |
| 31 | +import com.google.errorprone.BugPattern; |
| 32 | +import com.google.errorprone.VisitorState; |
| 33 | +import com.google.errorprone.bugpatterns.BugChecker; |
| 34 | +import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; |
| 35 | +import com.google.errorprone.matchers.Description; |
| 36 | +import com.google.errorprone.matchers.Matcher; |
| 37 | +import com.sun.source.tree.ExpressionTree; |
| 38 | +import com.sun.source.tree.MethodInvocationTree; |
| 39 | +import com.sun.source.tree.Tree; |
| 40 | +import com.sun.tools.javac.code.Type; |
| 41 | + |
| 42 | +/** A BugPattern; see the summary. */ |
| 43 | +@BugPattern(summary = "The types passed to this assertion are incompatible.", severity = WARNING) |
| 44 | +public final class AssertSameIncompatible extends BugChecker |
| 45 | + implements MethodInvocationTreeMatcher { |
| 46 | + private static final Matcher<ExpressionTree> JUNIT_MATCHER = |
| 47 | + staticMethod() |
| 48 | + .onClassAny("org.junit.Assert", "junit.framework.Assert", "junit.framework.TestCase") |
| 49 | + .namedAnyOf("assertSame", "assertNotSame"); |
| 50 | + |
| 51 | + private static final Matcher<ExpressionTree> TRUTH_MATCHER = |
| 52 | + instanceMethod() |
| 53 | + .onDescendantOf("com.google.common.truth.Subject") |
| 54 | + .namedAnyOf("isSameInstanceAs", "isNotSameInstanceAs"); |
| 55 | + |
| 56 | + private static final ImmutableSet<String> NEGATIVE_ASSERTIONS = |
| 57 | + ImmutableSet.of("assertNotSame", "isNotSameInstanceAs"); |
| 58 | + |
| 59 | + @Override |
| 60 | + public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { |
| 61 | + if (JUNIT_MATCHER.matches(tree, state)) { |
| 62 | + return handleJUnit(tree, state); |
| 63 | + } |
| 64 | + if (TRUTH_MATCHER.matches(tree, state)) { |
| 65 | + return handleTruth(tree, state); |
| 66 | + } |
| 67 | + return NO_MATCH; |
| 68 | + } |
| 69 | + |
| 70 | + private Description handleJUnit(MethodInvocationTree tree, VisitorState state) { |
| 71 | + var arguments = tree.getArguments(); |
| 72 | + // Grab the last and penultimate arguments to deal with the message-taking overloads. |
| 73 | + var expected = getType(arguments.reversed().get(1)); |
| 74 | + var actual = getType(arguments.reversed().get(0)); |
| 75 | + return handle( |
| 76 | + tree, |
| 77 | + actual, |
| 78 | + expected, |
| 79 | + NEGATIVE_ASSERTIONS.contains(getSymbol(tree).getSimpleName().toString()), |
| 80 | + state); |
| 81 | + } |
| 82 | + |
| 83 | + private Description handleTruth(MethodInvocationTree tree, VisitorState state) { |
| 84 | + var expected = getType(tree.getArguments().get(0)); |
| 85 | + var assertThat = getReceiver(tree); |
| 86 | + if ((!(assertThat instanceof MethodInvocationTree mit) |
| 87 | + || !getSymbol(mit).getSimpleName().contentEquals("assertThat") |
| 88 | + || mit.getArguments().size() != 1)) { |
| 89 | + return NO_MATCH; |
| 90 | + } |
| 91 | + var actual = getType(getOnlyElement(mit.getArguments())); |
| 92 | + return handle( |
| 93 | + tree, actual, expected, getSymbol(tree).getSimpleName().toString().contains("Not"), state); |
| 94 | + } |
| 95 | + |
| 96 | + private Description handle( |
| 97 | + Tree tree, Type actual, Type expected, boolean inverted, VisitorState state) { |
| 98 | + if (compatible(actual, expected, state)) { |
| 99 | + return NO_MATCH; |
| 100 | + } |
| 101 | + return buildDescription(tree) |
| 102 | + .setMessage( |
| 103 | + format( |
| 104 | + "The types passed to this assertion are incompatible (this check will always %s):" |
| 105 | + + " type `%s` is not compatible with `%s`", |
| 106 | + inverted ? "pass" : "fail", prettyType(actual), prettyType(expected))) |
| 107 | + .build(); |
| 108 | + } |
| 109 | + |
| 110 | + private static boolean compatible(Type typeA, Type typeB, VisitorState state) { |
| 111 | + var types = state.getTypes(); |
| 112 | + Type erasedA = types.erasure(typeA); |
| 113 | + Type erasedB = types.erasure(typeB); |
| 114 | + return types.isCastable(erasedA, erasedB) || types.isCastable(erasedB, erasedA); |
| 115 | + } |
| 116 | +} |
0 commit comments