Merge remote-tracking branch 'origin/master'
authorDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 14 Jun 2016 08:05:03 +0000 (11:05 +0300)
committerDmitry Batkovich <dmitry.batkovich@jetbrains.com>
Tue, 14 Jun 2016 08:05:03 +0000 (11:05 +0300)
RegExpSupport/test/org/intellij/lang/regexp/RegExpParseTest.java
RegExpSupport/testData/RETest.xml
java/java-analysis-impl/src/com/intellij/refactoring/util/duplicates/DuplicatesFinder.java
java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses.java [new file with mode: 0644]
java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses_after.java [new file with mode: 0644]
java/java-tests/testSrc/com/intellij/refactoring/ExtractMethodTest.java
plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/controlflow/IfStatementWithIdenticalBranchesInspection.java
plugins/InspectionGadgets/test/com/siyeh/igtest/controlflow/if_statement_with_identical_branches/IfStatementWithIdenticalBranches.java

index 7aa0738945220579f46b1f2345f33fb665a3ce4e..d9a06ed1fb33fabc9e282f0b57df8e7bf13804d7 100644 (file)
@@ -103,6 +103,10 @@ public class RegExpParseTest extends BaseParseTestCase {
     return super.getTestDataPath() + "/gen/";
   }
 
+  public void testOptions() throws Exception {
+    doTest("options/");
+  }
+
   public void testSimple() throws Exception {
     doTest("simple/");
   }
index 3e81b2ec2e897edb79b503c1cdac9d45e573eabd..12ec11dbd6e312f1bd544dc6151293f0e5dacaad 100644 (file)
     </test>
   </regressions>
 
+  <options>
+    <test host="com.intellij.psi.impl.JavaRegExpHost">
+      <pattern><![CDATA[(?i<error descr="Unknown inline option flag">Z</error>m)abc]]></pattern>
+      <expected>OK</expected>
+    </test>
+    <test host="com.intellij.psi.impl.JavaRegExpHost">
+      <pattern>(?idmsuxU)nice</pattern>
+      <expected>OK</expected>
+    </test>
+    <test host="com.intellij.psi.impl.JavaRegExpHost">
+      <pattern verify="false">(?idm-suxU)one(?suxU-idm)two</pattern>
+      <expected>OK</expected>
+    </test>
+  </options>
+
   <test>
     <pattern>abc)</pattern>
     <expected>ERR</expected>
