{"record":{"id":"820dc9efb8e74d85","repo":"slint-ui/slint","slug":"length-was-checked","errorCode":null,"errorMessage":"Length was checked","messagePattern":"Length was checked","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"info","filePath":"tools/lsp/preview/drop_location.rs","lineNumber":1246,"sourceCode":"\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    };\n\n    let (path, _) = drop_info.target_element_node.path_and_offset();\n\n    let document_cache = preview::document_cache()?;\n    let doc = document_cache.get_document_by_path(&path)?;\n    let source_file = doc.node.as_ref().unwrap().source_file.clone();\n\n    let mut selection_offset = drop_info.insert_info.insertion_position.offset()\n        + TextSize::new(\n            new_text.chars().take_while(|c| c.is_whitespace()).map(|c| c.len_utf8()).sum::<usize>()\n                as u32,","sourceCodeStart":1228,"sourceCodeEnd":1264,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/tools/lsp/preview/drop_location.rs#L1228-L1264","documentation":"Internal invariant panic from `.expect(\"Length was checked\")` on `element_text_lines.last()` in tools/lsp/preview/drop_location.rs:1246. The preceding `if element_text_lines.len() >= 2` check guarantees `last()` returns `Some`; the expect encodes that already-verified precondition while appending the closing line and post-indent to the re-inserted element text.","triggerScenarios":"Not triggerable with the current source — `last()` is only called after the `len() >= 2` guard. Panics only if a refactor removes or moves the length check away from the `last()` call, or if the vector is mutated between the check and the access.","commonSituations":"Hit by maintainers refactoring the multi-line element re-insertion logic in create_drop_element_workspace_edit who separate the bounds check from the indexed access, or by developers reusing this snippet elsewhere without copying the guard.","solutions":["Keep the `.last()` call inside the `if element_text_lines.len() >= 2` block.","Use `if let Some(last) = element_text_lines.last()` combined with an explicit `element_text_lines.len() >= 2` condition if both bounds matter.","Add a test with a multi-line element covering the len>=2 branch of the workspace edit builder."],"exampleFix":"// before\nif element_text_lines.len() >= 2 {\n    tmp.push_str(&format!(\"{}{}\\n{}\", drop_info.insert_info.indent, element_text_lines.last().expect(\"Length was checked\"), drop_info.insert_info.post_indent));\n}\n// after\nif let (Some(first), Some(last)) = (element_text_lines.first(), element_text_lines.last()) {\n    if first != last {\n        tmp.push_str(&format!(\"{}{}\\n{}\", drop_info.insert_info.indent, last, drop_info.insert_info.post_indent));\n    }\n}","handlingStrategy":"type-guard","validationCode":"debug_assert!(element_text_lines.len() >= 2, \"last() only called when len >= 2\");","typeGuard":"fn last_line(lines: &[String]) -> Option<&str> { lines.last().map(String::as_str) }","tryCatchPattern":"// Rust: no catch; use Option combinators instead of expect\nif element_text_lines.len() >= 2 {\n    if let Some(last) = element_text_lines.last() { /* ... */ }\n}","preventionTips":["Never mutate the vec between the len check and last().","Use debug_assert! to document preconditions without release panics.","Add a multi-line element test covering the len>=2 branch."],"tags":["rust","panic","lsp","invariant","bounds-check"],"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"}