Skip to content

Commit bf5d25f

Browse files
cpovirkError Prone Team
authored andcommitted
Fix minor error-message problems:
- Fix a bug with the "Full API" line for local classes. - Convert an em dash + line break into a double dash, and remove the accidental preceding period: The line break was kind of weird, and an em dash won't necessarily render well in all environments. While I was in the file, IntelliJ was getting on me to inline a couple fields into the constructor and to remove an unnecessary cast to `MethodType`, so I did those things, too. PiperOrigin-RevId: 479350008
1 parent e06811f commit bf5d25f

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

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

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@
3232
import static com.google.errorprone.bugpatterns.checkreturnvalue.Rules.mapAnnotationSimpleName;
3333
import static com.google.errorprone.fixes.SuggestedFix.emptyFix;
3434
import static com.google.errorprone.fixes.SuggestedFixes.qualifyType;
35+
import static com.google.errorprone.util.ASTHelpers.enclosingClass;
3536
import static com.google.errorprone.util.ASTHelpers.getAnnotationsWithSimpleName;
3637
import static com.google.errorprone.util.ASTHelpers.getSymbol;
3738
import static com.google.errorprone.util.ASTHelpers.getType;
3839
import static com.google.errorprone.util.ASTHelpers.hasDirectAnnotationWithSimpleName;
3940
import static com.google.errorprone.util.ASTHelpers.isGeneratedConstructor;
41+
import static com.google.errorprone.util.ASTHelpers.isLocal;
4042
import static com.sun.source.tree.Tree.Kind.METHOD;
4143

4244
import com.google.common.collect.ImmutableMap;
@@ -67,7 +69,6 @@
6769
import com.sun.tools.javac.code.Symbol;
6870
import com.sun.tools.javac.code.Symbol.MethodSymbol;
6971
import com.sun.tools.javac.code.Type;
70-
import com.sun.tools.javac.code.Type.MethodType;
7172
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
7273
import java.util.Optional;
7374
import javax.lang.model.element.ElementKind;
@@ -92,8 +93,6 @@ public class CheckReturnValue extends AbstractReturnValueIgnored
9293
static final String CRV_PACKAGES = "CheckReturnValue:Packages";
9394

9495
private final MessageTrailerStyle messageTrailerStyle;
95-
private final Optional<ResultUsePolicy> constructorPolicy;
96-
private final Optional<ResultUsePolicy> methodPolicy;
9796
private final ResultUsePolicyEvaluator evaluator;
9897

9998
public CheckReturnValue(ErrorProneFlags flags) {
@@ -102,8 +101,6 @@ public CheckReturnValue(ErrorProneFlags flags) {
102101
flags
103102
.getEnum("CheckReturnValue:MessageTrailerStyle", MessageTrailerStyle.class)
104103
.orElse(NONE);
105-
this.constructorPolicy = defaultPolicy(flags, CHECK_ALL_CONSTRUCTORS);
106-
this.methodPolicy = defaultPolicy(flags, CHECK_ALL_METHODS);
107104

108105
ResultUsePolicyEvaluator.Builder builder =
109106
ResultUsePolicyEvaluator.builder()
@@ -127,7 +124,13 @@ public CheckReturnValue(ErrorProneFlags flags) {
127124
flags
128125
.getList(CRV_PACKAGES)
129126
.ifPresent(packagePatterns -> builder.addRule(PackagesRule.fromPatterns(packagePatterns)));
130-
this.evaluator = builder.addRule(globalDefault(methodPolicy, constructorPolicy)).build();
127+
this.evaluator =
128+
builder
129+
.addRule(
130+
globalDefault(
131+
defaultPolicy(flags, CHECK_ALL_METHODS),
132+
defaultPolicy(flags, CHECK_ALL_CONSTRUCTORS)))
133+
.build();
131134
}
132135

133136
private static Optional<ResultUsePolicy> defaultPolicy(ErrorProneFlags flags, String flag) {
@@ -305,11 +308,7 @@ protected Description describeReturnValueIgnored(NewClassTree tree, VisitorState
305308
protected Description describeReturnValueIgnored(MemberReferenceTree tree, VisitorState state) {
306309
MethodSymbol symbol = getSymbol(tree);
307310
Type type = state.getTypes().memberType(getType(tree.getQualifierExpression()), symbol);
308-
// TODO(cgdecker): There are probably other types than MethodType that we could resolve here
309-
String parensAndMaybeEllipsis =
310-
type instanceof MethodType && ((MethodType) type).getParameterTypes().isEmpty()
311-
? "()"
312-
: "(...)";
311+
String parensAndMaybeEllipsis = type.getParameterTypes().isEmpty() ? "()" : "(...)";
313312

314313
String shortCallWithoutNew;
315314
String shortCall;
@@ -330,8 +329,8 @@ protected Description describeReturnValueIgnored(MemberReferenceTree tree, Visit
330329
String message =
331330
String.format(
332331
"The result of `%s` must be used\n"
333-
+ "`%s` acts as an implementation of `%s`.\n"
334-
+ " which is a `void` method, so it doesn't use the result of `%s`.\n"
332+
+ "`%s` acts as an implementation of `%s`"
333+
+ " -- which is a `void` method, so it doesn't use the result of `%s`.\n"
335334
+ "\n"
336335
+ "To use the result, you may need to restructure your code.\n"
337336
+ "\n"
@@ -355,8 +354,12 @@ protected Description describeReturnValueIgnored(MemberReferenceTree tree, Visit
355354
}
356355

357356
private String apiTrailer(MethodSymbol symbol, VisitorState state) {
358-
if (symbol.enclClass().isAnonymous()) {
359-
// I don't think we have a defined format for members of anonymous classes.
357+
// (isLocal returns true for both local classes and anonymous classes. That's good for us.)
358+
if (isLocal(enclosingClass(symbol))) {
359+
/*
360+
* We don't have a defined format for members of local and anonymous classes. After all, their
361+
* generated class names can change easily as other such classes are introduced.
362+
*/
360363
return "";
361364
}
362365
switch (messageTrailerStyle) {

core/src/test/java/com/google/errorprone/bugpatterns/testdata/CheckReturnValuePositiveCases.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public void testResolvedToVoidLambda() {
6464
public void testResolvedToVoidMethodReference(boolean predicate) {
6565
// BUG: Diagnostic contains: The result of `increment()` must be used
6666
//
67-
// `this.intValue::increment` acts as an implementation of `Runnable.run`.
68-
// which is a `void` method, so it doesn't use the result of `increment()`.
67+
// `this.intValue::increment` acts as an implementation of `Runnable.run`
68+
// -- which is a `void` method, so it doesn't use the result of `increment()`.
6969
//
7070
// To use the result, you may need to restructure your code.
7171
//
@@ -82,8 +82,8 @@ public void testResolvedToVoidMethodReference(boolean predicate) {
8282
public void testConstructorResolvedToVoidMethodReference() {
8383
// BUG: Diagnostic contains: The result of `new MyObject()` must be used
8484
//
85-
// `MyObject::new` acts as an implementation of `Runnable.run`.
86-
// which is a `void` method, so it doesn't use the result of `new MyObject()`.
85+
// `MyObject::new` acts as an implementation of `Runnable.run`
86+
// -- which is a `void` method, so it doesn't use the result of `new MyObject()`.
8787
//
8888
// To use the result, you may need to restructure your code.
8989
//

0 commit comments

Comments
 (0)