DeferFinalAssignmentFix should not be available if the previous assignment occurred in another member

E.g. in field initializer or in chained constructor: in this case defer does something strange and doesn't fix the problem

GitOrigin-RevId: bb50f7b0ccd847a61b7e00864706a2cb5ce86dcf
This commit is contained in:
Tagir Valeev
2020-01-10 13:41:14 +07:00
committed by intellij-monorepo-bot
parent c7557bba96
commit 1c910b0b32
2 changed files with 19 additions and 1 deletions

View File

@@ -505,6 +505,7 @@ public class HighlightControlFlowUtil {
break;
}
}
boolean canDefer = !inLoop;
if (!alreadyAssigned) {
if (!(variable instanceof PsiField)) return null;
@@ -555,6 +556,7 @@ public class HighlightControlFlowUtil {
}
}
}
canDefer = !alreadyAssigned;
}
if (alreadyAssigned) {
@@ -563,7 +565,7 @@ public class HighlightControlFlowUtil {
final HighlightInfo highlightInfo =
HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(expression).descriptionAndTooltip(description).create();
HighlightFixUtil.registerMakeNotFinalAction(variable, highlightInfo);
if (!inLoop) {
if (canDefer) {
QuickFixAction.registerQuickFixAction(highlightInfo, QUICK_FIX_FACTORY.createDeferFinalAssignmentFix(variable, expression));
}
return highlightInfo;

View File

@@ -0,0 +1,16 @@
// "Defer assignment to 'x' using temp variable" "false"
import java.io.*;
class a {
final int x;
a(int _x) {
x = _x;
}
a() {
this(0);
<caret>x = 1;
}
}