HashSet<String> (@NotNull PsiStatement statement) { HashSet<String> variableNames = new HashSet<>(); if (statement instanceof PsiForeachStatement) { PsiParameter declaration = ((PsiForeachStatement)statement).getIterationParameter(); variableNames.add((declaration).getName()); } if (statement instanceof PsiForStatement) { PsiStatement initStatement = ((PsiForStatement)statement).getInitialization(); if (initStatement instanceof PsiDeclarationStatement) { variableNames.addAll(processDeclarationStatement((PsiDeclarationStatement)initStatement)); } } PsiStatement statementBody = ((PsiLoopStatement)statement).getBody(); if (!(statementBody instanceof PsiBlockStatement)) return variableNames; variableNames.addAll(processFunctionStatements(((PsiBlockStatement)statementBody).getCodeBlock())); return variableNames; }