Skip to content

Commit

Permalink
Warn about usage of toString on TypeMirror and Element, not jus…
Browse files Browse the repository at this point in the history
…t `Type` and `Symbol`.

PiperOrigin-RevId: 634485413
  • Loading branch information
cpovirk authored and Error Prone Team committed May 17, 2024
1 parent 45ae00b commit cd4178f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@
import javax.inject.Inject;

/**
* Flags {@code com.sun.tools.javac.code.Symbol#toString} usage in {@link BugChecker}s.
* Flags {@code javax.lang.model.element.Element#toString} usage in {@link BugChecker}s.
*
* @author bhagwani@google.com (Sumit Bhagwani)
*/
@BugPattern(
summary = "Symbol#toString shouldn't be used for comparison as it is expensive and fragile.",
summary = "Element#toString shouldn't be used for comparison as it is expensive and fragile.",
severity = SUGGESTION)
public class SymbolToString extends AbstractToString {

private static final TypePredicate IS_SYMBOL = isDescendantOf("com.sun.tools.javac.code.Symbol");
private static final TypePredicate IS_SYMBOL = isDescendantOf("javax.lang.model.element.Element");

private static final Matcher<Tree> STRING_EQUALS =
toType(
Expand All @@ -72,7 +72,7 @@ protected TypePredicate typePredicate() {

@Override
protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorState state) {
return Optional.of("Symbol#toString shouldn't be used as it is expensive and fragile.");
return Optional.of("Element#toString shouldn't be used as it is expensive and fragile.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,17 @@
import javax.inject.Inject;

/**
* Flags {@code com.sun.tools.javac.code.Type#toString} usage in {@link BugChecker}s.
* Flags {@code javax.lang.model.type.TypeMirror#toString} usage in {@link BugChecker}s.
*
* @author bhagwani@google.com (Sumit Bhagwani)
*/
@BugPattern(
summary = "Type#toString shouldn't be used for comparison as it is expensive and fragile.",
summary =
"TypeMirror#toString shouldn't be used for comparison as it is expensive and fragile.",
severity = SUGGESTION)
public class TypeToString extends AbstractToString {

private static final TypePredicate IS_TYPE = isDescendantOf("com.sun.tools.javac.code.Type");
private static final TypePredicate IS_TYPE = isDescendantOf("javax.lang.model.type.TypeMirror");

private static final Matcher<Tree> STRING_EQUALS =
toType(
Expand All @@ -72,7 +73,7 @@ protected TypePredicate typePredicate() {

@Override
protected Optional<String> descriptionMessageForDefaultMatch(Type type, VisitorState state) {
return Optional.of("Type#toString shouldn't be used as it is expensive and fragile.");
return Optional.of("TypeMirror#toString shouldn't be used as it is expensive and fragile.");
}

@Override
Expand Down

0 comments on commit cd4178f

Please sign in to comment.