boolean (PsiVariable variable) { final PsiCodeBlock containingScope = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class, true, PsiClass.class); if (containingScope == null) { return false; } final PsiElement parent = variable.getParent(); if (!(parent instanceof PsiDeclarationStatement declarationStatement)) { return false; } PsiStatement nextStatement = PsiTreeUtil.getNextSiblingOfType(declarationStatement, PsiStatement.class); if (nextStatement instanceof PsiDeclarationStatement nextDeclarationStatement) { boolean referenceFound = false; for (PsiElement declaration : nextDeclarationStatement.getDeclaredElements()) { if (!(declaration instanceof PsiVariable nextVariable)) { continue; } final PsiExpression initializer = PsiUtil.skipParenthesizedExprDown(nextVariable.getInitializer()); if (!referenceFound && initializer instanceof PsiReferenceExpression referenceExpression) { final PsiElement referent = referenceExpression.resolve(); if (variable.equals(referent)) { referenceFound = true; continue; } } if (VariableAccessUtils.variableIsUsed(variable, initializer)) { return false; } } if (!referenceFound) { return false; } } else if (nextStatement instanceof PsiTryStatement tryStatement) { final PsiResourceList resourceList = tryStatement.getResourceList(); if (resourceList == null) { return false; } boolean referenceFound = false; for (PsiResourceListElement resource : resourceList) { if (resource instanceof PsiResourceVariable) { final PsiExpression initializer = ((PsiResourceVariable)resource).getInitializer(); if (!referenceFound && initializer instanceof PsiReferenceExpression referenceExpression) { final PsiElement referent = referenceExpression.resolve(); if (variable.equals(referent)) { referenceFound = true; continue; } } if (VariableAccessUtils.variableIsUsed(variable, initializer)) { return false; } } } if (!referenceFound) { return false; } if (VariableAccessUtils.variableIsUsed(variable, tryStatement.getTryBlock()) || VariableAccessUtils.variableIsUsed(variable, tryStatement.getFinallyBlock())) { return false; } for (PsiCatchSection section : tryStatement.getCatchSections()) { if (VariableAccessUtils.variableIsUsed(variable, section)) { return false; } } } else { return false; } if (isVariableUsedInFollowingDeclarations(variable, declarationStatement)) { return false; } nextStatement = PsiTreeUtil.getNextSiblingOfType(nextStatement, PsiStatement.class); while (nextStatement != null) { if (VariableAccessUtils.variableIsUsed(variable, nextStatement)) { return false; } nextStatement = PsiTreeUtil.getNextSiblingOfType(nextStatement, PsiStatement.class); } return true; }