Skip to content

Commit

Permalink
MemberName: emphasise the acronym issue in the finding, if it was par…
Browse files Browse the repository at this point in the history
…t of the issue.

PiperOrigin-RevId: 550494030
  • Loading branch information
graememorgan authored and Error Prone Team committed Jul 24, 2023
1 parent dad7174 commit 77ba705
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
Expand Up @@ -89,6 +89,10 @@ public final class MemberName extends BugChecker implements MethodTreeMatcher, V
"Static variables should be named in UPPER_SNAKE_CASE if deeply immutable or lowerCamelCase"
+ " if not.";

private static final String INITIALISM_DETAIL =
", with acronyms treated as words"
+ " (https://google.github.io/styleguide/javaguide.html#s5.3-camel-case)";

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
MethodSymbol symbol = getSymbol(tree);
Expand Down Expand Up @@ -123,16 +127,19 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
if (isConformant(symbol, name)) {
return NO_MATCH;
}
String suggested = fixInitialisms(suggestedRename(symbol, name));
return suggested.equals(name) || !canBeRemoved(symbol, state)
? buildDescription(tree)
.setMessage(
String.format(
"Methods and non-static variables should be named in lowerCamelCase; did you"
+ " mean '%s'?",
suggested))
.build()
: describeMatch(tree, renameMethodWithInvocations(tree, suggested, state));
String renamed = suggestedRename(symbol, name);
String suggested = fixInitialisms(renamed);
boolean fixable = !suggested.equals(name) && canBeRemoved(symbol, state);
String diagnostic =
"Methods and non-static variables should be named in lowerCamelCase"
+ (suggested.equals(renamed) ? "" : INITIALISM_DETAIL);
return buildDescription(tree)
.setMessage(
fixable
? diagnostic
: diagnostic + String.format("; did you" + " mean '%s'?", suggested))
.addFix(fixable ? renameMethodWithInvocations(tree, suggested, state) : emptyFix())
.build();
}

private static boolean hasTestAnnotation(MethodSymbol symbol) {
Expand Down Expand Up @@ -171,17 +178,18 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
if (EXEMPTED_VARIABLE_NAMES.contains(name)) {
return NO_MATCH;
}
String suggested = fixInitialisms(suggestedRename(symbol, name));
String renamed = suggestedRename(symbol, name);
String suggested = fixInitialisms(renamed);
boolean fixable = !suggested.equals(name) && canBeRenamed(symbol);
String diagnostic =
(isStaticVariable(symbol) ? STATIC_VARIABLE_FINDING : message())
+ (suggested.equals(renamed) ? "" : INITIALISM_DETAIL);
return buildDescription(tree)
.addFix(fixable ? renameVariable(tree, suggested, state) : emptyFix())
.setMessage(
isStaticVariable(symbol)
? STATIC_VARIABLE_FINDING
: message()
+ (fixable || suggested.equals(name)
? ""
: (" Did you mean " + suggested + "?")))
fixable
? diagnostic
: diagnostic + String.format("; did you" + " mean '%s'?", suggested))
.addFix(fixable ? renameVariable(tree, suggested, state) : emptyFix())
.build();
}

Expand Down
Expand Up @@ -52,6 +52,21 @@ public void nameWithUnderscores() {
.doTest();
}

@Test
public void nameWithUnderscores_findingEmphasisesInitialism() {
helper
.addSourceLines(
"Test.java",
"class Test {",
" // BUG: Diagnostic contains: acronyms",
" private int misnamedRPCClient;",
" int get() {",
" return misnamedRPCClient;",
" }",
"}")
.doTest();
}

@Test
public void staticFields() {
refactoringHelper
Expand Down

0 comments on commit 77ba705

Please sign in to comment.