Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .claude/custom-lint-rules.toml
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,40 @@ steps = [
[rules.example]
bad = 'BLOCK_MESSAGE: &str = "詳細: docs/todoN.md (N = 数字) の \"<section>\" 参照";'
good = 'BLOCK_MESSAGE: &str = "詳細: docs/adr/adr-NNN-feature.md 参照";'

# ─── ルール⑦: Rust 時刻フィールドの strict `>` 比較禁止 (boundary inconsistency 防止) ───
#
# 由来: PR #101 で `parse_listed_findings` の `c.created_at > push_time` が CodeRabbit
# Minor 指摘 → 同一ファイル内 3 関数 (parse_listed_findings / parse_new_comments /
# parse_findings) で `>` vs `>=` の latent drift が判明 (parse_rate_limit のみ既に `>=`)。
# 時刻フィールドの strict `>` は境界 (push_time と同時刻のイベント) を silently 除外
# するため、`>=` への揃えを構造的に強制する。
#
# 由来: PR #101 post-merge-feedback Tier 1 #2 採用、Bundle Z #B-α と同じ
# 「決定論的防止層」哲学 (ADR-007 の正規表現層)。
#
# Direction-specific 設計 (`>` のみ flag):
# `<` の strict 比較は別文脈 (例: is_stale の `created_at < threshold` =
# TTL 内/外判定) で意図的な exclusive 境界として正しいケースが多いため
# false positive 回避のため scope を `>` に限定。`<` 側の boundary 問題は
# 観測されたら別 rule で対応する。

[[rules]]
id = "no-time-field-strict-greater"
pattern = '\b(created_at|submitted_at|updated_at|comment_event_time|event_time|comment_created_at|published_at|posted_at|commented_at)\s*>\s*[a-zA-Z_]'
severity = "warning"
message = "時刻フィールドの strict `>` 比較を検出。境界 (同時刻イベント) を silently 除外するため `>=` 比較への揃えを推奨"
why = "PR #101 で同一ファイル内 3 関数で `>` vs `>=` の latent drift が判明 (parse_listed_findings の `c.created_at > push_time` が CR Minor 指摘 → 揃え修正)。時刻フィールドの strict inequality は境界の inclusive/exclusive choice を不可視にし、bug class を生む。`>=` への揃えで boundary を明示する"
extensions = ["rs"]

[rules.fix]
strategy = "`>=` への置き換え (境界 inclusive)"
steps = [
"`field > rhs` を `field >= rhs` に変更 (同時刻のイベントを含める)",
"意図的に exclusive にする場合: 比較対象を 1 単位ずらす (`field >= rhs.checked_add(1)?` 等) かコメントで意図を明示",
"false positive (時刻でない `created_at` 等) の場合: 変数名を時刻と紛らわしくない名前にリネーム",
]

[rules.example]
bad = "comments.iter().filter(|c| c.created_at > push_time)"
good = "comments.iter().filter(|c| c.created_at >= push_time)"
164 changes: 163 additions & 1 deletion src/hooks-post-tool-linter/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,6 @@ extensions = ["ts", "js"]

#[test]
fn md_mutable_anchor_skips_external_url_with_fragment() {
// 外部 URL の fragment は GFM anchor ではないため誤検知すべきでない
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
Expand All @@ -1357,4 +1356,167 @@ extensions = ["ts", "js"]
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

fn rs_time_field_strict_greater_rule() -> CustomRule {
make_test_rule(
"no-time-field-strict-greater",
r"\b(created_at|submitted_at|updated_at|comment_event_time|event_time|comment_created_at|published_at|posted_at|commented_at)\s*>\s*[a-zA-Z_]",
&["rs"],
)
}

fn build_rs_source_with_op(field_lhs: &str, op: &str, rhs: &str) -> String {
format!("fn f() {{ items.iter().filter(|c| c.{field_lhs} {op} {rhs}); }}\n")
}

fn build_doc_comment_source(field_lhs: &str, op: &str, rhs: &str) -> String {
format!("/// `{field_lhs} {op} {rhs}` (epoch 0 で実質全件)\nfn f() {{}}\n")
}

fn build_toml_with_field(field_lhs: &str, op: &str, rhs: &str) -> String {
format!("comment = \"{field_lhs} {op} {rhs}\"\n")
}

#[test]
fn rs_time_field_strict_greater_detects_created_at_gt_push_time() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("created_at", ">", "push_time"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert_eq!(violations.len(), 1);
}

#[test]
fn rs_time_field_strict_greater_detects_submitted_at_gt_since() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("submitted_at", ">", "since"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert_eq!(violations.len(), 1);
}

#[test]
fn rs_time_field_strict_greater_detects_updated_at_gt_threshold() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("updated_at", ">", "threshold"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert_eq!(violations.len(), 1);
}

#[test]
fn rs_time_field_strict_greater_detects_comment_event_time() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("comment_event_time", ">", "now"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert_eq!(violations.len(), 1);
}

#[test]
fn rs_time_field_strict_greater_skips_inclusive_comparison() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("created_at", ">=", "push_time"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_skips_strict_less_than() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"stale.rs",
&build_rs_source_with_op("created_at", "<", "threshold"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_skips_le_inclusive() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("created_at", "<=", "cutoff"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_skips_numeric_rhs() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("created_at", ">", "0"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_skips_doc_comment_with_inclusive() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"doc.rs",
&build_doc_comment_source("created_at", ">=", "push_time"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_skips_unrelated_field() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"parse.rs",
&build_rs_source_with_op("count", ">", "limit"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}

#[test]
fn rs_time_field_strict_greater_only_targets_rs() {
let dir = tempfile::tempdir().unwrap();
let file = write_file(
dir.path(),
"config.toml",
&build_toml_with_field("created_at", ">", "push_time"),
);
let rules = compile_test_rules(vec![rs_time_field_strict_greater_rule()]);
let violations = run_custom_rules(file.to_str().unwrap(), &rules);
assert!(violations.is_empty());
}
}