32
32
import static com .google .errorprone .bugpatterns .checkreturnvalue .Rules .mapAnnotationSimpleName ;
33
33
import static com .google .errorprone .fixes .SuggestedFix .emptyFix ;
34
34
import static com .google .errorprone .fixes .SuggestedFixes .qualifyType ;
35
+ import static com .google .errorprone .util .ASTHelpers .enclosingClass ;
35
36
import static com .google .errorprone .util .ASTHelpers .getAnnotationsWithSimpleName ;
36
37
import static com .google .errorprone .util .ASTHelpers .getSymbol ;
37
38
import static com .google .errorprone .util .ASTHelpers .getType ;
38
39
import static com .google .errorprone .util .ASTHelpers .hasDirectAnnotationWithSimpleName ;
39
40
import static com .google .errorprone .util .ASTHelpers .isGeneratedConstructor ;
41
+ import static com .google .errorprone .util .ASTHelpers .isLocal ;
40
42
import static com .sun .source .tree .Tree .Kind .METHOD ;
41
43
42
44
import com .google .common .collect .ImmutableMap ;
67
69
import com .sun .tools .javac .code .Symbol ;
68
70
import com .sun .tools .javac .code .Symbol .MethodSymbol ;
69
71
import com .sun .tools .javac .code .Type ;
70
- import com .sun .tools .javac .code .Type .MethodType ;
71
72
import com .sun .tools .javac .processing .JavacProcessingEnvironment ;
72
73
import java .util .Optional ;
73
74
import javax .lang .model .element .ElementKind ;
@@ -92,8 +93,6 @@ public class CheckReturnValue extends AbstractReturnValueIgnored
92
93
static final String CRV_PACKAGES = "CheckReturnValue:Packages" ;
93
94
94
95
private final MessageTrailerStyle messageTrailerStyle ;
95
- private final Optional <ResultUsePolicy > constructorPolicy ;
96
- private final Optional <ResultUsePolicy > methodPolicy ;
97
96
private final ResultUsePolicyEvaluator evaluator ;
98
97
99
98
public CheckReturnValue (ErrorProneFlags flags ) {
@@ -102,8 +101,6 @@ public CheckReturnValue(ErrorProneFlags flags) {
102
101
flags
103
102
.getEnum ("CheckReturnValue:MessageTrailerStyle" , MessageTrailerStyle .class )
104
103
.orElse (NONE );
105
- this .constructorPolicy = defaultPolicy (flags , CHECK_ALL_CONSTRUCTORS );
106
- this .methodPolicy = defaultPolicy (flags , CHECK_ALL_METHODS );
107
104
108
105
ResultUsePolicyEvaluator .Builder builder =
109
106
ResultUsePolicyEvaluator .builder ()
@@ -127,7 +124,13 @@ public CheckReturnValue(ErrorProneFlags flags) {
127
124
flags
128
125
.getList (CRV_PACKAGES )
129
126
.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 ();
131
134
}
132
135
133
136
private static Optional <ResultUsePolicy > defaultPolicy (ErrorProneFlags flags , String flag ) {
@@ -305,11 +308,7 @@ protected Description describeReturnValueIgnored(NewClassTree tree, VisitorState
305
308
protected Description describeReturnValueIgnored (MemberReferenceTree tree , VisitorState state ) {
306
309
MethodSymbol symbol = getSymbol (tree );
307
310
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 () ? "()" : "(...)" ;
313
312
314
313
String shortCallWithoutNew ;
315
314
String shortCall ;
@@ -330,8 +329,8 @@ protected Description describeReturnValueIgnored(MemberReferenceTree tree, Visit
330
329
String message =
331
330
String .format (
332
331
"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 "
335
334
+ "\n "
336
335
+ "To use the result, you may need to restructure your code.\n "
337
336
+ "\n "
@@ -355,8 +354,12 @@ protected Description describeReturnValueIgnored(MemberReferenceTree tree, Visit
355
354
}
356
355
357
356
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
+ */
360
363
return "" ;
361
364
}
362
365
switch (messageTrailerStyle ) {
0 commit comments