From 47ed1338f2157e3e145eff1021d97c3528b1b1d9 Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Thu, 26 Jun 2014 19:26:02 +0200 Subject: [PATCH] IDEA-126624 (Narrow scope of variable fix generating wrong code with some for loops) --- .../src/com/siyeh/ig/dataflow/ScopeUtils.java | 5 +++-- .../ig/dataflow/TooBroadScopeInspection.java | 16 ++++++++++++++-- .../too_broad_scope/ForStatement3.after.java | 8 ++++++++ .../dataflow/too_broad_scope/ForStatement3.java | 9 +++++++++ .../dataflow/TooBroadScopeInspectionFixTest.java | 3 ++- 5 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/ScopeUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/ScopeUtils.java index 368f1a13d773..cd61a938f8c1 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/ScopeUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/dataflow/ScopeUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2012 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -17,6 +17,7 @@ package com.siyeh.ig.dataflow; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; +import com.siyeh.ig.psiutils.ParenthesesUtils; import com.siyeh.ig.psiutils.PsiElementOrderComparator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -86,7 +87,7 @@ class ScopeUtils { final PsiExpression expression = statement.getExpression(); if (expression instanceof PsiAssignmentExpression) { final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression; - final PsiExpression lExpression = assignmentExpression.getLExpression(); + final PsiExpression lExpression = ParenthesesUtils.stripParentheses(assignmentExpression.getLExpression()); if (!lExpression.equals(referenceElement)) { commonParent = PsiTreeUtil.getParentOfType(commonParent, PsiCodeBlock.class); } diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/TooBroadScopeInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/TooBroadScopeInspection.java index b4a66996769e..0bba9c356c50 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/TooBroadScopeInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/dataflow/TooBroadScopeInspection.java @@ -1,5 +1,5 @@ /* - * Copyright 2003-2013 Dave Griffith, Bas Leijdekkers + * Copyright 2003-2014 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. @@ -96,11 +96,23 @@ public class TooBroadScopeInspection extends TooBroadScopeInspectionBase { PsiDeclarationStatement newDeclaration; if (commonParent instanceof PsiForStatement) { final PsiForStatement forStatement = (PsiForStatement)commonParent; - newDeclaration = createNewDeclaration(variable, initializer); final PsiStatement initialization = forStatement.getInitialization(); if (initialization == null) { return; } + if (initialization instanceof PsiExpressionStatement) { + final PsiExpressionStatement expressionStatement = (PsiExpressionStatement)initialization; + final PsiExpression expression = expressionStatement.getExpression(); + if (!(expression instanceof PsiAssignmentExpression)) { + return; + } + final PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression)expression; + final PsiExpression rhs = assignmentExpression.getRExpression(); + newDeclaration = createNewDeclaration(variable, rhs); + } + else { + newDeclaration = createNewDeclaration(variable, initializer); + } newDeclaration = (PsiDeclarationStatement)initialization.replace(newDeclaration); } else if (firstReferenceScope.equals(commonParent)) { newDeclaration = moveDeclarationToLocation(variable, referenceElement); diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.after.java new file mode 100644 index 000000000000..c0f859235f39 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.after.java @@ -0,0 +1,8 @@ +package com.siyeh.igfixes.dataflow.too_broad_scope; + +public class ForStatement3 { + void m() { + for (int i = 0; i < 10; i++) { + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.java new file mode 100644 index 000000000000..21b30a72e278 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/dataflow/too_broad_scope/ForStatement3.java @@ -0,0 +1,9 @@ +package com.siyeh.igfixes.dataflow.too_broad_scope; + +public class ForStatement3 { + void m() { + int i; + for ((i) = 0; i < 10; i++) { + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/dataflow/TooBroadScopeInspectionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/dataflow/TooBroadScopeInspectionFixTest.java index 72f2dc39d7e6..27ca0a41387f 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/dataflow/TooBroadScopeInspectionFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/dataflow/TooBroadScopeInspectionFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,4 +33,5 @@ public class TooBroadScopeInspectionFixTest extends IGQuickFixesTestCase { public void testForStatement() { doTest(InspectionGadgetsBundle.message("too.broad.scope.narrow.quickfix", "i")); } public void testForStatement2() { doTest(InspectionGadgetsBundle.message("too.broad.scope.narrow.quickfix", "i")); } + public void testForStatement3() { doTest(InspectionGadgetsBundle.message("too.broad.scope.narrow.quickfix", "i")); } }