Files
openide/java/java-tests/testData/inspection/dataFlow/fixture/PureNoArgMethodAsVariable.java
Tagir Valeev a2ea53a7b0 DFA: pure no-args methods are considered as variables
Ref-returning methods are not included into eq-classes, only variable state is tracked for them. Primitive-returning methods are handled like normal variables (their result is considered to be stable)
Fixes IDEA-141547 ConstantConditions inspection reports false positive when using non getter method
2017-12-22 17:59:45 +07:00

43 lines
1.1 KiB
Java

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
// IDEA-141547
public class PureNoArgMethodAsVariable {
public enum Bar {
A, B;
@Nullable
@Contract(pure = true)
public String getGroup() {
return this == A ? null : "B";
}
@Nullable
@Contract(pure = true)
public String group() {
return this == A ? null : "B";
}
}
public void foo(Bar bar) {
if (bar.getGroup() != null && check(bar.getGroup())) { // NO inspection error, OK!
System.out.print("ok");
}
if (bar.group() != null && check(bar.group())) { // Inspection error, NOT OK!
System.out.print("ok");
}
}
void testIntValue(Integer x) {
if(<warning descr="Condition 'x.intValue() > 5 && x.intValue() < 0' is always 'false'">x.intValue() > 5 &&
<warning descr="Condition 'x.intValue() < 0' is always 'false' when reached">x.intValue() < 0</warning></warning>) {
System.out.println("impossible");
}
}
public boolean check(@NotNull String string) {
return string.length() > 2;
}
}