Skip to content

Commit

Permalink
Add support for simple return with only a BeforeTemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Sep 18, 2021
1 parent a8f1737 commit c779e6f
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
return null;
}
JCCompilationUnit compilationUnit = context.get(JCCompilationUnit.class);
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/google/errorprone/refaster/UFreeIdent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.sun.source.tree.Tree;
import com.sun.source.util.TreeScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.util.Names;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -99,7 +101,15 @@ public Boolean visitIdentifier(IdentifierTree ident, Void v) {
if (!isGood) {
return Choice.none();
} else if (currentBinding == null) {
Symbol currentExprSymbol = ASTHelpers.getSymbol(expression);
if (currentExprSymbol instanceof TypeSymbol
|| (currentExprSymbol instanceof Symbol.MethodSymbol
&& target instanceof JCTree.JCIdent)) {
// The `JCExpression` by itself does not represent a valid Java expression.
return Choice.none();
}
unifier.putBinding(key(), expression);

return Choice.of(unifier);
} else if (currentBinding.toString().equals(expression.toString())) {
// TODO(lowasser): try checking types here in a way that doesn't reject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,9 @@ public void staticImportClassToken() throws IOException {
public void suppressWarnings() throws IOException {
runTest("SuppressWarningsTemplate");
}

@Test
public void onlyBeforeTemplate() throws IOException {
runTest("OnlyBeforeSimpleReturnTemplate");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2021 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.refaster.testdata;

/** Test data for {@code OnlyBeforeSimpleReturnTemplate}. */
public class OnlyBeforeSimpleReturnTemplateExample {
public String foo(String s) {
return s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2021 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.refaster.testdata;

/** Test data for {@code OnlyBeforeSimpleReturnTemplate}. */
public class OnlyBeforeSimpleReturnTemplateExample {
public String foo(String s) {
return /* match found */ s;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2021 The Error Prone Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.errorprone.refaster.testdata.template;

import com.google.errorprone.refaster.annotation.BeforeTemplate;

/**
* Template for handling the case where only the {@link BeforeTemplate} is specified with a simple return
* statement.
*/
public class OnlyBeforeSimpleReturnTemplate {
@BeforeTemplate
String before(String s) {
return s;
}
}

0 comments on commit c779e6f

Please sign in to comment.