IOStreamConstructorInspection: do not suggest fix in cases when FileNotFoundException is already handled (IDEA-288738)

GitOrigin-RevId: f55d0d899196a9db2fc9dfc2ab01fc7c5edb51bf
This commit is contained in:
Artemiy Sartakov
2022-02-16 18:16:00 +07:00
committed by intellij-monorepo-bot
parent 0d90827381
commit a028df8220
5 changed files with 87 additions and 0 deletions

View File

@@ -21,6 +21,7 @@ import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.CommentTracker;
import com.siyeh.ig.psiutils.ExpectedTypeUtils;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.TypeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -44,6 +45,7 @@ public class IOStreamConstructorInspection extends AbstractBaseJavaLocalInspecti
StreamType streamType = constructorModel.myStreamType;
PsiType expectedType = ExpectedTypeUtils.findExpectedType(newExpression, false);
if (expectedType == null) return;
if (isFileNotFoundHandled(newExpression)) return;
boolean canUseBaseType = TypeConversionUtil.isAssignable(expectedType, streamType.baseType(newExpression));
if (!canUseBaseType) return;
boolean isInfoLevel = PsiUtil.isLanguageLevel10OrHigher(holder.getFile());
@@ -55,6 +57,23 @@ public class IOStreamConstructorInspection extends AbstractBaseJavaLocalInspecti
};
}
private static boolean isFileNotFoundHandled(@NotNull PsiExpression context) {
PsiClassType fileNotFoundType = TypeUtils.getType("java.io.FileNotFoundException", context);
PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(context, PsiTryStatement.class, true, PsiMethod.class, PsiLambdaExpression.class);
while (tryStatement != null) {
PsiCatchSection[] catchSections = tryStatement.getCatchSections();
for (PsiCatchSection catchSection : catchSections) {
PsiParameter catchParameter = catchSection.getParameter();
if (catchParameter != null && TypeConversionUtil.isAssignable(catchParameter.getType(), fileNotFoundType)) {
PsiClassType ioException = TypeUtils.getType("java.io.IOException", context);
return !TypeConversionUtil.isAssignable(catchParameter.getType(), ioException);
}
}
tryStatement = PsiTreeUtil.getParentOfType(tryStatement, PsiTryStatement.class, true, PsiMethod.class, PsiLambdaExpression.class);
}
return false;
}
private static @Nullable PsiExpression getOnlyArgument(@NotNull PsiCallExpression expression) {
PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) return null;

View File

@@ -0,0 +1,20 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
class Foo {
void test(String str) {
try {
if (str.length() < 3) throw new FileNotFoundException("e");
try (InputStream in = Files.newInputStream(Paths.get(str))) {
}
catch (IOException e) {
System.out.println("Don't know what happened");
}
}
catch (FileNotFoundException e) {
System.out.println("file not found exception");
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with 'Files.newInputStream'" "false"
import java.io.*;
class Foo {
void test(String str) {
try(InputStream in = new FileInputStream(s<caret>tr)) {
}
catch (FileNotFoundException e) {
System.out.println("file not found exception");
}
catch (IOException e) {
System.out.println("Don't know what happened");
}
}
}

View File

@@ -0,0 +1,15 @@
// "Replace with 'Files.newInputStream'" "false"
import java.io.*;
class Foo {
void test(String str) {
try(InputStream in = new FileInputStream(s<caret>tr)) {
}
catch (FileNotFoundException | RuntimeException e) {
System.out.println("file not found exception");
}
catch (IOException e) {
System.out.println("Don't know what happened");
}
}
}

View File

@@ -0,0 +1,18 @@
// "Replace with 'Files.newInputStream'" "true"
import java.io.*;
class Foo {
void test(String str) {
try {
if (str.length() < 3) throw new FileNotFoundException("e");
try (InputStream in = new FileInputStream(st<caret>r)) {
}
catch (IOException e) {
System.out.println("Don't know what happened");
}
}
catch (FileNotFoundException e) {
System.out.println("file not found exception");
}
}
}