Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce NameContentEquals Refaster rule #1379

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private static boolean isVariableNameInUse(String name, VisitorState state) {
new TreeScanner<Boolean, @Nullable Void>() {
@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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 {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can debate whether BugCheckerRules is the best place for this rule, but it's in line with other rule definitions in this class.

@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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -31,4 +32,10 @@ ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelpe
String testConstantsFormat() {
return String.format("\"%s\"", Convert.quote("foo"));
}

ImmutableSet<Boolean> testNameContentEquals() {
return ImmutableSet.of(
((Name) null).toString().equals("foo".subSequence(0, 1).toString()),
((com.sun.tools.javac.util.Name) null).toString().equals("bar"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,4 +31,10 @@ ImmutableSet<BugCheckerRefactoringTestHelper> testBugCheckerRefactoringTestHelpe
String testConstantsFormat() {
return Constants.format("foo");
}

ImmutableSet<Boolean> testNameContentEquals() {
return ImmutableSet.of(
((Name) null).contentEquals("foo".subSequence(0, 1)),
((com.sun.tools.javac.util.Name) null).contentEquals("bar"));
}
}