Skip to content

feat: add 12 new detection categories and auto-fix mode#2

Merged
cschuman merged 2 commits into
mainfrom
feat/session-20260105-211955
Jan 6, 2026
Merged

feat: add 12 new detection categories and auto-fix mode#2
cschuman merged 2 commits into
mainfrom
feat/session-20260105-211955

Conversation

@cschuman

@cschuman cschuman commented Jan 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • Adds 27 new detection rules across 12 categories
  • Implements auto-fix mode with --fix and --dry-run flags
  • Adds unified diff output format

New Detection Categories

Category Rules Description
memory 3 pprof in hot paths, large struct copying, heap escapes
context 3 context.Background in handlers, missing timeouts, context leaks
database 2 missing pool config, unlimited connections
errors 2 fmt.Errorf in loops, error wrapping overhead
time 3 time.Parse/LoadLocation/Format in loops
interface 3 interface{} boxing, variadic interface allocation
http 3 missing MaxBytesReader, Body.Close, response buffering
cache 2 regexp.MatchString in loops, JSON schema in loops
benchmark 1 suggests benchmarks for perf-sensitive functions

Auto-Fix Mode

goperf --fix --dry-run ./...  # Preview fixes
goperf --fix ./...            # Apply fixes
goperf --format=diff ./...    # Unified diff output

Auto-fixable rules:

  • missing-body-closedefer resp.Body.Close()
  • context-leakdefer cancel()

Test plan

  • Built and tested against test file (20 issues detected)
  • Tested against goperf itself (131 issues detected)
  • Verified --fix --dry-run mode
  • Verified --format=diff output

Implements 8 major improvements for reducing false positives:

1. **Prepared statement detection** - Skips stmt.Exec() when using
   db.Prepare() pattern (idiomatic Go batch pattern)

2. **Transaction context awareness** - Lower severity for SQL in
   transactions since they batch at commit time

3. **Preallocation detection** - Tracks make([]T, 0, cap) to skip
   false positives for properly preallocated slices

4. **Bounded loop severity** - Small bounded loops (≤10 iterations)
   get lower severity since impact is limited

5. **Map-based optimization recognition** - Detects when nested loops
   use map lookups (O(1)) instead of linear search

6. **Intentional unbuffered channel detection** - Skips channels in
   select statements, signal patterns (done, quit, cancel), and
   chan struct{} (intentional synchronization)

7. **Call graph analysis** - New IndirectSQLInLoopRule detects when
   helper functions containing SQL are called in loops

8. **JSON encoder detection** - Recognizes json.Encoder/Decoder which
   cache reflection, vs json.Marshal/Unmarshal which don't

New rules added:
- indirect-sql-in-loop: Detect SQL in helper functions called from loops
- reflection-in-loop: Detect reflect.ValueOf/TypeOf in loops
- sync-pool-opportunity: Suggest sync.Pool for buffer allocations

All rules now have context-aware severity adjustment.
New detection rules:
- memory: pprof in hot paths, large struct copying, heap escape detection
- context: context.Background in handlers, missing timeouts, context leaks
- database: missing connection pool config, unlimited connections (SetMaxOpenConns(0))
- errors: fmt.Errorf in loops, error wrapping overhead
- time: time.Parse/LoadLocation/Format in loops
- interface: interface{} boxing in loops, variadic interface allocation
- http: missing MaxBytesReader, missing Body.Close, response buffering
- cache: regexp.MatchString in loops, JSON schema in loops

Enhanced existing rules:
- Expanded regex detection to catch MatchString/Match/ReplaceAll in loops

Auto-fix mode:
- New --fix flag to apply automatic fixes
- New --dry-run flag to preview fixes without applying
- New --format=diff for unified diff output
- Auto-fix support for: missing-body-close, context-leak
- Suggestion-only fixes for other rules

Benchmark suggestions:
- Detects functions with performance-sensitive patterns
- Generates benchmark scaffolding code

Total: 27 new detection rules across 12 categories
@cschuman cschuman merged commit d382cdf into main Jan 6, 2026
1 of 5 checks passed
@cschuman cschuman deleted the feat/session-20260105-211955 branch January 6, 2026 02:39
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