From 77ba705df2c213eddd5652622336f4a8fef3c387 Mon Sep 17 00:00:00 2001 From: ghm Date: Mon, 24 Jul 2023 02:38:55 -0700 Subject: [PATCH] MemberName: emphasise the acronym issue in the finding, if it was part of the issue. PiperOrigin-RevId: 550494030 --- .../errorprone/bugpatterns/MemberName.java | 44 +++++++++++-------- .../bugpatterns/MemberNameTest.java | 15 +++++++ 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java index 5c2f63e7ae0..d8c6c00942e 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MemberName.java @@ -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); @@ -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) { @@ -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(); } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java index 4a4438ee603..abec4f53d28 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/MemberNameTest.java @@ -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