IPP: keep block if declarations would conflict otherwise

This commit is contained in:
Bas Leijdekkers
2016-01-20 15:36:23 +01:00
parent 92ba812988
commit 57ee8e5879
4 changed files with 48 additions and 7 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2007-2014 Bas Leijdekkers
* Copyright 2007-2016 Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.ig.psiutils.VariableSearchUtils;
import com.siyeh.ipp.base.Intention;
import com.siyeh.ipp.base.PsiElementPredicate;
import org.jetbrains.annotations.NotNull;
@@ -69,12 +70,18 @@ public class ConvertCatchToThrowsIntention extends Intention {
if (tryBlock == null) {
return;
}
final PsiElement first = tryBlock.getFirstBodyElement();
final PsiElement last = tryBlock.getLastBodyElement();
if (first != null && last != null) {
tryStatement.getParent().addRangeAfter(first, last, tryStatement);
final PsiCodeBlock parentCodeBlock = PsiTreeUtil.getParentOfType(tryStatement, PsiCodeBlock.class);
if (parentCodeBlock == null || !VariableSearchUtils.containsConflictingDeclarations(tryBlock, parentCodeBlock)) {
final PsiElement first = tryBlock.getFirstBodyElement();
final PsiElement last = tryBlock.getLastBodyElement();
if (first != null && last != null) {
tryStatement.getParent().addRangeAfter(first, last, tryStatement);
}
tryStatement.delete();
}
else {
tryStatement.replace(tryBlock);
}
tryStatement.delete();
}
}
@@ -0,0 +1,15 @@
class TryWithConflictingDeclaration {
abstract void f(String s) throws Exception;
void m() {
try {
/* important comment */
String s = "hello";
f();
// another comment
} catch (Exception <caret>ignore) { }
String s = "bye";
System.out.println(s);
}
}
@@ -0,0 +1,15 @@
class TryWithConflictingDeclaration {
abstract void f(String s) throws Exception;
void m() throws Exception {
{
/* important comment */
String s = "hello";
f();
// another comment
}
String s = "bye";
System.out.println(s);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2014 JetBrains s.r.o.
* Copyright 2000-2016 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,6 +18,9 @@ package com.siyeh.ipp.exceptions;
import com.siyeh.IntentionPowerPackBundle;
import com.siyeh.ipp.IPPTestCase;
/**
* @see ConvertCatchToThrowsIntention
*/
public class ConvertCatchToThrowsTest extends IPPTestCase {
public void testSingleCatch() { doTest(); }
public void testPluralCatches() { doTest(); }
@@ -27,6 +30,7 @@ public class ConvertCatchToThrowsTest extends IPPTestCase {
public void testExistingThrows() { doTest(); }
public void testLambda() { doTest(); }
public void testLeaveFinallySection() { doTest(); }
public void testTryWithConflictingDeclaration() { doTest(); }
@Override
protected String getIntentionName() {