Skip to content

fix: handle files without extension in check_file_or_append#1184

Open
kuishou68 wants to merge 1 commit intotw93:mainfrom
kuishou68:fix/issue-1183-check-file-no-extension
Open

fix: handle files without extension in check_file_or_append#1184
kuishou68 wants to merge 1 commit intotw93:mainfrom
kuishou68:fix/issue-1183-check-file-no-extension

Conversation

@kuishou68
Copy link
Copy Markdown

Summary

Fixes a panic that occurs when check_file_or_append is called with a file path that has no extension (e.g., Makefile, README, or any downloaded binary without a suffix).

Root Cause

The original code unconditionally called .unwrap() on PathBuf::extension(), which returns None for extension-less filenames, causing a runtime panic:

// BEFORE (panics when there is no extension)
let extension = new_path.extension().unwrap().to_string_lossy().to_string();

Fix

Use .map() + unwrap_or_default() for file_stem, and keep extension as an Option<String>. Then branch on whether the extension is present when constructing the new path:

// AFTER (handles missing extension gracefully)
let file_stem = new_path
    .file_stem()
    .map(|s| s.to_string_lossy().to_string())
    .unwrap_or_default();
let extension = new_path
    .extension()
    .map(|e| e.to_string_lossy().to_string());
// ...
new_path = match &extension {
    Some(ext) => parent_dir.join(format!("{new_file_stem}.{ext}")),
    None => parent_dir.join(new_file_stem),
};

This also adds use std::path::Path; to support the Path::new("") fallback for the edge case where parent() returns None.

Closes #1183

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Panic in check_file_or_append when filename has no extension

1 participant