IDEA-126624 (Narrow scope of variable fix generating wrong code with some for loops)

This commit is contained in:
Bas Leijdekkers
2014-06-26 19:26:29 +02:00
parent 85d2a91c4a
commit 47ed1338f2
5 changed files with 36 additions and 5 deletions
@@ -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);
}
@@ -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);
@@ -0,0 +1,8 @@
package com.siyeh.igfixes.dataflow.too_broad_scope;
public class ForStatement3 {
void m() {
for (int i = 0; i < 10; i++) {
}
}
}
@@ -0,0 +1,9 @@
package com.siyeh.igfixes.dataflow.too_broad_scope;
public class ForStatement3 {
void m() {
int <caret>i;
for ((i) = 0; i < 10; i++) {
}
}
}
@@ -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")); }
}