'if without braces' inspection fix error
I'm getting errors trying to apply the 'if without braces' inspection fix, when there is an associated 'else' that also doesn't have braces:
boolean a = true; int x = 0; if (a) x = 1; else x = 2;
Putting the cursor on the 'if' and hitting Alt-Enter, Add Braces, results in a fatal 'Blame Inspection Gadgets' dialog with the following contents:
Incorrect statement "if(a){
x = 1;
}elsex = 2;".: Incorrect statement "if(a){
x = 1;
}elsex = 2;".
com.intellij.util.IncorrectOperationException: Incorrect statement "if(a){
x = 1;
}elsex = 2;".
at com.intellij.psi.impl.PsiJavaParserFacadeImpl.createStatementFromText(PsiJavaParserFacadeImpl.java:199)
at com.intellij.psi.impl.PsiElementFactoryImpl.createStatementFromText(PsiElementFactoryImpl.java:692)
at com.siyeh.ig.InspectionGadgetsFix.replaceStatement(InspectionGadgetsFix.java:139)
at com.siyeh.ig.style.ControlFlowStatementWithoutBracesInspection$ControlFlowStatementFix.doFix(ControlFlowStatementWithoutBracesInspection.java:93)
at com.siyeh.ig.InspectionGadgetsFix.applyFix(InspectionGadgetsFix.java:64)
at com.siyeh.ig.InspectionGadgetsFix.applyFix(InspectionGadgetsFix.java:38)
at com.intellij.codeInspection.ex.QuickFixWrapper.invoke(QuickFixWrapper.java:80)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$2.run(ShowIntentionActionsHandler.java:214)
at com.intellij.openapi.application.impl.ApplicationImpl.runWriteAction(ApplicationImpl.java:752)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler$3.run(ShowIntentionActionsHandler.java:227)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:110)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:86)
at com.intellij.openapi.command.impl.CommandProcessorImpl.executeCommand(CommandProcessorImpl.java:78)
at com.intellij.codeInsight.intention.impl.ShowIntentionActionsHandler.chooseActionAndInvoke(ShowIntentionActionsHandler.java:232)
at com.intellij.codeInsight.intention.impl.IntentionListStep$2$1.run(IntentionListStep.java:186)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:319)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at com.intellij.ide.IdeEventQueue.f(IdeEventQueue.java:608)
at com.intellij.ide.IdeEventQueue.b(IdeEventQueue.java:498)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
It looks as if the braces insertion for the first block loses the whitespace following the 'else', and so the next braces insertion is looking at an incorrect statement. How can something as basic as this pass the regression tests?
请先登录再写评论。
Yes, this problem got introduced by accident in the last EAP build and was fixed for the next one a couple of days ago. The tests clearly do not cover this specific area, but you are of course welcome to submit a patch.
Bas
Thanks for that Bas - I'll wait for the next EAP, it's no big deal. I was a bit surprised it wasn't picked up in testing because I'm pretty sure I recall a vaguely similar bug from many versions ago...
No matter, life goes on ;-)