rustqual 1.6.0

Comprehensive Rust code quality analyzer — seven dimensions: IOSP, Complexity, DRY, SRP, Coupling, Test Quality, Architecture
Documentation
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
use serde::Deserialize;

// ── Default constants (configurable thresholds) ─────────────────────────

pub const DEFAULT_MAX_SUPPRESSION_RATIO: f64 = 0.05;

/// How far above the actual metric value a `allow(dim, target=N)` pin may
/// sit before it is reported as a too-loose orphan. 0.10 = the pin may be
/// at most 10% above the value it covers; beyond that, tighten it to ~value.
pub const DEFAULT_PIN_HEADROOM: f64 = 0.10;

// Complexity
pub const DEFAULT_COMPLEXITY_ENABLED: bool = true;
pub const DEFAULT_MAX_COGNITIVE: usize = 15;
pub const DEFAULT_MAX_CYCLOMATIC: usize = 10;
pub const DEFAULT_MAX_NESTING_DEPTH: usize = 4;
pub const DEFAULT_MAX_FUNCTION_LINES: usize = 60;
pub const DEFAULT_DETECT_MAGIC_NUMBERS: bool = true;
pub const DEFAULT_DETECT_UNSAFE: bool = true;
pub const DEFAULT_DETECT_ERROR_HANDLING: bool = true;
pub const DEFAULT_ALLOW_EXPECT: bool = false;

// DRY / Duplicates
pub const DEFAULT_DUPLICATES_ENABLED: bool = true;
pub const DEFAULT_DETECT_WILDCARD_IMPORTS: bool = true;
pub const DEFAULT_DETECT_REPEATED_MATCHES: bool = true;
pub const DEFAULT_SIMILARITY_THRESHOLD: f64 = 0.85;
pub const DEFAULT_MIN_TOKENS: usize = 30;
pub const DEFAULT_MIN_LINES: usize = 5;
pub const DEFAULT_MIN_STATEMENTS: usize = 3;
pub const DEFAULT_DETECT_DEAD_CODE: bool = true;

// Boilerplate
pub const DEFAULT_BOILERPLATE_ENABLED: bool = true;

// SRP
pub const DEFAULT_SRP_ENABLED: bool = true;
pub const DEFAULT_SRP_SMELL_THRESHOLD: f64 = 0.6;
pub const DEFAULT_SRP_MAX_FIELDS: usize = 12;
pub const DEFAULT_SRP_MAX_METHODS: usize = 20;
pub const DEFAULT_SRP_MAX_FAN_OUT: usize = 10;
pub const DEFAULT_SRP_LCOM4_THRESHOLD: usize = 2;
/// Highest production line count a file may have before SRP_MODULE fires.
/// A single threshold (no baseline/ceiling ramp): a file is flagged when
/// its production lines strictly exceed this value.
pub const DEFAULT_SRP_FILE_LENGTH: usize = 300;
// Highest cluster count that still passes. A file with more than
// this many independent function clusters is flagged as having
// multiple responsibilities. Default `2` means 3+ clusters trigger
// the warning — preserving the historical threshold now that the
// comparison uses strict `>` consistent with other `max_*` fields.
pub const DEFAULT_SRP_MAX_INDEPENDENT_CLUSTERS: usize = 2;
pub const DEFAULT_SRP_MIN_CLUSTER_STATEMENTS: usize = 5;
pub const DEFAULT_SRP_MAX_PARAMETERS: usize = 5;

// Coupling
pub const DEFAULT_COUPLING_ENABLED: bool = true;
pub const DEFAULT_CHECK_SDP: bool = true;
pub const DEFAULT_MAX_INSTABILITY: f64 = 0.8;
pub const DEFAULT_MAX_FAN_IN: usize = 15;
pub const DEFAULT_MAX_FAN_OUT_COUPLING: usize = 12;

// Structural (binary checks)
pub const DEFAULT_STRUCTURAL_ENABLED: bool = true;

// Test Quality
pub const DEFAULT_TEST_ENABLED: bool = true;

// Quality weights: [IOSP, Complexity, DRY, SRP, Coupling, TestQuality, Architecture]
pub const DEFAULT_QUALITY_WEIGHTS: [f64; 7] = [0.22, 0.18, 0.13, 0.18, 0.09, 0.10, 0.10];

/// Maximum acceptable deviation from 1.0 for weight sum validation.
pub const WEIGHT_SUM_TOLERANCE: f64 = 0.001;

// ── Sub-config structs ──────────────────────────────────────────────────

/// Configuration for complexity analysis.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct ComplexityConfig {
    pub enabled: bool,
    pub max_cognitive: usize,
    pub max_cyclomatic: usize,
    pub max_nesting_depth: usize,
    pub max_function_lines: usize,
    pub include_nesting_penalty: bool,
    pub detect_magic_numbers: bool,
    pub detect_unsafe: bool,
    pub detect_error_handling: bool,
    pub allow_expect: bool,
    pub allowed_magic_numbers: Vec<String>,
}

