{"id":"677e3eebe8b89a3c","repo":"rust-lang/rust","slug":"usetree-ident-can-only-be-used-on-a-simple-impo","errorCode":null,"errorMessage":"`UseTree::ident` can only be used on a simple import","messagePattern":"`UseTree::ident` can only be used on a simple import","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_ast/src/ast.rs","lineNumber":3379,"sourceCode":"\n/// A tree of paths sharing common prefixes.\n/// Used in `use` items both at top-level and inside of braces in import groups.\n#[derive(Clone, Encodable, Decodable, Debug, Walkable)]\npub struct UseTree {\n    pub prefix: Path,\n    pub kind: UseTreeKind,\n}\n\nimpl UseTree {\n    /// If the `UseTree` is just an identifier, return that.\n    /// Panics if it's a glob (`*`) or a nested use tree.\n    pub fn ident(&self) -> Ident {\n        match self.kind {\n            UseTreeKind::Simple(Some(rename)) => rename,\n            UseTreeKind::Simple(None) => {\n                self.prefix.segments.last().expect(\"empty prefix in a simple import\").ident\n            }\n            _ => panic!(\"`UseTree::ident` can only be used on a simple import\"),\n        }\n    }\n\n    /// Returns the full span from the start of the path to the\n    /// closing `}` or nested spans, `*` of glob spans or the end of the\n    /// identifier of simple spans.\n    pub fn span(&self) -> Span {\n        self.prefix.span.to(self.hi_span())\n    }\n\n    /// Returns the trailing element's span. So for a nested\n    /// span you get the entire `{}`-block, for a glob you\n    /// get the span of the `*` itself, and for simple use trees\n    /// you get the identifier to rename the import to or the full\n    /// path if no rename is specified.\n    pub fn hi_span(&self) -> Span {\n        match self.kind {\n            UseTreeKind::Simple(None) => self.prefix.span,","sourceCodeStart":3361,"sourceCodeEnd":3397,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_ast/src/ast.rs#L3361-L3397","documentation":"`panic!(\"\\`UseTree::ident\\` can only be used on a simple import\")` fires when `UseTree::ident()` is called on a `UseTreeKind::Glob` (`use foo::*`) or `UseTreeKind::Nested` (`use foo::{a, b}`). The method is only meaningful for `UseTreeKind::Simple` — globs have no single ident and nested trees are groups. Calling it on other variants is a programmer error.","triggerScenarios":"Any code path that calls `UseTree::ident()` on a use tree without first checking the variant — e.g. a visitor that walks `use` declarations uniformly and unconditionally extracts `ident()`. Triggered by `use crate::{a, b};`, `use crate::*;`, or any braced/glob import passed to `ident()`.","commonSituations":"Lint or analysis passes over `use` items that forget to branch on `UseTreeKind`. Procedural macros rewriting imports generically. Refactors that previously only saw simple imports and later encounter globs/nested groups. Not reproducible from valid user code alone — only via API misuse.","solutions":["Branch on `self.kind` first; call `ident()` only inside the `UseTreeKind::Simple` arm.","For globs/nested trees, handle them explicitly (iterate `Nested.items`, or record the glob span) instead of falling through to `ident()`.","If you must get the trailing path element generically, use `self.prefix.segments.last()` after confirming the variant."],"exampleFix":"// before\nlet name = use_tree.ident();\n\n// after\nlet name = match use_tree.kind {\n    UseTreeKind::Simple(_) => use_tree.ident(),\n    UseTreeKind::Glob(_) => /* handle glob */\n    UseTreeKind::Nested { .. } => /* handle nested group */\n};","handlingStrategy":"type-guard","validationCode":"match use_tree.kind {\n    UseTreeKind::Simple(_) => { let _id = use_tree.ident(); /* ok */ }\n    UseTreeKind::Glob | UseTreeKind::Nested(_) => { /* not a simple import */ }\n}","typeGuard":"fn is_simple_import(tree: &UseTree) -> bool {\n    matches!(tree.kind, UseTreeKind::Simple(_))\n}","tryCatchPattern":null,"preventionTips":["Read the doc comment: `ident()` panics on glob or nested use trees.","Always dispatch on `UseTreeKind` before accessing the ident.","For glob (`*`) or `Nested`, iterate `use_tree.items` instead of calling `ident()`."],"tags":["rustc","internals","ast","use-tree","api-misuse"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}