gruff-rs 0.1.1

Rust static analyzer and quality linter for CI: dead-code, complexity, security, secrets, and architecture rules with deterministic SARIF/JSON output and baseline support.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use super::*;

pub(crate) const METADATA_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "modernisation.public-field",
        "Public struct field",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags public struct fields that expose representation.",
    ),
    rule_definition!(
        "modernisation.manual-is-empty",
        "Manual is_empty check",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags `len() == 0` and `len() != 0` comparisons that should use `is_empty()`.",
    ),
    rule_definition!(
        "modernisation.manual-contains",
        "Manual contains check",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags `iter().any(|x| *x == y)` patterns that should use `.contains(&y)`.",
    ),
    rule_definition!(
        "modernisation.manual-strip-prefix",
        "Manual strip_prefix",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags `if s.starts_with(p) { &s[p.len()..] }` shapes that should use `strip_prefix`.",
    ),
    rule_definition!(
        "modernisation.manual-unwrap-or-default",
        "Manual unwrap_or_default",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags `match opt { Some(v) => v, None => Default::default() }` shapes that should use `unwrap_or_default()`.",
    ),
    rule_definition!(
        "modernisation.question-mark-candidate",
        "Manual question-mark candidate",
        Pillar::Modernisation,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags manual `match`/`if let Err` Result-propagation shapes that should use `?`.",
    ),
    rule_definition!(
        "metrics.halstead-volume",
        "Halstead-style volume",
        Pillar::Complexity,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        METRICS_HALSTEAD_VOLUME_THRESHOLD,
        "Flags functions whose deterministic token volume exceeds the configured threshold.",
    ),
    rule_definition!(
        "metrics.maintainability-pressure",
        "Maintainability pressure",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        METRICS_MAINTAINABILITY_PRESSURE_THRESHOLD,
        "Flags functions whose maintainability pressure score falls below the configured minimum.",
    ),
];

const NAMING_GENERIC_FUNCTION_OPTIONS: &[OptionDefinition] = &[OptionDefinition {
    name: "extraGenericNames",
    description: "Additional generic function names rejected by naming.generic-function.",
    value_kind: OptionValueKind::StringArray,
}];

const NAMING_BOOLEAN_PREFIX_OPTIONS: &[OptionDefinition] = &[OptionDefinition {
    name: "predicatePrefixes",
    description: "Additional predicate prefixes accepted by naming.boolean-prefix.",
    value_kind: OptionValueKind::StringArray,
}];

const NAMING_PLACEHOLDER_OPTIONS: &[OptionDefinition] = &[OptionDefinition {
    name: "extraPlaceholders",
    description: "Additional placeholder identifiers rejected by naming.placeholder-identifier.",
    value_kind: OptionValueKind::StringArray,
}];

pub(crate) const NAMING_RULES: &[RuleDefinition] = &[
    RuleDefinition {
        id: "naming.generic-function",
        name: "Generic function name",
        pillar: Pillar::Naming,
        tier: "v0.1",
        kind: RuleKind::Rust,
        default_severity: Severity::Advisory,
        confidence: Confidence::High,
        threshold: None,
        options: NAMING_GENERIC_FUNCTION_OPTIONS,
        default_enabled: true,
        description: "Flags function names that are too generic to explain intent.",
    },
    RuleDefinition {
        id: "naming.boolean-prefix",
        name: "Boolean predicate prefix",
        pillar: Pillar::Naming,
        tier: "v0.1",
        kind: RuleKind::Rust,
        default_severity: Severity::Advisory,
        confidence: Confidence::High,
        threshold: None,
        options: NAMING_BOOLEAN_PREFIX_OPTIONS,
        default_enabled: true,
        description: "Flags bool-returning functions whose names do not read like predicates.",
    },
    RuleDefinition {
        id: "naming.placeholder-identifier",
        name: "Placeholder identifier",
        pillar: Pillar::Naming,
        tier: "v0.1",
        kind: RuleKind::Rust,
        default_severity: Severity::Advisory,
        confidence: Confidence::Medium,
        threshold: None,
        options: NAMING_PLACEHOLDER_OPTIONS,
        default_enabled: true,
        description: "Flags placeholder identifiers such as foo, bar, baz, and qux.",
    },
    rule_definition!(
        "naming.short-variable",
        "Short variable name",
        Pillar::Naming,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags very short local variable names outside accepted abbreviations.",
    ),
    rule_definition!(
        "naming.identifier-shadow",
        "Identifier shadow",
        Pillar::Naming,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags `let X = X(...)` bindings that shadow a same-file free function.",
    ),
];

