From 95f259a2bb57ee46a58cc75adb1d3a02b120fe70 Mon Sep 17 00:00:00 2001 From: dailz Date: Mon, 22 Jun 2026 15:02:33 +0800 Subject: [PATCH] fix(core): silence three clippy warnings blocking CI gate * line_index.rs: replace manual `x % BLOCK_SIZE == 0` with `.is_multiple_of(BLOCK_SIZE)` (Rust 1.81+). * line_index.rs: gate three pub(crate) accessor methods (`sampled_offsets`, `total_lines`, `has_trailing_newline`) behind `#[cfg(test)]`. They are only consumed by tests in file_reader.rs; marking them test-only removes them from production builds entirely, eliminating the dead_code warning without suppressing it. * json.rs: drop redundant `Some(... .ok()?)` wrapper in parse_json_object_with_duplicates. `.ok()` already returns Option, so wrapping it in Some and unwrapping with ? was a no-op. No behavior change. Unblocks `cargo clippy --workspace -- -D warnings`. --- crates/core/src/io/line_index.rs | 6 ++++-- crates/core/src/parser/json.rs | 22 ++++++++++++++++------ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/core/src/io/line_index.rs b/crates/core/src/io/line_index.rs index c492629..2c050df 100644 --- a/crates/core/src/io/line_index.rs +++ b/crates/core/src/io/line_index.rs @@ -158,7 +158,7 @@ impl LineIndex { // If the junction falls on a block boundary, record the start offset // (analogous to from_bytes always pushing offset 0 for line 0). - if starts_new_line && (old_total as usize) % BLOCK_SIZE == 0 { + if starts_new_line && (old_total as usize).is_multiple_of(BLOCK_SIZE) { self.sampled_offsets.push(start_offset); } @@ -212,15 +212,17 @@ impl LineIndex { self.total_lines as usize } - // ─── getter 方法 ──────────────────────────────────────────────────── + #[cfg(test)] pub(crate) fn sampled_offsets(&self) -> &[u64] { &self.sampled_offsets } + #[cfg(test)] pub(crate) fn total_lines(&self) -> u64 { self.total_lines } + #[cfg(test)] pub(crate) fn has_trailing_newline(&self) -> bool { self.has_trailing_newline } diff --git a/crates/core/src/parser/json.rs b/crates/core/src/parser/json.rs index d2b234f..624337e 100644 --- a/crates/core/src/parser/json.rs +++ b/crates/core/src/parser/json.rs @@ -22,8 +22,8 @@ use std::collections::{HashMap, HashSet}; // 默认的 serde_json::from_str::>() 遇到重复键时会采用"后者覆盖前者"(last-wins), // 前面的值被静默丢弃。这里我们通过自定义 Visitor 在反序列化过程中逐个观察 key-value 对, // 在保持 last-wins 行为的同时,将重复 key 的所有值记录到 DuplicateKey 中。 -use serde::de::{MapAccess, Visitor}; use serde::Deserializer; +use serde::de::{MapAccess, Visitor}; // serde_json::Value — 来自 serde_json 库(Rust 中最流行的 JSON 处理库)。 // Value 是一个枚举类型,可以表示任意 JSON 值: @@ -83,7 +83,10 @@ pub fn detect_json_log(line: &str) -> bool { // 则匹配成功。_ 是通配符,表示"不关心对象里面的具体内容"。 // // 如果匹配到 Ok(Value::Object(_)) 返回 true,否则返回 false。 - matches!(serde_json::from_str::(strip_bom(line)), Ok(Value::Object(_))) + matches!( + serde_json::from_str::(strip_bom(line)), + Ok(Value::Object(_)) + ) } // ─── DuplicateKeyVisitor ────────────────────────────────────────────────── @@ -147,7 +150,7 @@ fn parse_json_object_with_duplicates( json: &str, ) -> Option<(serde_json::Map, Vec)> { let mut deserializer = serde_json::Deserializer::from_str(json); - Some(deserializer.deserialize_map(DuplicateKeyVisitor).ok()?) + deserializer.deserialize_map(DuplicateKeyVisitor).ok() } // ─── take_string_field_from_map 辅助函数 ────────────────────────────────── @@ -189,7 +192,8 @@ pub fn parse_line(line: &str) -> Option { let (mut obj, duplicate_keys) = parse_json_object_with_duplicates(line)?; let raw_line = line.to_string(); - let timestamp = take_string_field_from_map(&mut obj, &["timestamp", "time", "ts", "@timestamp"]); + let timestamp = + take_string_field_from_map(&mut obj, &["timestamp", "time", "ts", "@timestamp"]); let level = take_string_field_from_map(&mut obj, &["level", "lvl", "severity"]) .map(|s| s.parse::().unwrap_or_else(|e| match e {})); @@ -513,8 +517,14 @@ mod tests { assert_eq!(entry.duplicate_keys.len(), 1); assert_eq!(entry.duplicate_keys[0].key, "message"); assert_eq!(entry.duplicate_keys[0].values.len(), 2); - assert_eq!(entry.duplicate_keys[0].values[0], Value::String("first".into())); - assert_eq!(entry.duplicate_keys[0].values[1], Value::String("second".into())); + assert_eq!( + entry.duplicate_keys[0].values[0], + Value::String("first".into()) + ); + assert_eq!( + entry.duplicate_keys[0].values[1], + Value::String("second".into()) + ); } #[test]