diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ConstantNaming.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ConstantNaming.java index a0354a83c6..73522b7c18 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ConstantNaming.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/ConstantNaming.java @@ -112,7 +112,7 @@ private static boolean isVariableNameInUse(String name, VisitorState state) { new TreeScanner() { @Override public Boolean visitVariable(VariableTree tree, @Nullable Void unused) { - return ASTHelpers.getSymbol(tree).getSimpleName().toString().equals(name) + return ASTHelpers.getSymbol(tree).getSimpleName().contentEquals(name) || super.visitVariable(tree, null); } diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java index db21ddb711..9ce8236f77 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/BugCheckerRules.java @@ -9,6 +9,7 @@ import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.sun.tools.javac.util.Constants; import com.sun.tools.javac.util.Convert; +import javax.lang.model.element.Name; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; /** Refaster rules related to {@link com.google.errorprone.bugpatterns.BugChecker} classes. */ @@ -67,4 +68,22 @@ String after(String value) { return Constants.format(value); } } + + /** Prefer {@link Name#contentEquals(CharSequence)} over more verbose alternatives. */ + static final class NameContentEquals { + @BeforeTemplate + boolean before(Name name, CharSequence string) { + return name.toString().equals(string.toString()); + } + + @BeforeTemplate + boolean before(Name name, String string) { + return name.toString().equals(string); + } + + @AfterTemplate + boolean after(Name name, CharSequence string) { + return name.contentEquals(string); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java index 91b0bf5962..6d1ac1db33 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestInput.java @@ -5,6 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers; import com.google.errorprone.bugpatterns.BugChecker; import com.sun.tools.javac.util.Convert; +import javax.lang.model.element.Name; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase { @@ -31,4 +32,10 @@ ImmutableSet testBugCheckerRefactoringTestHelpe String testConstantsFormat() { return String.format("\"%s\"", Convert.quote("foo")); } + + ImmutableSet testNameContentEquals() { + return ImmutableSet.of( + ((Name) null).toString().equals("foo".subSequence(0, 1).toString()), + ((com.sun.tools.javac.util.Name) null).toString().equals("bar")); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java index 013617aad6..3227c4207b 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/BugCheckerRulesTestOutput.java @@ -6,6 +6,7 @@ import com.google.errorprone.bugpatterns.BugChecker; import com.sun.tools.javac.util.Constants; import com.sun.tools.javac.util.Convert; +import javax.lang.model.element.Name; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class BugCheckerRulesTest implements RefasterRuleCollectionTestCase { @@ -30,4 +31,10 @@ ImmutableSet testBugCheckerRefactoringTestHelpe String testConstantsFormat() { return Constants.format("foo"); } + + ImmutableSet testNameContentEquals() { + return ImmutableSet.of( + ((Name) null).contentEquals("foo".subSequence(0, 1)), + ((com.sun.tools.javac.util.Name) null).contentEquals("bar")); + } }