pub(crate) const PERFORMANCE_AND_SECURITY_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "ci.github-event-shell-interpolation",
        "GitHub event shell interpolation",
        Pillar::Security,
        RuleKind::Text,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags GitHub event values interpolated directly into workflow shell steps.",
    ),
    rule_definition!(
        "performance.clone-in-loop",
        "Clone in loop",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags clone calls inside loop bodies as allocation hot spot candidates.",
    ),
    rule_definition!(
        "performance.format-in-loop",
        "Format in loop",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::Medium,
        None,
        "Flags format! calls inside loop bodies as allocation hot spot candidates.",
    ),
    rule_definition!(
        "performance.regex-in-loop",
        "Regex construction in loop",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags Regex::new calls inside loop bodies.",
    ),
    rule_definition!(
        "security.process-command",
        "Process command execution",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags process command construction for manual argument validation.",
    ),
    rule_definition!(
        "security.insecure-rng-for-secrets",
        "Insecure RNG for secret material",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::Medium,
        None,
        "Flags non-cryptographic rand calls inside secret-like generation functions.",
    ),
    rule_definition!(
        "security.sql-dynamic-query",
        "Dynamic SQL query argument",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags direct dynamic SQL query arguments such as query(format!(...)).",
    ),
    rule_definition!(
        "security.tls-verification-disabled",
        "TLS verification disabled",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags explicit TLS certificate or hostname verification bypasses.",
    ),
    rule_definition!(
        "security.unsafe-block",
        "Unsafe block",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags unsafe blocks without a nearby SAFETY rationale.",
    ),
    rule_definition!(
        "security.weak-crypto",
        "Weak cryptographic primitive",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::Medium,
        None,
        "Flags explicit weak cryptographic primitive imports or constructors for review.",
    ),
    rule_definition!(
        "security.path-traversal-candidate",
        "Path traversal candidate",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::Medium,
        None,
        "Flags filesystem path construction where externally-derived input is joined without normalisation.",
    ),
    rule_definition!(
        "security.hardcoded-bind-all-interfaces",
        "Hardcoded bind to all interfaces",
        Pillar::Security,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags listener address literals like `0.0.0.0` or `[::]` outside test infrastructure.",
    ),
];

pub(crate) const SENSITIVE_DATA_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "sensitive-data.api-key-pattern",
        "API key pattern",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags common API key patterns.",
    ),
    rule_definition!(
        "sensitive-data.aws-access-key",
        "AWS access key",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags AWS access key patterns.",
    ),
    rule_definition!(
        "sensitive-data.database-url-password",
        "Database URL password",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags database URLs that appear to include passwords.",
    ),
    rule_definition!(
        "sensitive-data.hardcoded-env-value",
        "Hardcoded environment-style secret",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags secret-like KEY=value literals committed in source or config.",
    ),
    rule_definition!(
        "sensitive-data.high-entropy-string",
        "High entropy string",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::Medium,
        None,
        "Flags long string literals that look like generated secrets.",
    ),
    rule_definition!(
        "sensitive-data.jwt-token",
        "JWT token",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags JWT-looking token strings.",
    ),
    rule_definition!(
        "sensitive-data.private-key",
        "Private key block",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags private key block markers.",
    ),
    rule_definition!(
        "sensitive-data.url-embedded-credentials",
        "URL embedded credentials",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags HTTP(S) URLs that include embedded username and password credentials.",
    ),
    rule_definition!(
        "sensitive-data.pii-test-fixture",
        "PII in test fixture",
        Pillar::SensitiveData,
        RuleKind::Text,
        Severity::Error,
        Confidence::High,
        None,
        "Flags realistic emails, SSN-shaped strings, or US phone numbers in fixture or sample files.",
    ),
];

pub(crate) const SIZE_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "size.file-length",
        "File length",
        Pillar::Size,
        RuleKind::Text,
        Severity::Warning,
        Confidence::High,
        FILE_LENGTH_THRESHOLD,
        "Flags files over the configured line-count threshold.",
    ),
    rule_definition!(
        "size.function-length",
        "Function length",
        Pillar::Size,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        FUNCTION_LENGTH_THRESHOLD,
        "Flags functions over the configured line-count threshold.",
    ),
    rule_definition!(
        "size.parameter-count",
        "Parameter count",
        Pillar::Size,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        PARAMETER_COUNT_THRESHOLD,
        "Flags functions with too many parameters.",
    ),
];

pub(crate) const TEST_QUALITY_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "test-quality.conditional-logic",
        "Conditional logic in test",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags tests that contain conditional logic.",
    ),
    rule_definition!(
        "test-quality.ignored-without-reason",
        "Ignored test without reason",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags ignored tests that do not explain why they are skipped.",
    ),
    rule_definition!(
        "test-quality.long-test",
        "Long test",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        TEST_LONG_THRESHOLD,
        "Flags long test functions that are harder to scan and maintain.",
    ),
    rule_definition!(
        "test-quality.no-assertions",
        "No assertions in test",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags tests that do not appear to assert behavior.",
    ),
    rule_definition!(
        "test-quality.sleep-in-test",
        "Sleep in test",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags tests that sleep instead of synchronizing on behavior.",
    ),
    rule_definition!(
        "test-quality.trivial-assertion",
        "Trivial assertion",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags assertions that prove literals or constants instead of behavior.",
    ),
    rule_definition!(
        "test-quality.unwrap-in-test",
        "Unwrap in test",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags unwrap calls in tests.",
    ),
    rule_definition!(
        "test-quality.should-panic-without-expected",
        "Should-panic without expected message",
        Pillar::TestQuality,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags `#[should_panic]` attributes without an `expected = \"...\"` clause.",
    ),
];

pub(crate) const WASTE_RULES: &[RuleDefinition] = &[
    rule_definition!(
        "waste.unnecessary-clone-candidate",
        "Unnecessary clone candidate",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags clone calls that may be avoidable.",
    ),
    rule_definition!(
        "waste.unreachable-code",
        "Unreachable code",
        Pillar::DeadCode,
        RuleKind::Rust,
        Severity::Warning,
        Confidence::High,
        None,
        "Flags statements after terminating statements.",
    ),
    rule_definition!(
        "waste.unwrap-expect",
        "Unwrap or expect",
        Pillar::Maintainability,
        RuleKind::Rust,
        Severity::Advisory,
        Confidence::High,
        None,
        "Flags unwrap and expect calls outside test attributes.",
    ),
];