IG: make ExpressionUtils.resolveLocalVariable() skip parentheses

This commit is contained in:
Bas Leijdekkers
2019-03-08 11:32:41 +01:00
parent afb39182bb
commit 27c17db32b
12 changed files with 19 additions and 48 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection;
import com.intellij.openapi.project.Project;
@@ -138,7 +138,7 @@ public class ReadWriteStringCanBeUsedInspection extends AbstractBaseJavaLocalIns
if (!isUtf8Charset(charsetExpression)) {
argumentList.addAfter(ct.markUnchanged(charsetExpression), bytesArg);
}
PsiLocalVariable variable = ExpressionUtils.resolveLocalVariable(PsiUtil.skipParenthesizedExprDown(bytesArg));
PsiLocalVariable variable = ExpressionUtils.resolveLocalVariable(bytesArg);
ct.replaceAndRestoreComments(bytesArg, stringExpression);
if (variable != null) {
ct = new CommentTracker();
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection;
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
@@ -90,7 +90,7 @@ public class WrapWithMutableCollectionFix implements LocalQuickFix {
anchor = ((PsiReferenceExpression)anchor.getParent()).getQualifierExpression();
}
if (!(anchor instanceof PsiExpression)) return null;
return ExpressionUtils.resolveLocalVariable(PsiUtil.skipParenthesizedExprDown((PsiExpression)anchor));
return ExpressionUtils.resolveLocalVariable((PsiExpression)anchor);
}
@Contract("null -> null")
@@ -982,14 +982,14 @@ public class StreamApiMigrationInspection extends AbstractBaseJavaLocalInspectio
if (!JavaTokenType.NE.equals(binOp.getOperationTokenType())) return null;
PsiExpression operand = ExpressionUtils.getValueComparedWithNull(binOp);
if (operand == null) return null;
PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(PsiUtil.skipParenthesizedExprDown(operand));
PsiAssignmentExpression assignment = ExpressionUtils.getAssignment(operand);
if (assignment == null) return null;
PsiMethodCallExpression readerCall =
tryCast(PsiUtil.skipParenthesizedExprDown(assignment.getRExpression()), PsiMethodCallExpression.class);
if (!BUFFERED_READER_READ_LINE.test(readerCall)) return null;
PsiExpression reader = readerCall.getMethodExpression().getQualifierExpression();
PsiLocalVariable lineVar = ExpressionUtils.resolveLocalVariable(PsiUtil.skipParenthesizedExprDown(assignment.getLExpression()));
PsiLocalVariable lineVar = ExpressionUtils.resolveLocalVariable(assignment.getLExpression());
if (lineVar == null) return null;
if (ReferencesSearch.search(lineVar).anyMatch(ref -> !PsiTreeUtil.isAncestor(loopStatement, ref.getElement(), true))) {
return null;
@@ -1,8 +1,8 @@
// "Unroll loop" "true"
class Test {
void test() {
boolean steps = {true, false};
fo<caret>r(boolean step : steps) {
boolean[] steps = {true, false};
fo<caret>r(boolean step : (steps)) {
foo(step);
unresolved(!step);
}
@@ -2,7 +2,7 @@ class Foo {
void test() {
StringBuilder sb;
<caret>sb = new StringBuilder();
sb.append("foo");
(sb).append("foo");
sb.append("bar");
sb.append("baz");
}
@@ -2,7 +2,7 @@
class Test {
void test() {
Object x = " hello ";
System.out.println(((Str<caret>ing)x).trim());
System.out.println(((String)x).substring(1));
System.out.println(((Str<caret>ing)(x)).trim());
System.out.println(((String)(x)).substring(1));
}
}
@@ -8,6 +8,6 @@ import java.util.Collection;
class C {
void m() {
final Collection<String> strings = new ArrayList<String>();
strings.<caret>addAll(new HashSet<String>());
(strings).<caret>addAll(new HashSet<String>());
}
}
@@ -1,18 +1,4 @@
/*
* Copyright 2008-2017 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.performance;
import com.intellij.codeInspection.CommonQuickFixBundle;
@@ -373,7 +359,6 @@ public class KeySetIterationMayUseEntrySetInspection extends BaseInspection {
@Nullable
@Contract("null -> null")
private static PsiExpression getIteratedExpression(PsiExpression iteratedValue) {
iteratedValue = PsiUtil.skipParenthesizedExprDown(iteratedValue);
PsiLocalVariable variable = ExpressionUtils.resolveLocalVariable(iteratedValue);
if (variable != null) {
final PsiMethod containingMethod = PsiTreeUtil.getParentOfType(variable, PsiMethod.class);
@@ -1,18 +1,4 @@
/*
* Copyright 2005-2019 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.siyeh.ig.psiutils;
import com.intellij.codeInsight.AnnotationUtil;
@@ -1034,6 +1020,7 @@ public class ExpressionUtils {
@Contract(value = "null -> null")
@Nullable
public static PsiLocalVariable resolveLocalVariable(@Nullable PsiExpression expression) {
expression = ParenthesesUtils.stripParentheses(expression);
PsiReferenceExpression referenceExpression = tryCast(expression, PsiReferenceExpression.class);
if(referenceExpression == null) return null;
return tryCast(referenceExpression.resolve(), PsiLocalVariable.class);
@@ -1,4 +1,4 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection.redundantCast;
import com.intellij.codeInspection.*;
@@ -28,8 +28,7 @@ public class CastCanBeRemovedNarrowingVariableTypeInspection extends AbstractBas
if (castTypeElement == null || castTypeElement.getAnnotations().length > 0) return;
PsiType castType = cast.getType();
if (!(castType instanceof PsiClassType) || ((PsiClassType)castType).isRaw()) return;
PsiReferenceExpression ref = tryCast(cast.getOperand(), PsiReferenceExpression.class);
PsiLocalVariable variable = ExpressionUtils.resolveLocalVariable(ref);
PsiLocalVariable variable = ExpressionUtils.resolveLocalVariable(cast.getOperand());
if (variable == null) return;
PsiTypeElement variableTypeElement = variable.getTypeElement();
if (variableTypeElement.isInferredType() || variableTypeElement.getAnnotations().length > 0) return;
@@ -4,7 +4,7 @@ import java.util.Set;
class Reference {
void m(Map<String, String> map) {
for (String s : map.values()) {
for (String s : (map.values())) {
System.out.println(s);
}
}
@@ -5,7 +5,7 @@ class Reference {
void m(Map<String, String> map) {
Set<String> keys = (map.keySet());
for (String key : <caret>keys) {
for (String key : (<caret>keys)) {
System.out.println(map.get(key));
}
}