Skip to content

Commit

Permalink
add a fix to issue google#2518
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjing-0919 committed May 29, 2022
1 parent 933cc17 commit 54c2562
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 301 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions .idea/checkstyle-idea.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ public List<ErrorProneToken> getOffsetTokens(int start, int end) {
getSourceCode().subSequence(start, end).toString(), start, context);
}

//CS304 Issue link: https://github.com/google/error-prone/issues/2709
/** Returns the start position of the node. */
public int getStartPosition(Tree node) {
JCCompilationUnit compilationUnit = (JCCompilationUnit) getPath().getCompilationUnit();
Expand Down
2 changes: 2 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@
<version>${autoservice.version}</version>
</path>
</annotationProcessorPaths>
<source>12</source>
<target>12</target>
</configuration>
</plugin>
<!-- Include the @BugPattern annotation in the main distribution
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,19 @@ public Description matchSwitch(SwitchTree tree, VisitorState state) {
return NO_MATCH;
}

//CS304 Issue link: https://github.com/google/error-prone/issues/2709
if (defaultCase.getCaseKind() == CaseTree.CaseKind.RULE && defaultCase.getBody() != null ){
if (!defaultCase.getBody().toString().equals("{\r\n}")){
return NO_MATCH;
}
}

// If `default` case is empty, and last in switch, add `// fall out` comment
// TODO(epmjohnston): Maybe move comment logic to https://errorprone.info/bugpattern/FallThrough
int idx = tree.getCases().indexOf(defaultCase);
if (idx != tree.getCases().size() - 1) {
return NO_MATCH;
}
//CS304 Issue link: https://github.com/google/error-prone/issues/2709
if (state
.getOffsetTokens(state.getStartPosition(defaultCase), state.getEndPosition(tree))
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,18 @@
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.BlockTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.ReturnTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.*;
import com.sun.source.tree.Tree.Kind;
import com.sun.source.tree.TypeCastTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.SimpleTreeVisitor;
import com.sun.source.util.TreePathScanner;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.code.Types.FunctionDescriptorLookupError;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
import javax.lang.model.element.ElementKind;
Expand All @@ -82,6 +73,14 @@ public Description matchMethod(MethodTree tree, VisitorState state) {
if (!tree.getParameters().isEmpty() || !tree.getThrows().isEmpty()) {
return NO_MATCH;
}
ArrayList<Integer> arr = new ArrayList<>();
List<? extends StatementTree> statements = tree.getBody().getStatements();
for (StatementTree statement: statements){
if (statement.getKind().equals(Kind.ENHANCED_FOR_LOOP)){
return NO_MATCH;
}
}

LambdaExpressionTree lambda = LAMBDA_VISITOR.visit(tree.getBody(), null);
if (lambda == null) {
return NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void arrowSwitchNegative() {
.doTest();
}


//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchWithoutComment(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -286,7 +286,7 @@ public void arrowSwitchWithoutComment(){
.doTest();
}


//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchCommentAboveBraces(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -311,6 +311,7 @@ public void arrowSwitchCommentAboveBraces(){
.doTest();
}

//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchCommentInMiddle(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -334,6 +335,7 @@ public void arrowSwitchCommentInMiddle(){
.doTest();
}

//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchCommentAfterArrow(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -356,6 +358,7 @@ public void arrowSwitchCommentAfterArrow(){
.doTest();
}

//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchCommentInsideBraces(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -380,7 +383,7 @@ public void arrowSwitchCommentInsideBraces(){
.doTest();
}


//CS304 Issue link: https://github.com/google/error-prone/issues/2709
@Test
public void arrowSwitchWithComment(){
assumeTrue(RuntimeVersion.isAtLeast14());
Expand All @@ -401,6 +404,4 @@ public void arrowSwitchWithComment(){
"}")
.doTest();
}


}

0 comments on commit 54c2562

Please sign in to comment.