mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Use VariableAccessUtils.getVariableReferences where possible (IDEA-CR-53442)
GitOrigin-RevId: daa9a181332e4d9ff69275897c4c401def2804b8
This commit is contained in:
committed by
intellij-monorepo-bot
parent
60e18c3976
commit
2dfcb76473
@@ -30,6 +30,7 @@ import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.usageView.UsageInfo;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.Query;
|
||||
import com.siyeh.ig.psiutils.VariableAccessUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
@@ -37,7 +38,6 @@ import org.jetbrains.annotations.TestOnly;
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
public class NullityInferrer {
|
||||
@@ -574,25 +574,18 @@ public class NullityInferrer {
|
||||
final PsiMethod method = (PsiMethod)grandParent;
|
||||
if (method.getBody() != null) {
|
||||
|
||||
List<PsiReference> all = new ArrayList<>(ReferencesSearch.search(parameter, new LocalSearchScope(method)).findAll());
|
||||
all.sort(Comparator.comparingInt(ref->ref.getElement().getTextOffset()));
|
||||
|
||||
for (PsiReference reference : all) {
|
||||
final PsiElement place = reference.getElement();
|
||||
if (place instanceof PsiReferenceExpression) {
|
||||
final PsiReferenceExpression expr = (PsiReferenceExpression)place;
|
||||
final PsiElement parent = PsiTreeUtil.skipParentsOfType(expr, PsiParenthesizedExpression.class, PsiTypeCastExpression.class);
|
||||
if (processParameter(parameter, expr, parent)) return;
|
||||
if (isNotNull(method)) {
|
||||
PsiElement toReturn = parent;
|
||||
if (parent instanceof PsiConditionalExpression &&
|
||||
((PsiConditionalExpression)parent).getCondition() != expr) { //todo check conditional operations
|
||||
toReturn = parent.getParent();
|
||||
}
|
||||
if (toReturn instanceof PsiReturnStatement) {
|
||||
registerNotNullAnnotation(parameter);
|
||||
return;
|
||||
}
|
||||
for (PsiReferenceExpression expr : VariableAccessUtils.getVariableReferences(parameter, method)) {
|
||||
final PsiElement parent = PsiTreeUtil.skipParentsOfType(expr, PsiParenthesizedExpression.class, PsiTypeCastExpression.class);
|
||||
if (processParameter(parameter, expr, parent)) return;
|
||||
if (isNotNull(method)) {
|
||||
PsiElement toReturn = parent;
|
||||
if (parent instanceof PsiConditionalExpression &&
|
||||
((PsiConditionalExpression)parent).getCondition() != expr) { //todo check conditional operations
|
||||
toReturn = parent.getParent();
|
||||
}
|
||||
if (toReturn instanceof PsiReturnStatement) {
|
||||
registerNotNullAnnotation(parameter);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-6
@@ -7,19 +7,17 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.controlFlow.*;
|
||||
import com.intellij.psi.search.LocalSearchScope;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.InlineUtil;
|
||||
import com.intellij.refactoring.util.RefactoringUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.Query;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.HighlightUtils;
|
||||
import com.siyeh.ig.psiutils.VariableAccessUtils;
|
||||
import gnu.trove.THashMap;
|
||||
import gnu.trove.THashSet;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
@@ -208,9 +206,7 @@ public class ReturnSeparatedFromComputationInspection extends AbstractBaseJavaLo
|
||||
return;
|
||||
}
|
||||
|
||||
Query<PsiReference> query = ReferencesSearch.search(context.returnedVariable, new LocalSearchScope(context.variableScope));
|
||||
List<PsiReference> usages = new ArrayList<>(query.findAll());
|
||||
usages.sort(Comparator.comparingInt(ref -> ref.getElement().getTextOffset()));
|
||||
List<PsiReferenceExpression> usages = VariableAccessUtils.getVariableReferences(context.returnedVariable, context.variableScope);
|
||||
for (PsiReference usage : usages) {
|
||||
PsiElement parent = PsiTreeUtil.skipParentsOfType(usage.getElement(),
|
||||
PsiParenthesizedExpression.class, PsiTypeCastExpression.class);
|
||||
|
||||
+6
-13
@@ -16,17 +16,12 @@ import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.util.LambdaRefactoringUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.ig.callMatcher.CallMatcher;
|
||||
import com.siyeh.ig.psiutils.CommentTracker;
|
||||
import com.siyeh.ig.psiutils.EquivalenceChecker;
|
||||
import com.siyeh.ig.psiutils.ExpressionUtils;
|
||||
import com.siyeh.ig.psiutils.MethodCallUtils;
|
||||
import com.siyeh.ig.psiutils.*;
|
||||
import org.jetbrains.annotations.Nls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -152,10 +147,9 @@ public class Java8MapForEachInspection extends AbstractBaseJavaLocalInspectionTo
|
||||
PsiType entryType = entryParameter.getType();
|
||||
ParameterCandidate key = new ParameterCandidate(entryType, true);
|
||||
ParameterCandidate value = new ParameterCandidate(entryType, false);
|
||||
List<PsiReference> references = new ArrayList<>(ReferencesSearch.search(entryParameter).findAll());
|
||||
references.sort(Comparator.comparingInt(ref->ref.getElement().getTextOffset()));
|
||||
for (PsiReference ref : references) {
|
||||
PsiMethodCallExpression entryCall = ExpressionUtils.getCallForQualifier(ObjectUtils.tryCast(ref.getElement(), PsiExpression.class));
|
||||
List<PsiReferenceExpression> references = VariableAccessUtils.getVariableReferences(entryParameter, body);
|
||||
for (PsiReferenceExpression ref : references) {
|
||||
PsiMethodCallExpression entryCall = ExpressionUtils.getCallForQualifier(ref);
|
||||
if (ENTRY_GETTER.test(entryCall)) {
|
||||
ParameterCandidate.select(entryCall, key, value).accept(entryCall);
|
||||
}
|
||||
@@ -163,9 +157,8 @@ public class Java8MapForEachInspection extends AbstractBaseJavaLocalInspectionTo
|
||||
key.createName(body, ct);
|
||||
value.createName(body, ct);
|
||||
PsiElementFactory factory = JavaPsiFacade.getElementFactory(entrySetCall.getProject());
|
||||
for (PsiReference ref : references) {
|
||||
PsiExpression expression = ObjectUtils.tryCast(ref.getElement(), PsiExpression.class);
|
||||
if (expression == null || !expression.isValid()) continue;
|
||||
for (PsiExpression expression : references) {
|
||||
if (!expression.isValid()) continue;
|
||||
PsiMethodCallExpression entryCall = ExpressionUtils.getCallForQualifier(expression);
|
||||
if (ENTRY_GETTER.test(entryCall)) {
|
||||
ct.replace(entryCall, factory.createIdentifier(ParameterCandidate.select(entryCall, key, value).myName));
|
||||
|
||||
+6
-4
@@ -10,7 +10,6 @@ import com.intellij.openapi.project.Project;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.search.searches.ReferencesSearch;
|
||||
import com.intellij.psi.util.InheritanceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiTypesUtil;
|
||||
@@ -25,7 +24,10 @@ import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import static com.intellij.codeInspection.streamMigration.StreamApiMigrationInspection.isCallOf;
|
||||
@@ -181,7 +183,8 @@ class CollectMigration extends BaseStreamApiMigration {
|
||||
StreamEx<? extends PsiExpression> targetReferences() {
|
||||
if (myTargetVariable == null) return StreamEx.empty();
|
||||
List<PsiElement> usedElements = usedElements().toList();
|
||||
return StreamEx.of(ReferencesSearch.search(myTargetVariable).findAll()).select(PsiReferenceExpression.class)
|
||||
PsiElement block = PsiUtil.getVariableCodeBlock(myTargetVariable, null);
|
||||
return StreamEx.of(VariableAccessUtils.getVariableReferences(myTargetVariable, block))
|
||||
.filter(ref -> usedElements.stream().noneMatch(allowedUsage -> PsiTreeUtil.isAncestor(allowedUsage, ref, false)));
|
||||
}
|
||||
|
||||
@@ -804,7 +807,6 @@ class CollectMigration extends BaseStreamApiMigration {
|
||||
|
||||
List<? extends PsiExpression> usages = terminal.targetReferences().toList();
|
||||
if (usages.isEmpty()) return null;
|
||||
usages.sort(Comparator.comparingInt(ref->ref.getTextOffset()));
|
||||
PsiMethodCallExpression toArrayCandidate = StreamEx.of(usages)
|
||||
.map(usage -> ExpressionUtils.getCallForQualifier(tryCast(usage, PsiExpression.class)))
|
||||
.nonNull().findFirst().orElse(null);
|
||||
|
||||
+17
-22
@@ -34,7 +34,6 @@ import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.Query;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -391,40 +390,36 @@ public class StringConcatenationInLoopsInspection extends BaseInspection {
|
||||
PsiElement scope,
|
||||
CommentTracker ct,
|
||||
Predicate<? super PsiReferenceExpression> skip) {
|
||||
Query<PsiReference> query =
|
||||
scope == null ? ReferencesSearch.search(variable) : ReferencesSearch.search(variable, new LocalSearchScope(scope));
|
||||
List<PsiReference> refs = new ArrayList<>(query.findAll());
|
||||
refs.sort(Comparator.comparingInt(ref -> ref.getElement().getTextOffset()));
|
||||
if (scope == null) {
|
||||
scope = PsiUtil.getVariableCodeBlock(variable, null);
|
||||
}
|
||||
List<PsiReferenceExpression> refs = VariableAccessUtils.getVariableReferences(variable, scope);
|
||||
if (myNullSafe) {
|
||||
fillNullables(variable, refs);
|
||||
}
|
||||
for(PsiReference ref : refs) {
|
||||
PsiElement target = ref.getElement();
|
||||
if(target instanceof PsiReferenceExpression && target.isValid() && !skip.test((PsiReferenceExpression)target)) {
|
||||
replace(variable, builderVariable, (PsiReferenceExpression)target, ct);
|
||||
for(PsiReferenceExpression target : refs) {
|
||||
if(target.isValid() && !skip.test(target)) {
|
||||
replace(variable, builderVariable, target, ct);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void fillNullables(PsiVariable variable, Collection<PsiReference> refs) {
|
||||
private void fillNullables(PsiVariable variable, Collection<PsiReferenceExpression> refs) {
|
||||
if (myNullables instanceof HashSet) return; // already filled
|
||||
myNullables = new HashSet<>();
|
||||
PsiExpression initializer = variable.getInitializer();
|
||||
if (initializer != null && NullabilityUtil.getExpressionNullability(initializer, true) != Nullability.NOT_NULL) {
|
||||
myNullables.add(initializer);
|
||||
}
|
||||
for (PsiReference ref : refs) {
|
||||
if (ref instanceof PsiExpression) {
|
||||
PsiExpression refExpr = (PsiExpression)ref;
|
||||
if (NullabilityUtil.getExpressionNullability(refExpr, true) != Nullability.NOT_NULL) {
|
||||
myNullables.add(refExpr);
|
||||
}
|
||||
if(PsiUtil.isOnAssignmentLeftHand(refExpr)) {
|
||||
PsiExpression rExpr =
|
||||
ExpressionUtils.getAssignmentTo(PsiTreeUtil.getParentOfType(refExpr, PsiAssignmentExpression.class), variable);
|
||||
if (rExpr != null && NullabilityUtil.getExpressionNullability(rExpr, true) != Nullability.NOT_NULL) {
|
||||
myNullables.add(rExpr);
|
||||
}
|
||||
for (PsiReferenceExpression refExpr : refs) {
|
||||
if (NullabilityUtil.getExpressionNullability(refExpr, true) != Nullability.NOT_NULL) {
|
||||
myNullables.add(refExpr);
|
||||
}
|
||||
if(PsiUtil.isOnAssignmentLeftHand(refExpr)) {
|
||||
PsiExpression rExpr =
|
||||
ExpressionUtils.getAssignmentTo(PsiTreeUtil.getParentOfType(refExpr, PsiAssignmentExpression.class), variable);
|
||||
if (rExpr != null && NullabilityUtil.getExpressionNullability(rExpr, true) != Nullability.NOT_NULL) {
|
||||
myNullables.add(rExpr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user