simplifiable assertion: don't assume types are compatible

EA-117420 - NPE: SimplifiableAssertionInspection$SimplifyAssertFix.replaceAssertLiteralWithAssertEquals
This commit is contained in:
Anna.Kozlova
2018-03-09 11:44:46 +01:00
parent 828000c586
commit 661f7d1668
4 changed files with 34 additions and 10 deletions
@@ -256,18 +256,18 @@ public abstract class SimplifiableAssertionInspection extends BaseInspection {
final PsiType lhsType = lhs.getType();
final PsiType rhsType = rhs.getType();
if (lhsType != null && rhsType != null && PsiUtil.isLanguageLevel5OrHigher(lhs)) {
if (isPrimitiveAndBoxedWithOverloads(lhsType, rhsType)) {
final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(rhsType);
assert unboxedType != null;
buf.append(lhs.getText()).append(",(").append(unboxedType.getCanonicalText()).append(')').append(rhs.getText());
}
else if (isPrimitiveAndBoxedWithOverloads(rhsType, lhsType)) {
final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(lhsType);
assert unboxedType != null;
buf.append('(').append(unboxedType.getCanonicalText()).append(')').append(lhs.getText()).append(',').append(rhs.getText());
final PsiPrimitiveType rhsUnboxedType = PsiPrimitiveType.getUnboxedType(rhsType);
if (isPrimitiveAndBoxedWithOverloads(lhsType, rhsType) && rhsUnboxedType != null) {
buf.append(lhs.getText()).append(",(").append(rhsUnboxedType.getCanonicalText()).append(')').append(rhs.getText());
}
else {
buf.append(lhs.getText()).append(',').append(rhs.getText());
final PsiPrimitiveType unboxedType = PsiPrimitiveType.getUnboxedType(lhsType);
if (isPrimitiveAndBoxedWithOverloads(rhsType, lhsType) && unboxedType != null) {
buf.append('(').append(unboxedType.getCanonicalText()).append(')').append(lhs.getText()).append(',').append(rhs.getText());
}
else {
buf.append(lhs.getText()).append(',').append(rhs.getText());
}
}
}
else {
@@ -0,0 +1,12 @@
import org.junit.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class JUnit4TestCase {
@Test
public void testOne() {
assertEquals("1", 1);
}
}
@@ -0,0 +1,11 @@
import org.junit.*;
import static org.junit.Assert.assertTrue;
public class JUnit4TestCase {
@Test
public void testOne() {
<caret>assertTrue("1".equals(1));
}
}
@@ -32,6 +32,7 @@ public class SimplifiableJUnitAssertionFixTest extends IGQuickFixesTestCase {
public void testDoublePrimitive() { doTest(); }
public void testEqualsToTrueJUnit5() { doTest(); }
public void testTrueToEqualsJUnit5() { doTest(); }
public void testTrueToEqualsBetweenIncompatibleTypes() { doTest(); }
public void testFalseToNotEqualsJUnit4() { doTest(); }
@Override