-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refaster: fix expression matching in absence of additional syntactic context #4
base: master
Are you sure you want to change the base?
Conversation
c779e6f
to
11ebd54
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
@@ -101,7 +101,7 @@ public Void visitVariable(VariableTree node, Context context) { | |||
|
|||
@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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC we can also use an interface here:
&& target instanceof JCTree.JCIdent)) { | |
&& target instanceof IdentifierTree)) { |
@@ -365,4 +365,9 @@ public void staticImportClassToken() throws IOException { | |||
public void suppressWarnings() throws IOException { | |||
runTest("SuppressWarningsTemplate"); | |||
} | |||
|
|||
@Test | |||
public void onlyBeforeTemplate() throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/** | |
* 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can now use 2022.
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
@BeforeTemplate
implementations of the formreturn someParam;
are validconstructs. With these changes such templates behave as expected, such that
someParam
only matches expressions of the appropriate type, not references tothe type itself.