{"record":{"id":"cb714f1e9edeb0e4","repo":"coding-horror/basic-computer-games","slug":"could-not-create-file","errorCode":null,"errorMessage":"Could not create file!","messagePattern":"Could not create file!","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"81_Splat/rust/src/stats.rs","lineNumber":45,"sourceCode":"                for s in stats {\n                    if s.is_empty() {\n                        continue;\n                    }\n\n                    let s = s.parse::<f32>().expect(\"Corrupt stats file!\");\n                    altitudes.push(s);\n                }\n\n                return Some(Stats { altitudes });\n            } else {\n                println!(\"PREVIOUS GAME DATA NOT FOUND!\");\n\n                if !utility::prompt_bool(\"WOULD YOU LIKE TO CREATE ONE?\", false) {\n                    return None;\n                } else {\n                    let mut file = File::create(path).expect(\"Invalid file path!\");\n                    file.write_all(\"\".as_bytes())\n                        .expect(\"Could not create file!\");\n\n                    return Some(Stats {\n                        altitudes: Vec::new(),\n                    });\n                }\n            }\n        }\n\n        println!(\"\\nRESULTS OF THIS SESSION WILL NOT BE SAVED.\");\n        None\n    }\n\n    pub fn add_altitude(&mut self, a: f32) {\n        let all_jumps = self.altitudes.len() + 1;\n        let mut placement = all_jumps;\n\n        for (i, altitude) in self.altitudes.iter().enumerate() {\n            if a <= *altitude {","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/coding-horror/basic-computer-games/blob/5301155192d91d74d337899cecc59dbda59c4c17/81_Splat/rust/src/stats.rs#L27-L63","documentation":"This panic is triggered by `.expect(\"Could not create file!\")` on `file.write_all(\"\".as_bytes())` in Splat's `Stats::new()` (line 45). Writing zero bytes is effectively a no-op for data purposes, so this `.expect` is really checking that the newly created file handle is writable. It would fail only on a very unusual post-create I/O error.","triggerScenarios":"An I/O error occurs on a file handle that was just successfully created by `File::create` on the preceding line. This is rare in practice — the more likely failure path is line 43 (`File::create` itself). Possible if the filesystem experiences a transient error, the handle is invalid, or a concurrent process interferes.","commonSituations":"Disk full or quota exceeded between create and write. NFS or network filesystem latency/failure. Antivirus or security software locking the file between create and write. Extremely rare in local filesystems.","solutions":["Remove the `write_all(\"\")` call entirely — it writes nothing and is pointless after a successful `File::create`.","If the intent is to truncate/initialize the file, use `write_all(\"\\n\".as_bytes())` or simply rely on `File::create` which already truncates.","Replace `.expect` with error handling that reports the underlying `io::Error`."],"exampleFix":"// before\nlet mut file = File::create(path).expect(\"Invalid file path!\");\nfile.write_all(\"\".as_bytes()).expect(\"Could not create file!\");\n\n// after\n// File::create already truncates the file to empty,\n// so the write_all(\"\") call is unnecessary.\nlet mut file = match File::create(path) {\n    Ok(f) => f,\n    Err(e) => { eprintln!(\"Cannot create stats file: {}\", e); return None; }\n};","handlingStrategy":"try-catch","validationCode":"// This write_all(\"\") is a no-op; verify the file handle is writable\n// by checking File::create succeeded (already done on the prior line).\n// No additional validation is needed — remove the call.","typeGuard":"// No type guard needed; the operation writes zero bytes.\n// The real guard is File::create's Result on the preceding line.","tryCatchPattern":"if let Err(e) = file.write_all(\"\".as_bytes()) {\n    eprintln!(\"Warning: file write check failed: {}\", e);\n    return None;\n}\n// Or better: remove the write_all(\"\") call entirely.","preventionTips":["Remove no-op operations like write_all(empty_bytes) — they add failure points without benefit.","File::create already truncates; no explicit empty write is needed.","Handle file I/O errors at the operation that actually matters, not a redundant sanity check."],"tags":["rust","file-io","panic","expect","dead-code","splat","stats-persistence"],"backgroundTag":null,"analyzedSha":"5301155192d91d74d337899cecc59dbda59c4c17","analyzedAt":"2026-08-13T19:29:00.979Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}