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`.
This commit is contained in:
dailz
2026-06-22 15:02:33 +08:00
parent e69b7af32a
commit 95f259a2bb
2 changed files with 20 additions and 8 deletions
+4 -2
View File
@@ -158,7 +158,7 @@ impl LineIndex {
// If the junction falls on a block boundary, record the start offset // If the junction falls on a block boundary, record the start offset
// (analogous to from_bytes always pushing offset 0 for line 0). // (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); self.sampled_offsets.push(start_offset);
} }
@@ -212,15 +212,17 @@ impl LineIndex {
self.total_lines as usize self.total_lines as usize
} }
// ─── getter 方法 ──────────────────────────────────────────────────── #[cfg(test)]
pub(crate) fn sampled_offsets(&self) -> &[u64] { pub(crate) fn sampled_offsets(&self) -> &[u64] {
&self.sampled_offsets &self.sampled_offsets
} }
#[cfg(test)]
pub(crate) fn total_lines(&self) -> u64 { pub(crate) fn total_lines(&self) -> u64 {
self.total_lines self.total_lines
} }
#[cfg(test)]
pub(crate) fn has_trailing_newline(&self) -> bool { pub(crate) fn has_trailing_newline(&self) -> bool {
self.has_trailing_newline self.has_trailing_newline
} }
+16 -6
View File
@@ -22,8 +22,8 @@ use std::collections::{HashMap, HashSet};
// 默认的 serde_json::from_str::<HashMap<_, _>>() 遇到重复键时会采用"后者覆盖前者"last-wins), // 默认的 serde_json::from_str::<HashMap<_, _>>() 遇到重复键时会采用"后者覆盖前者"last-wins),
// 前面的值被静默丢弃。这里我们通过自定义 Visitor 在反序列化过程中逐个观察 key-value 对, // 前面的值被静默丢弃。这里我们通过自定义 Visitor 在反序列化过程中逐个观察 key-value 对,
// 在保持 last-wins 行为的同时,将重复 key 的所有值记录到 DuplicateKey 中。 // 在保持 last-wins 行为的同时,将重复 key 的所有值记录到 DuplicateKey 中。
use serde::de::{MapAccess, Visitor};
use serde::Deserializer; use serde::Deserializer;
use serde::de::{MapAccess, Visitor};
// serde_json::Value — 来自 serde_json 库(Rust 中最流行的 JSON 处理库)。 // serde_json::Value — 来自 serde_json 库(Rust 中最流行的 JSON 处理库)。
// Value 是一个枚举类型,可以表示任意 JSON 值: // Value 是一个枚举类型,可以表示任意 JSON 值:
@@ -83,7 +83,10 @@ pub fn detect_json_log(line: &str) -> bool {
// 则匹配成功。_ 是通配符,表示"不关心对象里面的具体内容"。 // 则匹配成功。_ 是通配符,表示"不关心对象里面的具体内容"。
// //
// 如果匹配到 Ok(Value::Object(_)) 返回 true,否则返回 false。 // 如果匹配到 Ok(Value::Object(_)) 返回 true,否则返回 false。
matches!(serde_json::from_str::<Value>(strip_bom(line)), Ok(Value::Object(_))) matches!(
serde_json::from_str::<Value>(strip_bom(line)),
Ok(Value::Object(_))
)
} }
// ─── DuplicateKeyVisitor ────────────────────────────────────────────────── // ─── DuplicateKeyVisitor ──────────────────────────────────────────────────
@@ -147,7 +150,7 @@ fn parse_json_object_with_duplicates(
json: &str, json: &str,
) -> Option<(serde_json::Map<String, Value>, Vec<DuplicateKey>)> { ) -> Option<(serde_json::Map<String, Value>, Vec<DuplicateKey>)> {
let mut deserializer = serde_json::Deserializer::from_str(json); 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 辅助函数 ────────────────────────────────── // ─── take_string_field_from_map 辅助函数 ──────────────────────────────────
@@ -189,7 +192,8 @@ pub fn parse_line(line: &str) -> Option<LogEntry> {
let (mut obj, duplicate_keys) = parse_json_object_with_duplicates(line)?; let (mut obj, duplicate_keys) = parse_json_object_with_duplicates(line)?;
let raw_line = line.to_string(); 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"]) let level = take_string_field_from_map(&mut obj, &["level", "lvl", "severity"])
.map(|s| s.parse::<LogLevel>().unwrap_or_else(|e| match e {})); .map(|s| s.parse::<LogLevel>().unwrap_or_else(|e| match e {}));
@@ -513,8 +517,14 @@ mod tests {
assert_eq!(entry.duplicate_keys.len(), 1); assert_eq!(entry.duplicate_keys.len(), 1);
assert_eq!(entry.duplicate_keys[0].key, "message"); assert_eq!(entry.duplicate_keys[0].key, "message");
assert_eq!(entry.duplicate_keys[0].values.len(), 2); assert_eq!(entry.duplicate_keys[0].values.len(), 2);
assert_eq!(entry.duplicate_keys[0].values[0], Value::String("first".into())); assert_eq!(
assert_eq!(entry.duplicate_keys[0].values[1], Value::String("second".into())); entry.duplicate_keys[0].values[0],
Value::String("first".into())
);
assert_eq!(
entry.duplicate_keys[0].values[1],
Value::String("second".into())
);
} }
#[test] #[test]