From 810a23812fb79235a1a68fa32e1ae200b076891e Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Mon, 25 Jun 2018 12:41:56 +0300 Subject: [PATCH] disable surroundWith fix for var typed locals with non-denotable inferred types IDEA-CR-33936 --- .../beforeVarCantBeReplacedWithExplicitType.java | 11 +++++++++++ .../src/com/siyeh/ig/psiutils/ControlFlowUtils.java | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarCantBeReplacedWithExplicitType.java diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarCantBeReplacedWithExplicitType.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarCantBeReplacedWithExplicitType.java new file mode 100644 index 000000000000..5849c82e2b14 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/surroundWithTry/beforeVarCantBeReplacedWithExplicitType.java @@ -0,0 +1,11 @@ +// "Surround with try/catch" "false" +class Test { + public static void main(String[] args) { + var foo = new Test() { + int x = 2; + }; + System.out.println(foo.x); + } + + Test() throws Exception {} +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java index 8a769e71d7e0..c4fa131fff86 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ControlFlowUtils.java @@ -20,6 +20,7 @@ import com.intellij.psi.controlFlow.*; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.tree.IElementType; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.psi.util.PsiTypesUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.util.ArrayUtil; import com.intellij.util.ObjectUtils; @@ -948,7 +949,11 @@ public class ControlFlowUtils { if(parent instanceof PsiLocalVariable) { PsiElement grandParent = parent.getParent(); if(grandParent instanceof PsiDeclarationStatement && ((PsiDeclarationStatement)grandParent).getDeclaredElements().length == 1) { - return true; + PsiTypeElement typeElement = ((PsiLocalVariable)parent).getTypeElement(); + if (!typeElement.isInferredType() || + PsiTypesUtil.replaceWithExplicitType(((PsiLocalVariable)parent.copy()).getTypeElement()) != null) { + return true; + } } } if (parent instanceof PsiField) {