Skip to content

Commit 77ba705

Browse files
graememorganError Prone Team
authored andcommitted
MemberName: emphasise the acronym issue in the finding, if it was part of the issue.
PiperOrigin-RevId: 550494030
1 parent dad7174 commit 77ba705

File tree

2 files changed

+41
-18
lines changed

2 files changed

+41
-18
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public final class MemberName extends BugChecker implements MethodTreeMatcher, V
8989
"Static variables should be named in UPPER_SNAKE_CASE if deeply immutable or lowerCamelCase"
9090
+ " if not.";
9191

92+
private static final String INITIALISM_DETAIL =
93+
", with acronyms treated as words"
94+
+ " (https://google.github.io/styleguide/javaguide.html#s5.3-camel-case)";
95+
9296
@Override
9397
public Description matchMethod(MethodTree tree, VisitorState state) {
9498
MethodSymbol symbol = getSymbol(tree);
@@ -123,16 +127,19 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
123127
if (isConformant(symbol, name)) {
124128
return NO_MATCH;
125129
}
126-
String suggested = fixInitialisms(suggestedRename(symbol, name));
127-
return suggested.equals(name) || !canBeRemoved(symbol, state)
128-
? buildDescription(tree)
129-
.setMessage(
130-
String.format(
131-
"Methods and non-static variables should be named in lowerCamelCase; did you"
132-
+ " mean '%s'?",
133-
suggested))
134-
.build()
135-
: describeMatch(tree, renameMethodWithInvocations(tree, suggested, state));
130+
String renamed = suggestedRename(symbol, name);
131+
String suggested = fixInitialisms(renamed);
132+
boolean fixable = !suggested.equals(name) && canBeRemoved(symbol, state);
133+
String diagnostic =
134+
"Methods and non-static variables should be named in lowerCamelCase"
135+
+ (suggested.equals(renamed) ? "" : INITIALISM_DETAIL);
136+
return buildDescription(tree)
137+
.setMessage(
138+
fixable
139+
? diagnostic
140+
: diagnostic + String.format("; did you" + " mean '%s'?", suggested))
141+
.addFix(fixable ? renameMethodWithInvocations(tree, suggested, state) : emptyFix())
142+
.build();
136143
}
137144

138145
private static boolean hasTestAnnotation(MethodSymbol symbol) {
@@ -171,17 +178,18 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
171178
if (EXEMPTED_VARIABLE_NAMES.contains(name)) {
172179
return NO_MATCH;
173180
}
174-
String suggested = fixInitialisms(suggestedRename(symbol, name));
181+
String renamed = suggestedRename(symbol, name);
182+
String suggested = fixInitialisms(renamed);
175183
boolean fixable = !suggested.equals(name) && canBeRenamed(symbol);
184+
String diagnostic =
185+
(isStaticVariable(symbol) ? STATIC_VARIABLE_FINDING : message())
186+
+ (suggested.equals(renamed) ? "" : INITIALISM_DETAIL);
176187
return buildDescription(tree)
177-
.addFix(fixable ? renameVariable(tree, suggested, state) : emptyFix())
178188
.setMessage(
179-
isStaticVariable(symbol)
180-
? STATIC_VARIABLE_FINDING
181-
: message()
182-
+ (fixable || suggested.equals(name)
183-
? ""
184-
: (" Did you mean " + suggested + "?")))
189+
fixable
190+
? diagnostic
191+
: diagnostic + String.format("; did you" + " mean '%s'?", suggested))
192+
.addFix(fixable ? renameVariable(tree, suggested, state) : emptyFix())
185193
.build();
186194
}
187195

core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public void nameWithUnderscores() {
5252
.doTest();
5353
}
5454

55+
@Test
56+
public void nameWithUnderscores_findingEmphasisesInitialism() {
57+
helper
58+
.addSourceLines(
59+
"Test.java",
60+
"class Test {",
61+
" // BUG: Diagnostic contains: acronyms",
62+
" private int misnamedRPCClient;",
63+
" int get() {",
64+
" return misnamedRPCClient;",
65+
" }",
66+
"}")
67+
.doTest();
68+
}
69+
5570
@Test
5671
public void staticFields() {
5772
refactoringHelper

0 commit comments

Comments
 (0)