@NotNull
  private static Nullability inferNullability(@NotNull PsiVariable variable, PsiElement startElement) {
    String variableName = variable.getName();
    if (variableName == null) return Nullability.UNKNOWN;

    PsiElement methodOrLambdaBody = null;
    if (variable instanceof PsiPatternVariable) {
      return Nullability.NOT_NULL;
    }
    if (variable instanceof PsiLocalVariable || variable instanceof PsiParameter) {
      final PsiParameterListOwner methodOrLambda = PsiTreeUtil.getParentOfType(variable, PsiMethod.class, PsiLambdaExpression.class);
      if (methodOrLambda != null) {
        methodOrLambdaBody = methodOrLambda.getBody();
      }
    }
    if (methodOrLambdaBody instanceof PsiCodeBlock) {
      PsiElement topmostLambdaOrAnonymousClass = null;
      for (PsiElement element = startElement; element != null && element != methodOrLambdaBody; element = element.getParent()) {
        if (element instanceof PsiLambdaExpression || element instanceof PsiAnonymousClass) {
          topmostLambdaOrAnonymousClass = element;
        }
      }
      if (topmostLambdaOrAnonymousClass != null) {
        startElement = topmostLambdaOrAnonymousClass;
      }

      Project project = methodOrLambdaBody.getProject();
      PsiFile file = methodOrLambdaBody.getContainingFile();
      final PsiFile copy = PsiFileFactory.getInstance(project)
        .createFileFromText(file.getName(), file.getFileType(), file.getText(), file.getModificationStamp(), false);

      PsiCodeBlock bodyCopy = findCopy(copy, methodOrLambdaBody, PsiCodeBlock.class);
      if (startElement instanceof PsiExpression) {
        startElement = PsiTreeUtil.getParentOfType(startElement, PsiStatement.class);
      }
      if (startElement instanceof PsiStatement) {
        PsiStatement startStatementCopy = findCopy(copy, startElement, PsiStatement.class);

        try {
          PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
          startStatementCopy = wrapWithBlockStatementIfNeeded(startStatementCopy, factory);
          String dummyName = JavaCodeStyleManager.getInstance(project).suggestUniqueVariableName("_Dummy_", startElement.getParent(), true);
          PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement)factory.createStatementFromText(
            CommonClassNames.JAVA_LANG_OBJECT + " " + dummyName + " = " + variableName + ";", startStatementCopy);

          PsiElement parent = startStatementCopy.getParent();
          declarationStatement = (PsiDeclarationStatement)parent.addBefore(declarationStatement, startStatementCopy);
          PsiElement[] declaredElements = declarationStatement.getDeclaredElements();
          PsiExpression initializer = Objects.requireNonNull(((PsiVariable)declaredElements[0]).getInitializer());

          return inferNullability(bodyCopy, initializer);
        }
        catch (IncorrectOperationException ignore) {
          return Nullability.UNKNOWN;
        }
      }
    }
    return Nullability.UNKNOWN;
  }
@NotNull
  private static Nullability inferNullability(@NotNull PsiVariable variable, PsiElement startElement)