Skip to content

build(lint): deny tests outside test modules - #21

Merged
furedea merged 2 commits into
mainfrom
build/deny-tests-outside-test-module
Nov 15, 2026
Merged

furedea merged 2 commits into
mainfrom
build/deny-tests-outside-test-module

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

概要

置き換える ast-grep 規則

agent-config で安定して動かせなかった,#[test] 関数がテストモジュール外にあることを検出するブロッキング規則を,この clippy lint で置き換える.

既知の誤検知と対処

ピン留めされた clippy 0.1.95 では,#[cfg(all(unix, test))] mod tests { #[test] fn t() {} } のような複合 cfg のテストモジュール内のテストも報告される(通常の #[cfg(test)] mod tests は報告されない).このような複合 cfg のテストモジュールは稀なので,lint を外さず,レビューされた抑制コメントで例外を扱う方針とする.該当するモジュールには,理由付きで次のように書く.テンプレートは allow_attributes_without_reason により理由を必須にしている.

#[expect(clippy::tests_outside_test_module, reason = "compound cfg test module is a known false positive")]

検証

使い捨ての src/bin/throwaway.rs で cargo clippy --all-targets を実行した(確認後に削除済み).

  • #[cfg(test)] 外の #[test]: 警告される.
  • #[cfg(all(unix, test))] mod tests: 警告される(上記の既知の誤検知).
  • 既存の #[cfg(test)] mod tests(src/main.rs): 警告されない.

テンプレートの検査コマンド cargo fmt --check,cargo clippy --all-targets -- -D warnings,cargo test はすべて成功した.

計画からの重要な変更: 複合 cfg が報告されない前提は成り立たなかったため,上記の抑制方針を明記した.

🤖 Generated with Claude Code

Enable clippy::tests_outside_test_module as a warning so CI (-D warnings)
rejects #[test] functions outside a #[cfg(test)] module.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 15, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T13:18:13.333348Z 4e314b5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread Cargo.toml Resolved
@github-actions

Copy link
Copy Markdown

Review summary

1 blocking finding (P1)

  • P1, Cargo.toml:25: CI (.github/workflows/ci.yml:43) and lefthook (lefthook.yml:23) run cargo clippy -- -D warnings with no --all-targets. In a build that isn't a test build, rustc removes #[test] functions before clippy runs, so tests_outside_test_module never fires in CI or in the pre-commit hook. The PR's local check used --all-targets, which is not the command CI runs. This means the lint doesn't yet replace the blocking ast-grep rule. To fix it, add --all-targets to the CI clippy step, and ideally to the lefthook command too.

The lint itself and the documented #[expect(..., reason = ...)] approach for compound-cfg test modules look fine.

I couldn't run cargo in the review sandbox, so this conclusion comes from reading the CI and hook commands and from how rustc's #[test] expansion works.

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1c0b5b75aa

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread Cargo.toml Resolved
#[test] functions are compiled out of non-test builds, so
tests_outside_test_module never fired under plain `cargo clippy`.
Pass --all-targets in CI and in the pre-commit hook.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
furedea
furedea previously approved these changes Nov 15, 2026
@furedea

furedea commented Nov 15, 2026

Copy link
Copy Markdown
Owner

@codex review

@github-actions

Copy link
Copy Markdown

Review summary

I found no blocking issues.

  • Adding clippy::tests_outside_test_module = "warn" and switching CI and lefthook to cargo clippy --all-targets -- -D warnings together make clippy check test code, and the lint does what the PR says.
  • Turning on --all-targets also runs the existing deny lints (unwrap_used, expect_used, indexing_slicing) on test code. clippy.toml already sets allow-*-in-tests = true, so ordinary test code won't start failing.
  • The only test in the template, in src/main.rs, sits inside a plain #[cfg(test)] mod tests, so the lint doesn't flag it.
  • The PR documents the known false positive on compound-cfg test modules and how to suppress it.

I didn't run clippy myself: running commands needed approval in this environment.

No inline findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 4e314b51d3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@furedea
furedea merged commit 146ae50 into main Nov 15, 2026
18 checks passed
@furedea
furedea deleted the build/deny-tests-outside-test-module branch November 15, 2026 13:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant