private static boolean isImmediatelyUsedByYield(PsiVariable variable) {
      PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class);
      if (containingScope == null) return false;
      PsiDeclarationStatement declarationStatement = tryCast(variable.getParent(), PsiDeclarationStatement.class);
      if (declarationStatement == null) return false;
      PsiYieldStatement yieldStatement =
        tryCast(PsiTreeUtil.getNextSiblingOfType(declarationStatement, PsiStatement.class), PsiYieldStatement.class);
      if (yieldStatement == null) return false;
      PsiExpression returnValue = PsiUtil.skipParenthesizedExprDown(yieldStatement.getExpression());
      return returnValue instanceof PsiReferenceExpression &&
             ExpressionUtils.isReferenceTo(returnValue, variable) &&
             !isVariableUsedInFollowingDeclarations(variable, declarationStatement);
    }
private static boolean isImmediatelyUsedByYield(PsiVariable variable)