mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
ConvertFieldToAtomicIntention: show on illegal references to non-effectively-final variables
A small step towards IDEA-177104 Suggestion to transform variable changed in lambda into one-element array is a bad practice
This commit is contained in:
+15
@@ -1,6 +1,7 @@
|
||||
package com.intellij.refactoring.typeMigration.intentions;
|
||||
|
||||
import com.intellij.codeInsight.FileModificationService;
|
||||
import com.intellij.codeInsight.daemon.impl.analysis.HighlightControlFlowUtil;
|
||||
import com.intellij.codeInsight.intention.LowPriorityAction;
|
||||
import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction;
|
||||
import com.intellij.lang.java.JavaLanguage;
|
||||
@@ -15,9 +16,11 @@ import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleSettings;
|
||||
import com.intellij.psi.impl.AllowedApiFilterExtension;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.refactoring.typeMigration.TypeMigrationVariableTypeFixProvider;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
@@ -87,6 +90,18 @@ public class ConvertFieldToAtomicIntention extends PsiElementBaseIntentionAction
|
||||
if (parent instanceof PsiLocalVariable || parent instanceof PsiField) {
|
||||
return (PsiVariable)parent;
|
||||
}
|
||||
if (parent instanceof PsiReferenceExpression) {
|
||||
// Display "Convert to atomic" on the illegal reference to non-effectively final local variable
|
||||
// as this could be a desired fix in such case
|
||||
PsiLocalVariable variable = ObjectUtils.tryCast(((PsiReferenceExpression)parent).resolve(), PsiLocalVariable.class);
|
||||
if (variable == null) return null;
|
||||
PsiElement scope = PsiTreeUtil.getParentOfType(variable, PsiMember.class, PsiLambdaExpression.class);
|
||||
if (scope != null &&
|
||||
scope != PsiTreeUtil.getParentOfType(parent, PsiMember.class, PsiLambdaExpression.class) &&
|
||||
!HighlightControlFlowUtil.isEffectivelyFinal(variable, scope, null)) {
|
||||
return variable;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user