index 99d7bdeb25fae008fbc107cc83cd8c073b71a6f7..c5ab2a7a67e7cb07b3f32cd55e883ee515a75815 100644 (file)
@@ -182,7 +182,7 @@ public class DuplicatesFinder {
       if (sibling == null) return null;
       if (!canBeEquivalent(element, sibling)) return null;
       candidates.add(sibling);
-      sibling = PsiTreeUtil.skipSiblingsForward(sibling, PsiWhiteSpace.class, PsiComment.class);
+      sibling = PsiTreeUtil.skipSiblingsForward(sibling, PsiWhiteSpace.class, PsiComment.class, PsiEmptyStatement.class);
     }
     LOG.assertTrue(myPattern.length == candidates.size());
     if (myPattern.length == 1 && myPattern[0] instanceof PsiExpression) {
@@ -340,7 +340,7 @@ public class DuplicatesFinder {
     }
 
     if (pattern instanceof PsiAssignmentExpression) {
-      final PsiExpression lExpression = ((PsiAssignmentExpression)pattern).getLExpression();
+      final PsiExpression lExpression = PsiUtil.skipParenthesizedExprDown(((PsiAssignmentExpression)pattern).getLExpression());
       if (lExpression.getType() instanceof PsiPrimitiveType &&
           lExpression instanceof PsiReferenceExpression &&
           ((PsiReferenceExpression)lExpression).resolve() instanceof PsiParameter) {
@@ -541,9 +541,11 @@ public class DuplicatesFinder {
     return true;
   }
 
-  private static boolean checkParameterModification(final PsiExpression expression,
+  private static boolean checkParameterModification(PsiExpression expression,
                                                     final IElementType sign,
                                                     PsiExpression candidate) {
+    expression = PsiUtil.skipParenthesizedExprDown(expression);
+    candidate = PsiUtil.skipParenthesizedExprDown(candidate);
     if (expression instanceof PsiReferenceExpression && ((PsiReferenceExpression)expression).resolve() instanceof PsiParameter &&
         (sign.equals(JavaTokenType.MINUSMINUS)|| sign.equals(JavaTokenType.PLUSPLUS))) {
       if (candidate instanceof PsiReferenceExpression && ((PsiReferenceExpression)candidate).resolve() instanceof PsiParameter) {
@@ -597,7 +599,7 @@ public class DuplicatesFinder {
       return match.registerReturnValue(new VariableReturnValue(variable));
     }
     else if (candidate instanceof PsiReturnStatement) {
-      final PsiExpression returnValue = ((PsiReturnStatement)candidate).getReturnValue();
+      final PsiExpression returnValue = PsiUtil.skipParenthesizedExprDown(((PsiReturnStatement)candidate).getReturnValue());
       if (myMultipleExitPoints) {
         return match.registerReturnValue(new ConditionalReturnStatementValue(returnValue));
       }
@@ -607,7 +609,7 @@ public class DuplicatesFinder {
         if (classOrLambda == null || !PsiTreeUtil.isAncestor(commonParent, classOrLambda, false)) {
           if (returnValue != null && !match.registerReturnValue(ReturnStatementReturnValue.INSTANCE)) return false; //do not register return value for return; statement
         }
-        return matchPattern(patternReturnStatement.getReturnValue(), returnValue, candidates, match);
+        return matchPattern(PsiUtil.skipParenthesizedExprDown(patternReturnStatement.getReturnValue()), returnValue, candidates, match);
       }
     }
     else return false;
@@ -654,20 +656,27 @@ public class DuplicatesFinder {
     return false;
   }
 
-  private static PsiElement[] getFilteredChildren(PsiElement element1) {
+  public static PsiElement[] getFilteredChildren(PsiElement element1) {
     PsiElement[] children1 = element1.getChildren();
     ArrayList<PsiElement> array = new ArrayList<PsiElement>();
     for (PsiElement child : children1) {
-      if (!(child instanceof PsiWhiteSpace) && !(child instanceof PsiComment)) {
+      if (!(child instanceof PsiWhiteSpace) && !(child instanceof PsiComment) && !(child instanceof PsiEmptyStatement)) {
         if (child instanceof PsiBlockStatement) {
-          Collections.addAll(array, getFilteredChildren(child));
-          continue;
-        } else if (child instanceof PsiCodeBlock) {
+          child = ((PsiBlockStatement)child).getCodeBlock();
+        }
+        if (child instanceof PsiCodeBlock) {
           final PsiStatement[] statements = ((PsiCodeBlock)child).getStatements();
-          if (statements.length == 1) {
-            array.add(statements[0]);
-            continue;
+          for (PsiStatement statement : statements) {
+            if (statement instanceof PsiBlockStatement) {
+              Collections.addAll(array, getFilteredChildren(statement));
+            } else if (!(statement instanceof PsiEmptyStatement)) {
+              array.add(statement);
+            }
           }
+          continue;
+        } else if (child instanceof PsiParenthesizedExpression) {
+          array.add(PsiUtil.skipParenthesizedExprDown((PsiParenthesizedExpression)child));
+          continue;
         }
         array.add(child);
       }
diff --git a/java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses.java b/java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses.java
new file mode 100644 (file)
index 0000000..56ecb9c
--- /dev/null
@@ -0,0 +1,15 @@
+class Test {
+
+  public void method1()
+  {
+    System.out.println(((1)));
+    ;;
+    System.out.println(2);
+  }
+
+  public void method2()
+  {
+    <selection>System.out.println(1);
+    System.out.println(2);</selection>
+  }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses_after.java b/java/java-tests/testData/refactoring/extractMethod/CodeDuplicatesWithEmptyStatementsBlocksParentheses_after.java
new file mode 100644 (file)
index 0000000..423e6da
--- /dev/null
@@ -0,0 +1,17 @@
+class Test {
+
+  public void method1()
+  {
+      newMethod();
+  }
+
+  public void method2()
+  {
+      newMethod();
+  }
+
+    private void newMethod() {
+        System.out.println(1);
+        System.out.println(2);
+    }
+}
\ No newline at end of file
index c3c5f39dc7468b74f02dda75b2893469d22a5a8d..4578edfe355eebf903d65155080bb9aaed95932f 100644 (file)
@@ -273,6 +273,10 @@ public class ExtractMethodTest extends LightCodeInsightTestCase {
     doDuplicatesTest();
   }
 
+  public void testCodeDuplicatesWithEmptyStatementsBlocksParentheses() throws Exception {
+    doDuplicatesTest();
+  }
+
   public void testCodeDuplicatesWithMultExitPoints() throws Exception {
     doDuplicatesTest();
   }
index d41d72387a1a11a014f5b4d5dc7dc06f193807ea..7cd2b3a8b037c4d5f762dfc50fa5d328ed63386e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
+ * Copyright 2003-2016 Dave Griffith, Bas Leijdekkers
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -163,22 +163,28 @@ public class IfStatementWithIdenticalBranchesInspection
 
   private static class IfStatementWithIdenticalBranchesVisitor extends BaseInspectionVisitor {
 
+    private static PsiStatement unwrap(PsiStatement statement) {
+      if (statement == null) {
+        return null;
+      }
+      final PsiElement[] children = DuplicatesFinder.getFilteredChildren(statement);
+      if (children.length == 1 && children[0] instanceof PsiStatement) {
+        return (PsiStatement) children[0];
+      }
+      return statement;
+    }
+
     @Override
     public void visitIfStatement(@NotNull PsiIfStatement ifStatement) {
       super.visitIfStatement(ifStatement);
-      final PsiStatement elseBranch = ifStatement.getElseBranch();
-      final PsiStatement thenBranch = ifStatement.getThenBranch();
+      final PsiStatement elseBranch = unwrap(ifStatement.getElseBranch());
+      final PsiStatement thenBranch = unwrap(ifStatement.getThenBranch());
       if (thenBranch == null) {
         return;
       }
       final Project project = ifStatement.getProject();
-      final InputVariables inputVariables =
-        new InputVariables(Collections.<PsiVariable>emptyList(),
-                           project, new LocalSearchScope(thenBranch), false);
-      final DuplicatesFinder finder =
-        new DuplicatesFinder(new PsiElement[]{thenBranch},
-                             inputVariables, null,
-                             Collections.<PsiVariable>emptyList());
+      final InputVariables inputVariables = new InputVariables(Collections.emptyList(), project, new LocalSearchScope(thenBranch), false);
+      final DuplicatesFinder finder = new DuplicatesFinder(new PsiElement[]{thenBranch}, inputVariables, null, Collections.emptyList());
       if (elseBranch instanceof PsiIfStatement) {
         final PsiIfStatement statement = (PsiIfStatement)elseBranch;
         final PsiStatement branch = statement.getThenBranch();
index fef2e0f1f6ff9202952448f57a5392649868da9d..6ba241705081e4e5050e7da95519c1e9df46c30b 100644 (file)
@@ -173,4 +173,15 @@ class NotADup {
     }
     System.out.println("j = " + j);
   }
+
+  void n(int i) {
+    <warning descr="'if' statement with identical branches">if</warning> (i == 0) {
+      System.out.println(((i)));
+      ;
+      ;
+      {
+      }
+    }
+    else System.out.println(i);
+  }
 }