fix(dead_code): 清理假阳性与真死代码
Some checks failed
CI / build (push) Has been cancelled
CI / check (push) Has been cancelled

- settings.rs: retention 常量与 clamp_retention 仅服务端使用,加 #[cfg(feature = "server")]
- comment.rs: 删除未使用的 Comment 结构体
- cache.rs: 删除未使用的 invalidate_all_comment_caches 及其测试

验证: cargo check (server/wasm), cargo test (311 passed), dx check, cargo clippy
This commit is contained in:
xfy 2026-06-16 17:48:47 +08:00
parent 7d713cabc2
commit 2caca3a48d
3 changed files with 7 additions and 64 deletions

View File

@ -389,15 +389,6 @@ pub async fn invalidate_pending_count() {
.await;
}
/// 清空所有评论相关缓存。
#[cfg(feature = "server")]
#[allow(dead_code)]
pub async fn invalidate_all_comment_caches() {
COMMENT_CACHE.invalidate_all();
COMMENT_COUNT_CACHE.invalidate_all();
PENDING_COUNT_CACHE.invalidate_all();
}
#[cfg(all(test, feature = "server"))]
mod tests {
use super::*;
@ -613,17 +604,4 @@ mod tests {
assert!(get_pending_count().await.is_none());
}
#[tokio::test]
#[serial]
async fn invalidate_all_comment_caches_clears_everything() {
set_comments_by_post(1, vec![]).await;
set_comment_count(1, 10).await;
set_pending_count(5).await;
invalidate_all_comment_caches().await;
assert!(get_comments_by_post(1).await.is_none());
assert!(get_comment_count(1).await.is_none());
assert!(get_pending_count().await.is_none());
}
}

View File

@ -44,48 +44,6 @@ impl CommentStatus {
}
}
/// 服务端内部使用的完整评论结构体,仅在启用 server feature 时编译。
///
/// 包含作者邮箱、IP、User-Agent 等敏感或管理字段,不直接返回给前端。
#[cfg(feature = "server")]
#[allow(dead_code)]
pub struct Comment {
/// 评论主键。
pub id: i64,
/// 所属文章主键。
pub post_id: i32,
/// 父评论主键,用于实现嵌套回复。
pub parent_id: Option<i64>,
/// 嵌套深度0 表示顶层评论。
pub depth: i32,
/// 评论者名称。
pub author_name: String,
/// 评论者邮箱,用于 Gravatar 与后台联系。
pub author_email: String,
/// 评论者个人主页 URL。
pub author_url: Option<String>,
/// 原始 Markdown 内容。
pub content_md: String,
/// 渲染后的 HTML 内容。
pub content_html: Option<String>,
/// 内容哈希,用于检测重复或垃圾评论。
pub content_hash: Option<String>,
/// 当前审核状态。
pub status: CommentStatus,
/// 评论者 IP 地址。
pub ip_address: Option<String>,
/// 评论者浏览器 User-Agent。
pub user_agent: Option<String>,
/// 审核通过时间。
pub approved_at: Option<DateTime<Utc>>,
/// 评论创建时间。
pub created_at: DateTime<Utc>,
/// 评论最后更新时间。
pub updated_at: DateTime<Utc>,
/// 软删除时间。
pub deleted_at: Option<DateTime<Utc>>,
}
/// 面向前端展示的评论结构体,已脱敏。
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PublicComment {

View File

@ -5,8 +5,10 @@ pub const DEFAULT_RETENTION_DAYS: i32 = 30;
/// 默认不启用自动清理。
pub const DEFAULT_AUTO_PURGE_ENABLED: bool = false;
/// 保留天数下限(天)。
#[cfg(feature = "server")]
pub const MIN_RETENTION_DAYS: i32 = 1;
/// 保留天数上限(天)。防止误填超大值导致永不清理。
#[cfg(feature = "server")]
pub const MAX_RETENTION_DAYS: i32 = 365;
/// 回收站配置。
@ -29,6 +31,7 @@ impl Default for TrashSettings {
impl TrashSettings {
/// 将保留天数钳制到合法范围 [MIN, MAX]。
#[cfg(feature = "server")]
pub fn clamp_retention(days: i32) -> i32 {
days.clamp(MIN_RETENTION_DAYS, MAX_RETENTION_DAYS)
}
@ -46,24 +49,28 @@ mod tests {
}
#[test]
#[cfg(feature = "server")]
fn clamp_retention_keeps_valid() {
assert_eq!(TrashSettings::clamp_retention(7), 7);
assert_eq!(TrashSettings::clamp_retention(30), 30);
}
#[test]
#[cfg(feature = "server")]
fn clamp_retention_clamps_below_min() {
assert_eq!(TrashSettings::clamp_retention(0), MIN_RETENTION_DAYS);
assert_eq!(TrashSettings::clamp_retention(-5), MIN_RETENTION_DAYS);
}
#[test]
#[cfg(feature = "server")]
fn clamp_retention_clamps_above_max() {
assert_eq!(TrashSettings::clamp_retention(366), MAX_RETENTION_DAYS);
assert_eq!(TrashSettings::clamp_retention(i32::MAX), MAX_RETENTION_DAYS);
}
#[test]
#[cfg(feature = "server")]
fn clamp_retention_boundary() {
assert_eq!(TrashSettings::clamp_retention(MIN_RETENTION_DAYS), MIN_RETENTION_DAYS);
assert_eq!(TrashSettings::clamp_retention(MAX_RETENTION_DAYS), MAX_RETENTION_DAYS);