{"record":{"id":"b4f1b589a43e998a","repo":"slint-ui/slint","slug":"not-empty","errorCode":null,"errorMessage":"Not empty","messagePattern":"Not empty","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"tools/lsp/preview/drop_location.rs","lineNumber":1235,"sourceCode":"}\n\npub fn create_swap_element_workspace_edit(\n    drop_info: &DropInformation,\n    element: &editor_preview::ElementRcNode,\n    placeholder_text: String,\n    format: editor_preview::ByteFormat,\n) -> Option<(lsp_types::WorkspaceEdit, DropData)> {\n    let component_type = element.component_type();\n    let new_text = {\n        let element_text_lines = extract_text_of_element(element, &[\"x\", \"y\"]);\n\n        if element_text_lines.is_empty() {\n            String::new()\n        } else {\n            let mut tmp = format!(\n                \"{}{}\\n\",\n                drop_info.insert_info.pre_indent,\n                element_text_lines.first().expect(\"Not empty\")\n            );\n\n            for l in element_text_lines.iter().take(element_text_lines.len() - 1).skip(1) {\n                tmp.push_str(&format!(\"{}{l}\\n\", drop_info.insert_info.indent));\n            }\n\n            if element_text_lines.len() >= 2 {\n                tmp.push_str(&format!(\n                    \"{}{}\\n{}\",\n                    drop_info.insert_info.indent,\n                    element_text_lines.last().expect(\"Length was checked\"),\n                    drop_info.insert_info.post_indent\n                ));\n            }\n\n            tmp\n        }\n    };","sourceCodeStart":1217,"sourceCodeEnd":1253,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/tools/lsp/preview/drop_location.rs#L1217-L1253","documentation":"Internal invariant panic from `.expect(\"Not empty\")` on `element_text_lines.first()` in tools/lsp/preview/drop_location.rs:1235. The enclosing `if element_text_lines.is_empty()` branch guarantees the vector has at least one element, so `first()` is `Some`; the expect documents that checked precondition while building the re-inserted element text with its pre-indent.","triggerScenarios":"Not triggerable as written — the guard `if element_text_lines.is_empty() { String::new() } else { ... }` ensures `first()` succeeds. Would only panic if the code were restructured so the `.first()` call no longer sits inside the non-empty else branch (e.g. hoisting it out or converting the if to a match on something else).","commonSituations":"Encountered by maintainers after refactoring the workspace-edit builder in drop_location.rs so the emptiness check and the `first()` access become separated, or by anyone copying this pattern without the guard.","solutions":["Keep the `first()` access inside the branch guarded by `!element_text_lines.is_empty()`.","If the guard cannot be proven locally, switch to `element_text_lines.first().map(...).unwrap_or_default()`.","Prefer `if let Some(first) = element_text_lines.first()` to make the invariant explicit in code rather than in a panic message."],"exampleFix":"// before\nlet mut tmp = format!(\"{}{}\\n\", drop_info.insert_info.pre_indent, element_text_lines.first().expect(\"Not empty\"));\n// after\nlet Some(first_line) = element_text_lines.first() else { return String::new(); };\nlet mut tmp = format!(\"{}{}\\n\", drop_info.insert_info.pre_indent, first_line);","handlingStrategy":"type-guard","validationCode":"if element_text_lines.is_empty() { return String::new(); }","typeGuard":"fn first_line(lines: &[String]) -> Option<&str> { lines.first().map(String::as_str) }","tryCatchPattern":"// Rust: no catch; use let-else instead of expect\nlet Some(first) = element_text_lines.first() else { return String::new(); };","preventionTips":["Keep bounds-check and indexed access adjacent in the same block.","Use if-let/let-else so the non-empty case is encoded in control flow, not a panic string.","Copy the guard along with the code when reusing the snippet elsewhere."],"tags":["rust","panic","lsp","invariant","internal"],"backgroundTag":"internal-invariant-violation","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}