Refaster: fix expression matching in absence of additional syntactic context#4
Refaster: fix expression matching in absence of additional syntactic context#4rickie wants to merge 6 commits into
Conversation
c779e6f to
11ebd54
Compare
Stephan202
left a comment
There was a problem hiding this comment.
I forgot the use case we had for this; can you refresh my memory? I think it'll be important to provide context when we open a PR upstream.
|
Yes of course. The example that is provided in the This problem came up when we were defining the In the end, this seemed to be the minimal example for a reproduction. The The other changes in |
11ebd54 to
581e166
Compare
Stephan202
left a comment
There was a problem hiding this comment.
Rebased and added one commit. Suggested commit message:
Refaster: fix expression matching in absence of additional syntactic context
`@BeforeTemplate` implementations of the form `return someParam;` are valid
constructs. With these changes such templates behave as expected, such that
`someParam` only matches expressions of the appropriate type, not references to
the type itself.
| @Override | ||
| public Void scan(Tree tree, Context context) { | ||
| if (tree == null) { | ||
| if (tree == null || tree instanceof JCTree.JCPackageDecl) { |
There was a problem hiding this comment.
Ideally we'd do this:
| if (tree == null || tree instanceof JCTree.JCPackageDecl) { | |
| if (tree == null || tree instanceof PackageTree) { |
But PackageTree is marked @since 9. Perhaps we should wait for (or contribute) the changes necessary to target JDK 11, so that we can make this nice. See google#2783 (comment).
Edit: it's just IDEA flagging this; the build does pass with this change. In light of the above I think it's fine to go ahead with this then.
| Symbol currentExprSymbol = ASTHelpers.getSymbol(expression); | ||
| if (currentExprSymbol instanceof TypeSymbol | ||
| || (currentExprSymbol instanceof Symbol.MethodSymbol | ||
| && target instanceof JCTree.JCIdent)) { |
There was a problem hiding this comment.
IIUC we can also use an interface here:
| && target instanceof JCTree.JCIdent)) { | |
| && target instanceof IdentifierTree)) { |
| } | ||
|
|
||
| @Test | ||
| public void onlyBeforeTemplate() throws IOException { |
There was a problem hiding this comment.
| public void onlyBeforeTemplate() throws IOException { | |
| public void onlyBeforeSimpleReturnTemplate() throws IOException { |
| /** | ||
| * Template for handling the case where only the {@link BeforeTemplate} is specified with a simple return | ||
| * statement. | ||
| */ |
There was a problem hiding this comment.
| /** | |
| * Template for handling the case where only the {@link BeforeTemplate} is specified with a simple return | |
| * statement. | |
| */ | |
| /** | |
| * Template that flags all string-typed expressions, irrespective of the syntactic context in which | |
| * they occur. | |
| */ |
| @@ -0,0 +1,24 @@ | |||
| /* | |||
| * Copyright 2021 The Error Prone Authors. | |||
b1adbf3 to
21bc6e2
Compare
|
Upstream PR: google#2805 |
3b86713 to
a1fd29b
Compare
a1fd29b to
e3d86cf
Compare
e3d86cf to
29b528a
Compare
29b528a to
e5a7be0
Compare
79366f8 to
8bab354
Compare
8bab354 to
d334c28
Compare
d334c28 to
fdc96e3
Compare
fdc96e3 to
d3d6b1d
Compare
@BeforeTemplateimplementations of the formreturn someParam;are validconstructs. With these changes such templates behave as expected, such that
someParamonly matches expressions of the appropriate type, not references tothe type itself.