boolean (boolean nullsExpected) { PsiElement body = null; if (myCodeFragmentMember instanceof PsiParameterListOwner) { body = ((PsiParameterListOwner)myCodeFragmentMember).getBody(); } if (body == null) return false; Set<PsiExpression> returnedExpressions = StreamEx.of(myExitStatements) .select(PsiReturnStatement.class) .map(PsiReturnStatement::getReturnValue) .nonNull() .toSet(); for (Iterator<PsiExpression> it = returnedExpressions.iterator(); it.hasNext(); ) { PsiType type = it.next().getType(); if (nullsExpected) { if (type == PsiTypes.nullType()) { it.remove(); // don't need to check } else if (type instanceof PsiPrimitiveType) { return false; } } else { if (type == PsiTypes.nullType()) { return false; } else if (type instanceof PsiPrimitiveType) { it.remove(); // don't need to check } } } if (returnedExpressions.isEmpty()) return true; final var dfaRunner = new StandardDataFlowRunner(myProject); final var returnChecker = new JavaDfaListener() { @Override public void beforeValueReturn(@NotNull DfaValue value, @Nullable PsiExpression expression, @NotNull PsiElement context, @NotNull DfaMemoryState state) { if (context == myCodeFragmentMember && expression != null && returnedExpressions.stream().anyMatch(ret -> PsiTreeUtil.isAncestor(ret, expression, false))) { DfaNullability nullability = DfaNullability.fromDfType(state.getDfType(value)); boolean result = nullability == (nullsExpected ? DfaNullability.NULL : DfaNullability.NOT_NULL); if (!result) { dfaRunner.cancel(); } } } }; return dfaRunner.analyzeMethod(body, returnChecker) == RunnerResult.OK; }