[EA-259285] CharTable not found in: PsiCodeBlock

RedundantThrowsDeclarationInspection used to try to remove or replace try statements regardless if their corresponding PSI elements are still valid. Since PSI nodes were being removed or replace in the loop some of them got to become invalid and when BlockUtils#unwrapTryBlock was invoked on an invalid PSI element it threw the AssertionError. This patch adds checks if the PSI element that is being processed is still valid and if it is not, then stop processing such PSI elements.

GitOrigin-RevId: 6d071ef7888f46bbda10b6d99853ef338055f3a6
This commit is contained in:
Nikita Eshkeev
2021-03-22 21:37:33 +00:00
committed by intellij-monorepo-bot
parent 76529a0d5c
commit af4bd9a9cd
4 changed files with 40 additions and 8 deletions
@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInspection.unneededThrows;
import com.intellij.analysis.AnalysisScope;
@@ -263,21 +263,23 @@ public final class RedundantThrowsDeclarationInspection extends GlobalJavaBatchI
for (Map.Entry<PsiElement, PsiElement> mapping : mappings.entrySet()) {
final PsiElement from = mapping.getKey();
final PsiElement to = mapping.getValue();
if (!from.isValid()) continue;
if (to == null) {
new CommentTracker().deleteAndRestoreComments(from);
}
else {
else if (to.isValid()) {
final PsiElement element = new CommentTracker().replaceAndRestoreComments(from, to);
instance.shortenClassReferences(element);
}
}
for (PsiTryStatement tryStatement : tryStatements) {
if (tryStatement.getCatchSections().length == 0 &&
tryStatement.getFinallyBlock() == null &&
tryStatement.getResourceList() == null) {
BlockUtils.unwrapTryBlock(tryStatement);
}
if (!tryStatement.isValid()) continue;
if (tryStatement.getCatchSections().length != 0) continue;
if (tryStatement.getFinallyBlock() != null) continue;
if (tryStatement.getResourceList() != null) continue;
BlockUtils.unwrapTryBlock(tryStatement);
}
});
}
@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.codeInsight;
import com.intellij.openapi.project.Project;
@@ -112,6 +112,8 @@ public final class BlockUtils {
}
public static void unwrapTryBlock(PsiTryStatement tryStatement) {
assert tryStatement.isValid() : "The tryStatement PSI element must be valid";
PsiCodeBlock tryBlock = tryStatement.getTryBlock();
if (tryBlock == null) {
return;
@@ -0,0 +1,10 @@
// "FileNotFoundException" "true"
import java.io.FileNotFoundException;
class Main {
public void f() {}
{
f();
}
}
@@ -0,0 +1,18 @@
// "FileNotFoundException" "true"
import java.io.FileNotFoundException;
class Main {
public void f() throws <caret>FileNotFoundException {}
{
try {
f();
} catch (FileNotFoundException e1) {
try {
f();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
}
}
}