{"record":{"id":"c5be5075d32607a2","repo":"JetBrains/intellij-community","slug":"can-t-bind-to-non-labeled-statement","errorCode":null,"errorMessage":"Can't bind to non-labeled statement","messagePattern":"Can't bind to non-labeled statement","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-psi-impl/src/com/intellij/psi/impl/source/PsiLabelReference.java","lineNumber":58,"sourceCode":"      }\n    }\n    return null;\n  }\n\n  @Override\n  public @NotNull String getCanonicalText() {\n    return getElement().getText();\n  }\n\n  @Override\n  public PsiElement handleElementRename(@NotNull String newElementName) throws IncorrectOperationException {\n    myIdentifier = (PsiIdentifier)PsiImplUtil.setName(myIdentifier, newElementName);\n    return myIdentifier;\n  }\n\n  @Override\n  public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {\n    if (!(element instanceof PsiLabeledStatement)) throw new IncorrectOperationException(\"Can't bind to non-labeled statement\");\n    return handleElementRename(((PsiLabeledStatement)element).getName());\n  }\n\n  @Override\n  public boolean isReferenceTo(@NotNull PsiElement element) {\n    return resolve() == element;\n  }\n\n  @Override\n  public boolean isSoft() {\n    return false;\n  }\n}","sourceCodeStart":40,"sourceCodeEnd":71,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-psi-impl/src/com/intellij/psi/impl/source/PsiLabelReference.java#L40-L71","documentation":"PsiLabelReference is the reference from a break/continue statement to its label. bindToElement re-points a reference at a target element, but a label reference can only be bound to a PsiLabeledStatement; anything else throws IncorrectOperationException(\"Can't bind to non-labeled statement\").","triggerScenarios":"Calling bindToElement on the reference returned by PsiBreakStatement.getLabelReference() (or continue) with an element that is not a PsiLabeledStatement — e.g. a PsiMethod, PsiClass, or PsiField resolved from elsewhere.","commonSituations":"Refactoring plugins that rebind references after moving targets; 'fix reference' intentions that resolve to the wrong element; automated migration tools that call bindToElement uniformly on all references.","solutions":["Check the element type first: only call bindToElement when element instanceof PsiLabeledStatement.","If you want to change the label text, call handleElementRename(newLabelName) instead, which renames the identifier.","For generic reference processing, prefer checking reference.isReferenceTo(target) or use the ResolveResult target kinds before binding."],"exampleFix":"// before\nlabelRef.bindToElement(target); // target is e.g. PsiMethod -> throws\n\n// after\nif (target instanceof PsiLabeledStatement) {\n  labelRef.bindToElement(target);\n} else {\n  labelRef.handleElementRename(target.getName()); // just rename label text\n}","handlingStrategy":"type-guard","validationCode":"if (!(target instanceof PsiLabeledStatement)) return; // cannot bind label ref","typeGuard":"static boolean canBindLabelRef(PsiElement e) { return e instanceof PsiLabeledStatement; }","tryCatchPattern":"try { labelRef.bindToElement(t); } catch (IncorrectOperationException e) { /* fall back to handleElementRename */ }","preventionTips":["Match reference type to target type before bindToElement.","Use handleElementRename when only the label text should change."],"tags":["java","psi","references","labels","bind"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}