chore: clear clippy warnings and tighten the build guard

- base64_encode uses div_ceil plus a slice loop (clippy: manual_div_ceil,
  chunks_exact_to_as_chunks); add known-vector tests.
- macOS-only UserEvent variants no longer warn on Windows.
- needless borrow, needless bool, and a collapsible match arm in the
  Destroyed handler.
- build.ps1 no longer treats the word "powershell" as an updater marker;
  it now checks Start-Process / Invoke-WebRequest instead.
This commit is contained in:
2026-11-03 22:11:03 +08:00
parent c783a6dc56
commit 8221eb5908
2 changed files with 31 additions and 13 deletions

View File

@@ -33,6 +33,9 @@ struct SelfWriteRecord {
content: String,
}
/// Menu-only events exist for macOS; on Windows the page drives the same
/// actions over IPC, so those variants are never constructed here.
#[cfg_attr(not(target_os = "macos"), allow(dead_code))]
#[derive(Debug)]
enum UserEvent {
NewFile(WindowId),
@@ -322,7 +325,7 @@ fn confirm_discard_unsaved(error: &str) -> bool {
rfd::MessageDialog::new()
.set_level(rfd::MessageLevel::Warning)
.set_title("Could Not Save")
.set_description(&format!(
.set_description(format!(
"{error}\n\nThe file may be read-only or locked by another program. Close the window and discard the unsaved changes?"
))
.set_buttons(rfd::MessageButtons::OkCancel)
@@ -610,17 +613,20 @@ fn image_mime_type(path: &Path) -> Option<&'static str> {
fn base64_encode(bytes: &[u8]) -> String {
const TABLE: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(((bytes.len() + 2) / 3) * 4);
let mut chunks = bytes.chunks_exact(3);
for chunk in &mut chunks {
let n = ((chunk[0] as u32) << 16) | ((chunk[1] as u32) << 8) | chunk[2] as u32;
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
let mut index = 0;
while index + 3 <= bytes.len() {
let n = ((bytes[index] as u32) << 16)
| ((bytes[index + 1] as u32) << 8)
| bytes[index + 2] as u32;
out.push(TABLE[((n >> 18) & 0x3f) as usize] as char);
out.push(TABLE[((n >> 12) & 0x3f) as usize] as char);
out.push(TABLE[((n >> 6) & 0x3f) as usize] as char);
out.push(TABLE[(n & 0x3f) as usize] as char);
index += 3;
}
match chunks.remainder() {
match &bytes[index..] {
[a] => {
let n = (*a as u32) << 16;
out.push(TABLE[((n >> 18) & 0x3f) as usize] as char);
@@ -2797,6 +2803,15 @@ mod tests {
assert!(html.contains("<hr />"));
}
#[test]
fn base64_matches_known_vectors() {
assert_eq!(base64_encode(b""), "");
assert_eq!(base64_encode(b"f"), "Zg==");
assert_eq!(base64_encode(b"fo"), "Zm8=");
assert_eq!(base64_encode(b"foo"), "Zm9v");
assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
}
#[test]
fn only_markdown_extensions_are_supported_documents() {
for name in ["note.md", "note.MARKDOWN", "note.mdown", "note.mkd"] {
@@ -4334,6 +4349,7 @@ fn build_startup_page(strings: &Strings) -> String {
)
}
#[allow(clippy::too_many_arguments)] // shared window plumbing; split with the webview module
fn build_webview(
window: &Window,
web_context: &mut WebContext,
@@ -4365,10 +4381,8 @@ fn build_webview(
false,
));
false
} else if url.starts_with("file:") {
false
} else {
true
!url.starts_with("file:")
}
})
.with_ipc_handler(move |msg| {
@@ -5509,9 +5523,11 @@ fn main() {
window_id,
..
} => {
if app.windows.contains_key(&window_id) {
close_window(&mut app, window_id, control_flow);
// The window may already be gone (closed by CloseRequested).
if !app.windows.contains_key(&window_id) {
return;
}
close_window(&mut app, window_id, control_flow);
}
_ => {}
}