impl Default for ComplexityConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_COMPLEXITY_ENABLED,
            max_cognitive: DEFAULT_MAX_COGNITIVE,
            max_cyclomatic: DEFAULT_MAX_CYCLOMATIC,
            max_nesting_depth: DEFAULT_MAX_NESTING_DEPTH,
            max_function_lines: DEFAULT_MAX_FUNCTION_LINES,
            include_nesting_penalty: true,
            detect_magic_numbers: DEFAULT_DETECT_MAGIC_NUMBERS,
            detect_unsafe: DEFAULT_DETECT_UNSAFE,
            detect_error_handling: DEFAULT_DETECT_ERROR_HANDLING,
            allow_expect: DEFAULT_ALLOW_EXPECT,
            allowed_magic_numbers: vec![
                "0".into(),
                "1".into(),
                "-1".into(),
                "2".into(),
                "0.0".into(),
                "1.0".into(),
            ],
        }
    }
}

/// Configuration for duplicate / DRY detection.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct DuplicatesConfig {
    pub enabled: bool,
    pub similarity_threshold: f64,
    pub min_tokens: usize,
    pub min_lines: usize,
    pub min_statements: usize,
    pub ignore_trait_impls: bool,
    pub detect_dead_code: bool,
    pub detect_wildcard_imports: bool,
    pub detect_repeated_matches: bool,
}

impl Default for DuplicatesConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_DUPLICATES_ENABLED,
            similarity_threshold: DEFAULT_SIMILARITY_THRESHOLD,
            min_tokens: DEFAULT_MIN_TOKENS,
            min_lines: DEFAULT_MIN_LINES,
            min_statements: DEFAULT_MIN_STATEMENTS,
            ignore_trait_impls: true,
            detect_dead_code: DEFAULT_DETECT_DEAD_CODE,
            detect_wildcard_imports: DEFAULT_DETECT_WILDCARD_IMPORTS,
            detect_repeated_matches: DEFAULT_DETECT_REPEATED_MATCHES,
        }
    }
}

/// The fixed vocabulary for `[boilerplate].accepted_display_idioms` — the
/// trivial-Display forms a project may declare as its house idiom (BP-002
/// then treats them as policy, not boilerplate). Validation is fail-loud:
/// an entry outside this list is a config error, never a silent no-op.
pub const DISPLAY_IDIOM_VOCABULARY: &[&str] =
    &["write_str", "write_char", "write_macro", "delegation"];

/// Configuration for boilerplate detection.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct BoilerplateConfig {
    pub enabled: bool,
    pub patterns: Vec<String>,
    pub suggest_crates: bool,
    /// Trivial-Display forms declared as the project's house idiom; BP-002
    /// does not flag an fmt body whose every write op is an accepted idiom.
    /// Entries must come from [`DISPLAY_IDIOM_VOCABULARY`].
    pub accepted_display_idioms: Vec<String>,
}

impl Default for BoilerplateConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_BOILERPLATE_ENABLED,
            patterns: vec![],
            suggest_crates: true,
            accepted_display_idioms: vec![],
        }
    }
}

/// Configuration for SRP (Single Responsibility Principle) analysis.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct SrpConfig {
    pub enabled: bool,
    pub smell_threshold: f64,
    pub max_fields: usize,
    pub max_methods: usize,
    pub max_fan_out: usize,
    pub lcom4_threshold: usize,
    pub weights: [f64; 4],
    pub file_length: usize,
    pub max_independent_clusters: usize,
    pub min_cluster_statements: usize,
    pub max_parameters: usize,
}

impl Default for SrpConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_SRP_ENABLED,
            smell_threshold: DEFAULT_SRP_SMELL_THRESHOLD,
            max_fields: DEFAULT_SRP_MAX_FIELDS,
            max_methods: DEFAULT_SRP_MAX_METHODS,
            max_fan_out: DEFAULT_SRP_MAX_FAN_OUT,
            lcom4_threshold: DEFAULT_SRP_LCOM4_THRESHOLD,
            weights: [0.4, 0.25, 0.15, 0.2],
            file_length: DEFAULT_SRP_FILE_LENGTH,
            max_independent_clusters: DEFAULT_SRP_MAX_INDEPENDENT_CLUSTERS,
            min_cluster_statements: DEFAULT_SRP_MIN_CLUSTER_STATEMENTS,
            max_parameters: DEFAULT_SRP_MAX_PARAMETERS,
        }
    }
}

