boolean (PsiDeclarationStatement decl) { PsiElement[] declaredElements = decl.getDeclaredElements(); if (declaredElements.length != 1) return false; if (!(declaredElements[0] instanceof PsiLocalVariable var)) return false; if (var.getInitializer() == null) return false; if (var.getTypeElement().isInferredType() && !PsiTypesUtil.isDenotableType(var.getType(), var)) { return false; } PsiElement parent = decl.getParent(); if (parent instanceof PsiForStatement) { String varName = var.getName(); parent = parent.getNextSibling(); while (parent != null) { Ref<Boolean> conflictFound = new Ref<>(false); parent.accept(new JavaRecursiveElementWalkingVisitor() { @Override public void visitClass(@NotNull PsiClass aClass) { } @Override public void visitVariable(@NotNull PsiVariable variable) { super.visitVariable(variable); if (varName.equals(variable.getName())) { conflictFound.set(true); stopWalking(); } } }); if (conflictFound.get()) { return false; } parent = parent.getNextSibling(); } } return true; }