You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Intellij Plugin breaks method inlining and variable extraction from inside if condition #1101
Not only does it fail (without reporting it), but it adds an additional if (true):
publicstaticvoidmain(String[] args){
varpojo = newMyPojo();
if (pojo != null && !isEmpty2(pojo.getCollection())) {
if (true) {
System.out.println("empty");
}
}
}
with the plugin disabled, it works:
publicstaticvoidmain(String[] args){
varpojo = newMyPojo();
if (pojo != null) {
Collection<?> c = pojo.getCollection();
if (!isEmpty(c)) {
System.out.println("empty");
}
}
}
(not the ideal result but at least it does not fail)
In addition, when I do that on my actual project I get an error notification from the plugin and the formatting gets broken:
(I did not try to reproduce this with an MRE, I guess it is a side effect of the first issue)
I just noticed that the same issue occurs if you simply try to extract a local variable from within the if condition instead of performing an inlining. IntelliJ would normally split the if in a similar way.
I guess this is what IntelliJ tries to do under the hood for the inlining (as shown when the plugin is disabled), and the at the source of the problem.
changed the title [-]Intellij Plugin breaks method inlining inside if condition[/-][+]Intellij Plugin breaks method inlining and variable extraction from inside if condition[/+]on May 30, 2024
After formatting, CodeStyleManager.getInstance(project).reformat() is returning an instance of PsiKeywordImpl instead of the expected PsiIfStatement. Not sure why this is happening. I don't see where we return any PSI elements directly, we only return a string. So, something about how that string is being parsed into PSI elements isn't working as expected.
Because the exception is thrown, the remaining refactor logic from IntelliJ doesn't seem to be running.
IntelliJ developer is here. We will work around the problem on the IntelliJ side (ClassCastException in CodeBlockSurrounder). However, the root cause is not identified, and similar problem may still happen in other contexts. Please track IDEA-340109 for details.
Activity
DidierLoiseau commentedon May 30, 2024
I just noticed that the same issue occurs if you simply try to extract a local variable from within the
ifcondition instead of performing an inlining. IntelliJ would normally split theifin a similar way.I guess this is what IntelliJ tries to do under the hood for the inlining (as shown when the plugin is disabled), and the at the source of the problem.
[-]Intellij Plugin breaks method inlining inside if condition[/-][+]Intellij Plugin breaks method inlining and variable extraction from inside if condition[/+]nrayburn-tech commentedon Aug 23, 2024
For the first example, the code fails at the line here https://github.com/JetBrains/intellij-community/blob/cd9bfbd98a7dca730fbc469156ce1ed30364afba/java/java-analysis-impl/src/com/siyeh/ig/psiutils/CodeBlockSurrounder.java#L884 due to a ClassCastException.
After formatting,
CodeStyleManager.getInstance(project).reformat()is returning an instance ofPsiKeywordImplinstead of the expectedPsiIfStatement. Not sure why this is happening. I don't see where we return any PSI elements directly, we only return a string. So, something about how that string is being parsed into PSI elements isn't working as expected.Because the exception is thrown, the remaining refactor logic from IntelliJ doesn't seem to be running.
amaembo commentedon Oct 17, 2024
IntelliJ developer is here. We will work around the problem on the IntelliJ side (ClassCastException in CodeBlockSurrounder). However, the root cause is not identified, and similar problem may still happen in other contexts. Please track IDEA-340109 for details.