Commit a63ff23
authored
fix: retune precision false positives (#81)
## Summary
This PR retunes CodeGuard's local quality precision checks to reduce
high-volume false positives from common TypeScript/JavaScript, React,
Next.js, and API-boundary patterns while preserving the intended
production-readiness signals.
### What changed
- Hardened `function.hidden-mutation` so local scratch mutation in pure
builders/parsers is not treated as hidden mutation.
- Allows local `Set.add`, `Map.set`, `array.push`, `.sort`,
`.split().pop()`, object accumulation, and Cheerio cleanup when the
function returns a derived value.
- Keeps the rule focused on mutation of arguments, module/global state,
collaborators, React state, DOM, DB, filesystem, or other external
resources.
- Retuned `function.command-query-mix` for query-safe builder/parser
patterns.
- Pure builders/parsers that mutate local scratch state no longer look
like command/query violations.
- Command functions that intentionally write and return useful results
are treated as normal API/application behavior.
- Improved boundary/resource validation recognition.
- Credits `z.safeParse`, helper-returned validated values, NextResponse
early-return guards, `new URL(...)` plus protocol allowlists, shared
upload validators, and imported byte-limit constants.
- Credits `Content-Length` preflight, explicit `take`/`limit`, `slice`,
count/size/byte guards, and bounded response byte-length checks for
`defensive.missing-resource-limit`.
- Split sequence-allocation risk from arithmetic overflow.
- Added `defensive.sequence-collision-risk` for count-derived external
ID allocation without guarded unique-collision retry.
- Keeps guarded Prisma/P2002 retry loops out of
`defensive.integer-overflow`.
- Suppresses metric/stat arithmetic from overflow findings.
- Reduced naming and smell noise.
- Allows common UI boolean/domain names such as `show*`, `matches*`,
`visible`, `active`, `open`, `selected`, `enabled`.
- Allows collection/domain abbreviations such as `krs`, `docs`, and
`ids`.
- Tolerates centralized enum/domain dispatch maps for
`smell.switch-on-type`.
- Added rule metadata and documentation.
- Registered `defensive.sequence-collision-risk` in the rule catalog and
fix-template catalog.
- Updated `docs/checks.md` so users can discover the new defensive rule.
### Tests
- Added regression coverage for local builder/parser mutation patterns.
- Added regression coverage for boundary validation and resource-limit
proof patterns.
- Added regression coverage for sequence-collision vs integer-overflow
behavior.
- Updated existing UI false-positive hardening tests to match the
narrower analyzer behavior.
## Validation
- `go test ./tests/checks -run
'TestDefensive(Integer|Resource)|TestRulesCatalog' -count=1`
- `go test ./tests/checks -count=1`
- `go test ./...`
- `golangci-lint run`
- `make codeguard-ci`12 files changed
Lines changed: 622 additions & 199 deletions
File tree
- docs
- internal/codeguard
- checks/quality
- rules
- tests/checks
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1186 | 1186 | | |
1187 | 1187 | | |
1188 | 1188 | | |
| 1189 | + | |
1189 | 1190 | | |
1190 | 1191 | | |
1191 | 1192 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| 16 | + | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| |||
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
38 | 43 | | |
39 | 44 | | |
40 | 45 | | |
| |||
53 | 58 | | |
54 | 59 | | |
55 | 60 | | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
56 | 65 | | |
57 | 66 | | |
58 | 67 | | |
| |||
130 | 139 | | |
131 | 140 | | |
132 | 141 | | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
133 | 145 | | |
134 | 146 | | |
135 | 147 | | |
| |||
149 | 161 | | |
150 | 162 | | |
151 | 163 | | |
152 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
153 | 171 | | |
154 | 172 | | |
155 | 173 | | |
| |||
158 | 176 | | |
159 | 177 | | |
160 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
161 | 189 | | |
162 | 190 | | |
163 | 191 | | |
| |||
205 | 233 | | |
206 | 234 | | |
207 | 235 | | |
208 | | - | |
| 236 | + | |
209 | 237 | | |
210 | 238 | | |
211 | 239 | | |
| |||
220 | 248 | | |
221 | 249 | | |
222 | 250 | | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
223 | 275 | | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
224 | 280 | | |
225 | 281 | | |
226 | 282 | | |
| |||
230 | 286 | | |
231 | 287 | | |
232 | 288 | | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
233 | 308 | | |
234 | 309 | | |
235 | 310 | | |
| |||
333 | 408 | | |
334 | 409 | | |
335 | 410 | | |
336 | | - | |
| 411 | + | |
337 | 412 | | |
338 | 413 | | |
339 | 414 | | |
| |||
343 | 418 | | |
344 | 419 | | |
345 | 420 | | |
346 | | - | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
347 | 425 | | |
348 | 426 | | |
349 | 427 | | |
| |||
352 | 430 | | |
353 | 431 | | |
354 | 432 | | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
355 | 448 | | |
356 | 449 | | |
357 | 450 | | |
| |||
Lines changed: 14 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | | - | |
| 13 | + | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| |||
133 | 133 | | |
134 | 134 | | |
135 | 135 | | |
136 | | - | |
| 136 | + | |
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
149 | | - | |
| 149 | + | |
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| |||
178 | 178 | | |
179 | 179 | | |
180 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
181 | 184 | | |
182 | 185 | | |
183 | 186 | | |
| |||
188 | 191 | | |
189 | 192 | | |
190 | 193 | | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
191 | 199 | | |
192 | 200 | | |
193 | 201 | | |
| |||
202 | 210 | | |
203 | 211 | | |
204 | 212 | | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
205 | 216 | | |
206 | 217 | | |
207 | 218 | | |
| |||
Lines changed: 49 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
Lines changed: 8 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
161 | 161 | | |
162 | 162 | | |
163 | 163 | | |
164 | | - | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
165 | 171 | | |
166 | 172 | | |
167 | 173 | | |
| |||
191 | 197 | | |
192 | 198 | | |
193 | 199 | | |
194 | | - | |
| 200 | + | |
195 | 201 | | |
196 | 202 | | |
197 | 203 | | |
| |||
0 commit comments