Commit 0e12b5fc authored by Tagir Valeev's avatar Tagir Valeev
Browse files

IDEA-204773 Enable non-null parameter inference for non-stable source methods

parent 294d7d89
Showing with 18 additions and 7 deletions
+18 -7
......@@ -16,7 +16,8 @@ import org.jetbrains.annotations.NotNull;
* @author peter
*/
public class InferenceFromSourceUtil {
static boolean shouldInferFromSource(@NotNull PsiMethodImpl method) {
static boolean shouldInferFromSource(@NotNull PsiMethodImpl method, boolean allowOverridden) {
if (!allowOverridden && PsiUtil.canBeOverridden(method)) return false;
return CachedValuesManager.getCachedValue(method, () -> CachedValueProvider.Result
.create(calcShouldInferFromSource(method), method, PsiModificationTracker.JAVA_STRUCTURE_MODIFICATION_COUNT));
}
......@@ -24,7 +25,7 @@ public class InferenceFromSourceUtil {
private static boolean calcShouldInferFromSource(@NotNull PsiMethod method) {
if (isLibraryCode(method) ||
method.hasModifierProperty(PsiModifier.ABSTRACT) ||
PsiUtil.canBeOverridden(method)) {
method.hasModifierProperty(PsiModifier.NATIVE)) {
return false;
}
......
......@@ -41,7 +41,7 @@ public class JavaSourceInference {
*/
@NotNull
public static Nullability inferNullability(PsiMethodImpl method) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method)) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method, false)) {
return Nullability.UNKNOWN;
}
......@@ -71,7 +71,7 @@ public class JavaSourceInference {
PsiParameterList parent = ObjectUtils.tryCast(parameter.getParent(), PsiParameterList.class);
if (parent == null) return Nullability.UNKNOWN;
PsiMethodImpl method = ObjectUtils.tryCast(parent.getParent(), PsiMethodImpl.class);
if (method == null || !InferenceFromSourceUtil.shouldInferFromSource(method)) return Nullability.UNKNOWN;
if (method == null || !InferenceFromSourceUtil.shouldInferFromSource(method, true)) return Nullability.UNKNOWN;
return CachedValuesManager.getCachedValue(parameter, () -> {
Nullability nullability = Nullability.UNKNOWN;
......@@ -97,7 +97,7 @@ public class JavaSourceInference {
*/
@NotNull
public static Mutability inferMutability(PsiMethodImpl method) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method)) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method, false)) {
return Mutability.UNKNOWN;
}
......@@ -124,7 +124,7 @@ public class JavaSourceInference {
*/
@NotNull
public static List<StandardMethodContract> inferContracts(@NotNull PsiMethodImpl method) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method)) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method, false)) {
return Collections.emptyList();
}
......@@ -144,7 +144,7 @@ public class JavaSourceInference {
* @return true if method was inferred to be pure; false if method is not pure or cannot be analyzed
*/
public static boolean inferPurity(@NotNull PsiMethodImpl method) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method) || PsiType.VOID.equals(method.getReturnType())) {
if (!InferenceFromSourceUtil.shouldInferFromSource(method, false) || PsiType.VOID.equals(method.getReturnType())) {
return false;
}
......
class X {
public void test(String s) {
System.out.println(s.trim());
}
void use() {
test(<warning descr="Passing 'null' argument to parameter annotated as @NotNull">null</warning>);
}
}
\ No newline at end of file
......@@ -673,4 +673,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
public void testRewriteFinal() { doTest(); }
public void testFinalGettersForFinalFields() { doTest(); }
public void testInlineSimpleMethods() { doTest(); }
public void testInferenceForNonStableParameters() { doTest(); }
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment