Skip to content

feat(rules): test-code awareness for the built-in rules (#663) - #666

Merged
ajianaz merged 4 commits into
developfrom
feat/663-test-code-awareness
Oct 16, 2026
Merged

ajianaz merged 4 commits into
developfrom
feat/663-test-code-awareness

Conversation

@ajianaz

@ajianaz ajianaz commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #663.

What

New engine::test_context says which lines are test code: whole test files by path (*_test.*, *.test.ts, spec/, __tests__/, fixtures/, ...), Rust #[cfg(test)] modules and #[test] functions (brace matching that ignores braces in strings, chars and comments), and, in a diff, a hunk whose header is mod tests. ScanFile carries it (in_test), so every scanner sees the same answer.

  • Built-in rules bug-unwrap, bug-expect, bug-println, qual-clone, qual-error-ignore, sec-hardcoded-url, sec-hardcoded-secret skip test code.
  • security_scanner / secrets_scanner run only the high-confidence secret list on inline test blocks (same as they already did for test files), so a real AWS key in a test is still reported.
  • bug-println is also skipped in main.rs, src/bin/, examples/, benches/.
  • rules_engine.include_tests: true turns the new exemptions off. Detection is conservative: when it cannot tell, the line stays scanned. Custom rules and bug-todo are not exempt.
  • deterministic::run now builds the ScanFiles once and calls the _in scanners.

Why

Issue #663. Tests produced most of the noise and the remaining false Critical/Major findings on real code.

Testing

  • cargo fmt --check, cargo clippy --all-targets --features tree-sitter -- -D warnings, cargo test --features tree-sitter (1245 passed; 17 new: brace matching incl. strings/chars/comments, attribute stacks, numbering gaps, hunk headers, rule exemptions, include_tests, high-confidence secret still found in a test block).
  • Real build on uteke and uteke-cloud, cap off, include_tests: true (old behavior) vs default:
Module Total before → after Critical before → after
uteke-core 2400 → 235 6 → 0
uteke-cli 902 → 806 5 → 0
uteke-mcp 65 → 0 0 → 0
uteke-server 250 → 80 0 → 0
uteke-cloud 218 → 170 2 Crit + 3 Major → 1 Crit (placeholder in a shell-script comment)

Known limits

  • bug-println in a CLI crate's commands/ modules is still reported (the rule layer does not know the crate type); silence with ignore.rules.
  • For a diff, the exemption only applies when the hunk shows the #[cfg(test)] block or a mod tests header; otherwise the line is scanned.
  • Only Rust has inline test-block detection; other languages are covered by path.

ajianaz and others added 4 commits October 15, 2026 16:04
Whole test files and Rust #[cfg(test)] modules / #[test] functions (also a
diff hunk inside mod tests) are exempt from the noisy rules; secret
scanners keep only their high-confidence patterns there. bug-println is
skipped in program entry points. rules_engine.include_tests turns it off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: ajianaz <ajianaz@users.noreply.github.com>
…ection in test blocks)

An unclosed #[cfg(test)] region in a whole file is dropped instead of
exempting the rest of the file, single-line raw strings no longer confuse
brace matching, and inline test blocks keep every security pattern except
the generic hardcoded-secret one.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: ajianaz <ajianaz@users.noreply.github.com>
… strings

Block comments are skipped when counting braces, and a test region is
dropped when an item starts at or left of its own indentation or its
closing brace does not balance the count, so a parser differential cannot
exempt production code. Column-0 text that is not a Rust item (TOML
fixtures in raw strings) does not abandon a region.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: ajianaz <ajianaz@users.noreply.github.com>
… cannot exempt code

#[cfg(test)] fn helper() {} decorated the next line instead of its own
item, and an attribute inside a block comment counted as one. Comment
state is now tracked on every line so it cannot go stale.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: ajianaz <ajianaz@users.noreply.github.com>
@ajianaz
ajianaz merged commit c7be190 into develop Oct 16, 2026
14 checks passed
@ajianaz
ajianaz deleted the feat/663-test-code-awareness branch October 16, 2026 00:11
@ajianaz ajianaz mentioned this pull request Oct 16, 2026
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.

feat(rules): test-code awareness (#[cfg(test)], fixtures, CLI binaries) for built-in rules

1 participant