diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java index 038bffddab53..98a20300cd22 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/HardcodedContracts.java @@ -199,6 +199,10 @@ public class HardcodedContracts { return className.startsWith("junit.framework.") || className.startsWith("org.junit."); } + private static boolean isJunit5(String className) { + return className.startsWith("org.junit.jupiter."); + } + private static boolean isTestng(String className) { return className.startsWith("org.testng."); } @@ -245,7 +249,7 @@ public class HardcodedContracts { if (paramCount == 0) return Collections.emptyList(); - int checkedParam = testng ? 0 : paramCount - 1; + int checkedParam = testng || isJunit5(className) ? 0 : paramCount - 1; MethodContract.ValueConstraint[] constraints = createConstraintArray(paramCount); if ("assertTrue".equals(methodName) || "assumeTrue".equals(methodName)) { constraints[checkedParam] = FALSE_VALUE; diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/Junit5Assert.java b/java/java-tests/testData/inspection/dataFlow/fixture/Junit5Assert.java index 9a0c5684f31f..bc7ca9fbd981 100644 --- a/java/java-tests/testData/inspection/dataFlow/fixture/Junit5Assert.java +++ b/java/java-tests/testData/inspection/dataFlow/fixture/Junit5Assert.java @@ -8,4 +8,20 @@ class Contracts { String s = o.toString(); } + void foo2(@Nullable Object o) { + Assertions.assertNotNull(o, "message"); + String s = o.toString(); + } + + void foo3(java.util.function.BooleanSupplier o) { + Assertions.assertTrue(o, "message"); + } + + void foo3(boolean b) { + Assertions.assertTrue(b, "message"); + if (!b) { + System.out.println("how?"); + } + } + } \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/HardcodedContractsTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/HardcodedContractsTest.java index b0add7d87154..15018ea6e785 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/HardcodedContractsTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/HardcodedContractsTest.java @@ -17,11 +17,18 @@ package com.intellij.java.codeInspection; import com.intellij.JavaTestUtil; import com.intellij.codeInspection.dataFlow.DataFlowInspection; +import com.intellij.testFramework.LightProjectDescriptor; +import org.jetbrains.annotations.NotNull; /** * @author peter */ public class HardcodedContractsTest extends DataFlowInspectionTestCase { + @NotNull + @Override + protected LightProjectDescriptor getProjectDescriptor() { + return JAVA_8; + } @Override protected String getTestDataPath() { @@ -110,8 +117,13 @@ public class HardcodedContractsTest extends DataFlowInspectionTestCase { } public void testJunit5Assert() { - myFixture.addClass("package org.junit.jupiter.api; public class Assertions {\n" + + myFixture.addClass("package org.junit.jupiter.api;" + + "import java.util.function.BooleanSupplier;" + + "public class Assertions {\n" + " public static void assertNotNull(Object actual){}" + + " public static void assertNotNull(Object actual, String message){}" + + " public static void assertTrue(boolean b, String message){}" + + " public static void assertTrue(BooleanSupplier b, String message){}" + "}"); checkHighlighting(); }