|
24 | 24 | import static com.google.errorprone.util.ASTHelpers.getReceiver;
|
25 | 25 | import static com.google.errorprone.util.ASTHelpers.getSymbol;
|
26 | 26 | import static com.google.errorprone.util.ASTHelpers.getType;
|
| 27 | +import static com.google.errorprone.util.ASTHelpers.isSubtype; |
27 | 28 |
|
| 29 | +import com.google.errorprone.ErrorProneFlags; |
28 | 30 | import com.google.errorprone.VisitorState;
|
29 | 31 | import com.google.errorprone.annotations.FormatMethod;
|
30 | 32 | import com.google.errorprone.bugpatterns.BugChecker.BinaryTreeMatcher;
|
@@ -115,6 +117,15 @@ protected abstract Optional<Fix> toStringFix(
|
115 | 117 | .named("append")
|
116 | 118 | .withParameters("java.lang.Object"));
|
117 | 119 |
|
| 120 | + private static final Matcher<ExpressionTree> JOINER = |
| 121 | + instanceMethod().onDescendantOf("com.google.common.base.Joiner").named("join"); |
| 122 | + |
| 123 | + private final boolean handleJoiner; |
| 124 | + |
| 125 | + protected AbstractToString(ErrorProneFlags flags) { |
| 126 | + this.handleJoiner = flags.getBoolean("AbstractToString:Joiner").orElse(true); |
| 127 | + } |
| 128 | + |
118 | 129 | private static boolean isInVarargsPosition(
|
119 | 130 | ExpressionTree argTree, MethodInvocationTree methodInvocationTree, VisitorState state) {
|
120 | 131 | int parameterCount = getSymbol(methodInvocationTree).getParameters().size();
|
@@ -166,6 +177,14 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
|
166 | 177 | handleStringifiedTree(argTree, ToStringKind.FLOGGER, state);
|
167 | 178 | }
|
168 | 179 | }
|
| 180 | + if (handleJoiner && JOINER.matches(tree, state)) { |
| 181 | + var symbol = getSymbol(tree); |
| 182 | + if (!isSubtype(symbol.params().get(0).type, state.getSymtab().iterableType, state)) { |
| 183 | + for (ExpressionTree argTree : tree.getArguments()) { |
| 184 | + handleStringifiedTree(argTree, ToStringKind.IMPLICIT, state); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
169 | 188 | return NO_MATCH;
|
170 | 189 | }
|
171 | 190 |
|
|
0 commit comments