/// Per-test-code threshold overrides.
///
/// rustqual applies a curated subset of checks to test code — DRY-001/003/005
/// (duplicate fns / fragments / repeated matches), LONG_FN (function length),
/// and SRP file-length (SRP_MODULE). The god-struct check (SRP-001) also fires
/// on test structs — a god-fixture is a real smell — but at production
/// thresholds, with no separate test knob (`// qual:allow(srp, god_struct)`
/// covers the rare legitimate fixture). The remaining checks stay test-exempt:
/// ERROR_HANDLING, MAGIC_NUMBER, IOSP, DRY-002 dead code, DRY-004 wildcard
/// imports, Coupling,
/// all Structural detectors, and the SRP *module*-cohesion (independent-cluster)
/// check — a test file's independent `#[test]` fns are its purpose, not a smell.
/// **This split is fixed — only the thresholds below are configurable.**
///
/// Each field overrides the matching production threshold *for test code
/// only*. The default is `None`, meaning the production value is inherited —
/// so out of the box test code is judged by the same limits as production.
/// Field names mirror the production sections (`[complexity]`, `[srp]`).
#[derive(Debug, Deserialize, Clone, Default)]
#[serde(default, deny_unknown_fields)]
pub struct TestsConfig {
    /// Overrides `[complexity].max_function_lines` for test fns (LONG_FN).
    pub max_function_lines: Option<usize>,
    /// Overrides `[srp].file_length` for test files (SRP_MODULE).
    pub file_length: Option<usize>,
}

/// Configuration for suppression-marker quality checks.
///
/// Today this holds a single knob, `pin_headroom`: a metric pin
/// `// qual:allow(dim, target=N)` is reported as a too-loose orphan when
/// `N` exceeds the actual value it covers by more than this fraction. A pin
/// at or below `value × (1 + pin_headroom)` is accepted; above it, the
/// author is told to tighten the pin to ~value or remove it. This keeps
/// pins honest — a pin parked far above the real metric silently absorbs
/// future regressions up to its ceiling.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct SuppressionConfig {
    /// Maximum fraction a metric pin may exceed the value it covers
    /// before being flagged too-loose. Default `0.10` (10%).
    pub pin_headroom: f64,
}

impl Default for SuppressionConfig {
    fn default() -> Self {
        Self {
            pin_headroom: DEFAULT_PIN_HEADROOM,
        }
    }
}

/// Configuration for coupling analysis.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct CouplingConfig {
    pub enabled: bool,
    pub max_instability: f64,
    pub max_fan_in: usize,
    pub max_fan_out: usize,
    pub check_sdp: bool,
}

impl Default for CouplingConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_COUPLING_ENABLED,
            max_instability: DEFAULT_MAX_INSTABILITY,
            max_fan_in: DEFAULT_MAX_FAN_IN,
            max_fan_out: DEFAULT_MAX_FAN_OUT_COUPLING,
            check_sdp: DEFAULT_CHECK_SDP,
        }
    }
}

/// Configuration for structural binary checks.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct StructuralConfig {
    pub enabled: bool,
    pub check_btc: bool,
    pub check_slm: bool,
    pub check_nms: bool,
    pub check_oi: bool,
    pub check_sit: bool,
    pub check_deh: bool,
    pub check_iet: bool,
}

impl Default for StructuralConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_STRUCTURAL_ENABLED,
            check_btc: true,
            check_slm: true,
            check_nms: true,
            check_oi: true,
            check_sit: true,
            check_deh: true,
            check_iet: true,
        }
    }
}

/// Configuration for test quality analysis.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct TestConfig {
    pub enabled: bool,
    /// Optional path to an LCOV coverage file for TQ-004/TQ-005 checks.
    pub coverage_file: Option<String>,
    /// Extra macro names (beyond `assert*`) to recognize as assertions in TQ-001.
    pub extra_assertion_macros: Vec<String>,
}

impl Default for TestConfig {
    fn default() -> Self {
        Self {
            enabled: DEFAULT_TEST_ENABLED,
            coverage_file: None,
            extra_assertion_macros: vec![],
        }
    }
}

/// Configuration for rustqual-wide report aggregation (used in workspace mode).
///
/// Applies to all dimensions, not architecture-specific.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct ReportConfig {
    /// Aggregation strategy across workspace member crates.
    /// Allowed values: "loc_weighted" | "arithmetic".
    pub aggregation: String,
}

impl Default for ReportConfig {
    fn default() -> Self {
        Self {
            aggregation: "loc_weighted".to_string(),
        }
    }
}

/// Configuration for quality score dimension weights.
#[derive(Debug, Deserialize, Clone)]
#[serde(default, deny_unknown_fields)]
pub struct WeightsConfig {
    pub iosp: f64,
    pub complexity: f64,
    pub dry: f64,
    pub srp: f64,
    pub coupling: f64,
    pub test_quality: f64,
    pub architecture: f64,
}

impl WeightsConfig {
    /// Convert weights to an array in the standard dimension order.
    /// Operation: trivial field access.
    pub fn as_array(&self) -> [f64; 7] {
        [
            self.iosp,
            self.complexity,
            self.dry,
            self.srp,
            self.coupling,
            self.test_quality,
            self.architecture,
        ]
    }
}

impl Default for WeightsConfig {
    fn default() -> Self {
        let [iosp, complexity, dry, srp, coupling, test_quality, architecture] =
            DEFAULT_QUALITY_WEIGHTS;
        Self {
            iosp,
            complexity,
            dry,
            srp,
            coupling,
            test_quality,
            architecture,
        }
    }
}