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 9c97f027dff1..39696a69a16a 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 @@ -50,6 +50,11 @@ public class HardcodedContracts { constraints[0] = NULL_VALUE; return Collections.singletonList(new MethodContract(constraints, THROW_EXCEPTION)); } + if (("checkArgument".equals(methodName) || "checkState".equals(methodName)) && paramCount > 0) { + MethodContract.ValueConstraint[] constraints = createConstraintArray(paramCount); + constraints[0] = FALSE_VALUE; + return Collections.singletonList(new MethodContract(constraints, THROW_EXCEPTION)); + } } else if ("java.util.Objects".equals(className)) { if ("requireNonNull".equals(methodName) && paramCount > 0) { diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/BooleanPreconditions.java b/java/java-tests/testData/inspection/dataFlow/fixture/BooleanPreconditions.java new file mode 100644 index 000000000000..16697653abf6 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/BooleanPreconditions.java @@ -0,0 +1,13 @@ +import org.jetbrains.annotations.Nullable; + +class Contracts { + + private void check(@Nullable Object o, @Nullable Object o2) { + com.google.common.base.Preconditions.checkArgument(o != null); + com.google.common.base.Preconditions.checkState(o2 != null, ""); + System.out.println(o.hashCode()); + System.out.println(o2.hashCode()); + } + + +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index 5280bb2f972e..2a105e5bf7d0 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -364,6 +364,16 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { myFixture.testHighlighting(true, false, true, getTestName(false) + ".java"); } + public void testBooleanPreconditions() { + myFixture.addClass("package com.google.common.base; public class Preconditions { " + + "public static T checkArgument(boolean b) {}\n" + + "public static T checkArgument(boolean b, String msg) {}\n" + + "public static T checkState(boolean b, String msg) {}\n" + + "}"); + myFixture.enableInspections(new DataFlowInspection()); + myFixture.testHighlighting(true, false, true, getTestName(false) + ".java"); + } + public void testGuavaCheckNotNull() { myFixture.addClass("package com.google.common.base; public class Preconditions { " + "public static T checkNotNull(T reference) {}\n" +