Generate null-check after assignment instruction

Fixes IDEA-232554 False positive @nullable method returns non-null only

GitOrigin-RevId: 35d2655f3340b604c0056dfa90059c8a645d8632
This commit is contained in:
Tagir Valeev
2020-02-11 09:12:40 +00:00
committed by intellij-monorepo-bot
parent 5f6e96d4a7
commit 8235f95686
3 changed files with 27 additions and 1 deletions
@@ -237,6 +237,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
}
addInstruction(new AssignInstruction(rExpr, myFactory.createValue(lExpr)));
addNullCheck(expression);
finishElement(expression);
}
@@ -815,7 +816,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor {
PsiExpression returnValue = statement.getReturnValue();
if (myExpressionBlockContext != null) {
// We treat return inside switch expression (which is disallowed syntax) as break-with-value
// We treat return inside switch expression (which is disallowed syntax) as yield
myExpressionBlockContext.generateReturn(returnValue, this);
} else {
@@ -0,0 +1,24 @@
import org.jetbrains.annotations.Nullable;
class Hello {
@Nullable
String something;
@Nullable
private String getAndCacheSomething() {
if (something != null) {
return something;
}
return something = getSomething();
}
@Nullable
private String getAndCacheSomething2() {
if (something != null) {
return something;
}
something = getSomething();
return something;
}
@Nullable
String getSomething() {
return null;
}
}
@@ -276,4 +276,5 @@ public class DataFlowInspection8Test extends DataFlowInspectionTestCase {
}
public void testInlineLambdaFromLocal() { doTest(); }
public void testAllowRequireNonNullInCtor() { doTest(); }
public void testNullableNotNullAssignmentInReturn() { doTest(); }
}