rustqual 0.3.9

Comprehensive Rust code quality analyzer — six dimensions: Complexity, Coupling, DRY, IOSP, SRP, Test Quality
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
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
use std::collections::{HashMap, HashSet};

use crate::config::sections::SrpConfig;

use super::union_find::UnionFind;
use super::{MethodFieldData, ResponsibilityCluster, SrpWarning, StructInfo};

/// Build SRP warnings for structs that exceed the smell threshold.
/// Operation: groups methods by parent type, computes LCOM4 and composite
/// score per struct via closures (filter_map), no own calls.
pub fn build_struct_warnings(
    structs: &[StructInfo],
    methods: &[MethodFieldData],
    config: &SrpConfig,
) -> Vec<SrpWarning> {
    // Group methods by parent type
    let mut methods_by_type: HashMap<&str, Vec<&MethodFieldData>> = HashMap::new();
    for m in methods {
        methods_by_type.entry(&m.parent_type).or_default().push(m);
    }

    structs
        .iter()
        .filter_map(|s| {
            let type_methods = methods_by_type.get(s.name.as_str());
            let method_list: Vec<&MethodFieldData> =
                type_methods.map(|v| v.to_vec()).unwrap_or_default();

            // Skip structs with fewer than 2 instance methods (LCOM4 undefined)
            if method_list.len() < 2 {
                return None;
            }

            let (lcom4, clusters) = compute_lcom4(&method_list, &s.fields);
            let fan_out = compute_fan_out(&method_list);
            let composite =
                compute_composite_score(lcom4, s.fields.len(), method_list.len(), fan_out, config);

            if composite >= config.smell_threshold {
                Some(SrpWarning {
                    struct_name: s.name.clone(),
                    file: s.file.clone(),
                    line: s.line,
                    lcom4,
                    field_count: s.fields.len(),
                    method_count: method_list.len(),
                    fan_out,
                    composite_score: composite,
                    clusters,
                    suppressed: false,
                })
            } else {
                None
            }
        })
        .collect()
}

/// Compute LCOM4: number of connected components in the method-field graph.
/// Operation: Union-Find on method indices connected by shared field accesses.
/// Uses closures to wrap UnionFind calls for IOSP lenient-mode compliance.
fn compute_lcom4(
    methods: &[&MethodFieldData],
    struct_fields: &[String],
) -> (usize, Vec<ResponsibilityCluster>) {
    let n = methods.len();
    if n == 0 {
        return (0, vec![]);
    }

    let make_uf = |size| UnionFind::new(size);
    let mut uf = make_uf(n);
    let unite = |uf: &mut UnionFind, a, b| uf.union(a, b);
    let components = |uf: &mut UnionFind| uf.component_members();
    // Build field -> method indices map.
    // Constructors (returning Self) conceptually touch ALL struct fields.
    let mut field_to_methods: HashMap<&str, Vec<usize>> = HashMap::new();
    methods.iter().enumerate().for_each(|(i, m)| {
        let fields_to_add: Vec<&str> = if m.is_constructor {
            struct_fields.iter().map(|f| f.as_str()).collect()
        } else {
            m.field_accesses
                .iter()
                .filter(|f| struct_fields.iter().any(|sf| sf == *f))
                .map(|f| f.as_str())
                .collect()
        };
        fields_to_add.iter().for_each(|&field| {
            field_to_methods.entry(field).or_default().push(i);
        });
    });
    // Union methods that share fields
    field_to_methods.values().for_each(|indices| {
        indices.windows(2).for_each(|w| unite(&mut uf, w[0], w[1]));
    });
    // Build clusters from connected components
    let component_members = components(&mut uf);
    let clusters: Vec<ResponsibilityCluster> = component_members
        .values()
        .map(|member_indices| {
            let cluster_methods: Vec<String> = member_indices
                .iter()
                .map(|&i| methods[i].method_name.clone())
                .collect();
            let cluster_fields: HashSet<String> = member_indices
                .iter()
                .flat_map(|&i| {
                    methods[i]
                        .field_accesses
                        .iter()
                        .filter(|f| struct_fields.iter().any(|sf| sf == *f))
                        .cloned()
                })
                .collect();
            ResponsibilityCluster {
                methods: cluster_methods,
                fields: cluster_fields.into_iter().collect(),
            }
        })
        .collect();
    (component_members.len(), clusters)
}

/// Compute total fan-out: distinct external call targets across all methods.
/// Operation: set union.
fn compute_fan_out(methods: &[&MethodFieldData]) -> usize {
    let all_targets: HashSet<&str> = methods
        .iter()
        .flat_map(|m| m.call_targets.iter().map(|s| s.as_str()))
        .collect();
    all_targets.len()
}

/// Compute the composite SRP smell score from sub-metrics.
/// Operation: arithmetic normalization + weighted sum.
fn compute_composite_score(
    lcom4: usize,
    field_count: usize,
    method_count: usize,
    fan_out: usize,
    config: &SrpConfig,
) -> f64 {
    // Normalize LCOM4: 0 when <=1 (cohesive), scales linearly above threshold
    let lcom4_norm = if lcom4 <= 1 {
        0.0
    } else {
        let excess = (lcom4 - 1) as f64;
        let threshold_range = (config.lcom4_threshold.max(1) - 1) as f64;
        if threshold_range > 0.0 {
            (excess / threshold_range).min(1.0)
        } else {
            1.0
        }
    };

    let field_norm = (field_count as f64 / config.max_fields as f64).min(1.0);
    let method_norm = (method_count as f64 / config.max_methods as f64).min(1.0);
    let fan_out_norm = (fan_out as f64 / config.max_fan_out as f64).min(1.0);

    let [w_lcom4, w_fields, w_methods, w_fan_out] = config.weights;

    w_lcom4 * lcom4_norm
        + w_fields * field_norm
        + w_methods * method_norm
        + w_fan_out * fan_out_norm
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::sections::SrpConfig;

    fn make_method(name: &str, parent: &str, fields: &[&str], calls: &[&str]) -> MethodFieldData {
        MethodFieldData {
            method_name: name.to_string(),
            parent_type: parent.to_string(),
            field_accesses: fields.iter().map(|s| s.to_string()).collect(),
            call_targets: calls.iter().map(|s| s.to_string()).collect(),
            is_constructor: false,
        }
    }

    fn make_constructor(name: &str, parent: &str, calls: &[&str]) -> MethodFieldData {
        MethodFieldData {
            method_name: name.to_string(),
            parent_type: parent.to_string(),
            field_accesses: HashSet::new(),
            call_targets: calls.iter().map(|s| s.to_string()).collect(),
            is_constructor: true,
        }
    }

    fn make_struct(name: &str, fields: &[&str]) -> StructInfo {
        StructInfo {
            name: name.to_string(),
            file: "test.rs".to_string(),
            line: 1,
            fields: fields.iter().map(|s| s.to_string()).collect(),
        }
    }

    #[test]
    fn test_lcom4_fully_cohesive() {
        // All methods access the same field → LCOM4 = 1
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &["x"], &[]);
        let m3 = make_method("c", "Foo", &["x"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (lcom4, clusters) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 1);
        assert_eq!(clusters.len(), 1);
    }

    #[test]
    fn test_lcom4_two_clusters() {
        // Two disjoint groups of methods
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &["x"], &[]);
        let m3 = make_method("c", "Foo", &["y"], &[]);
        let m4 = make_method("d", "Foo", &["y"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3, &m4];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (lcom4, clusters) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 2);
        assert_eq!(clusters.len(), 2);
    }

    #[test]
    fn test_lcom4_no_shared_fields() {
        // Each method accesses a unique field → N components
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &["y"], &[]);
        let m3 = make_method("c", "Foo", &["z"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3];
        let fields = vec!["x".to_string(), "y".to_string(), "z".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 3);
    }

    #[test]
    fn test_lcom4_empty_methods() {
        let methods: Vec<&MethodFieldData> = vec![];
        let fields = vec!["x".to_string()];
        let (lcom4, clusters) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 0);
        assert!(clusters.is_empty());
    }

    #[test]
    fn test_lcom4_method_with_no_field_access() {
        // Method that doesn't access any struct fields → isolated component
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &[], &["helper"]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2];
        let fields = vec!["x".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 2);
    }

    #[test]
    fn test_fan_out_distinct_targets() {
        let m1 = make_method("a", "Foo", &[], &["helper", "process"]);
        let m2 = make_method("b", "Foo", &[], &["helper", "format"]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2];
        let fan_out = compute_fan_out(&methods);
        assert_eq!(fan_out, 3); // helper, process, format
    }

    #[test]
    fn test_fan_out_empty() {
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1];
        let fan_out = compute_fan_out(&methods);
        assert_eq!(fan_out, 0);
    }

    #[test]
    fn test_composite_score_fully_cohesive() {
        let config = SrpConfig::default();
        // LCOM4=1, small struct → low score
        let score = compute_composite_score(1, 3, 3, 0, &config);
        assert!(
            score < config.smell_threshold,
            "Cohesive struct should score below threshold, got {score}"
        );
    }

    #[test]
    fn test_composite_score_high_lcom4() {
        let config = SrpConfig::default();
        // LCOM4=4, many fields, many methods, high fan-out
        let score = compute_composite_score(4, 15, 20, 12, &config);
        assert!(
            score >= config.smell_threshold,
            "Incohesive struct should exceed threshold, got {score}"
        );
    }

    #[test]
    fn test_composite_score_lcom4_one_is_zero() {
        let config = SrpConfig::default();
        let score_cohesive = compute_composite_score(1, 5, 5, 2, &config);
        let score_incohesive = compute_composite_score(3, 5, 5, 2, &config);
        assert!(score_incohesive > score_cohesive);
    }

    #[test]
    fn test_build_struct_warnings_no_warning_for_small_struct() {
        let structs = vec![make_struct("Counter", &["count"])];
        let m1 = make_method("increment", "Counter", &["count"], &[]);
        let m2 = make_method("get", "Counter", &["count"], &[]);
        let methods = vec![m1, m2];
        let config = SrpConfig::default();
        let warnings = build_struct_warnings(&structs, &methods, &config);
        assert!(warnings.is_empty(), "Small cohesive struct should not warn");
    }

    #[test]
    fn test_build_struct_warnings_single_method_skipped() {
        // Structs with <2 methods are skipped (LCOM4 is undefined)
        let structs = vec![make_struct("Solo", &["x", "y", "z"])];
        let m1 = make_method("do_it", "Solo", &["x"], &[]);
        let methods = vec![m1];
        let config = SrpConfig::default();
        let warnings = build_struct_warnings(&structs, &methods, &config);
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_build_struct_warnings_no_methods_skipped() {
        let structs = vec![make_struct("Data", &["x", "y"])];
        let methods = vec![];
        let config = SrpConfig::default();
        let warnings = build_struct_warnings(&structs, &methods, &config);
        assert!(warnings.is_empty());
    }

    #[test]
    fn test_build_struct_warnings_triggers_for_incohesive() {
        // Create a struct with clearly disjoint method groups + high fan-out
        let structs = vec![make_struct(
            "GodObject",
            &[
                "db", "cache", "logger", "metrics", "config", "state", "buffer", "queue", "pool",
                "handler", "router", "auth",
            ],
        )];
        let methods = vec![
            make_method(
                "read_db",
                "GodObject",
                &["db"],
                &["query", "parse", "validate"],
            ),
            make_method("write_db", "GodObject", &["db"], &["insert", "commit"]),
            make_method(
                "read_cache",
                "GodObject",
                &["cache"],
                &["get_key", "deserialize"],
            ),
            make_method(
                "write_cache",
                "GodObject",
                &["cache"],
                &["set_key", "serialize"],
            ),
            make_method("log_info", "GodObject", &["logger"], &["format_log"]),
            make_method(
                "log_error",
                "GodObject",
                &["logger", "metrics"],
                &["format_log", "increment"],
            ),
            make_method(
                "route_request",
                "GodObject",
                &["router", "handler"],
                &["match_path", "dispatch"],
            ),
            make_method(
                "authenticate",
                "GodObject",
                &["auth", "config"],
                &["verify_token", "check_role"],
            ),
            make_method(
                "flush_buffer",
                "GodObject",
                &["buffer", "queue"],
                &["drain", "send"],
            ),
            make_method(
                "manage_pool",
                "GodObject",
                &["pool", "state"],
                &["allocate", "release"],
            ),
        ];
        let config = SrpConfig::default();
        let warnings = build_struct_warnings(&structs, &methods, &config);
        assert!(
            !warnings.is_empty(),
            "Incohesive god object should trigger SRP warning"
        );
        assert_eq!(warnings[0].struct_name, "GodObject");
    }

    #[test]
    fn test_lcom4_transitive_connection() {
        // a→{x}, b→{x,y}, c→{y} → all connected through b
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &["x", "y"], &[]);
        let m3 = make_method("c", "Foo", &["y"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(
            lcom4, 1,
            "Transitively connected methods should form one component"
        );
    }

    #[test]
    fn test_cluster_contains_correct_fields() {
        let m1 = make_method("a", "Foo", &["x"], &[]);
        let m2 = make_method("b", "Foo", &["y"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (_, clusters) = compute_lcom4(&methods, &fields);
        assert_eq!(clusters.len(), 2);
        // Each cluster should have exactly one method and one field
        for c in &clusters {
            assert_eq!(c.methods.len(), 1);
            assert_eq!(c.fields.len(), 1);
        }
    }

    #[test]
    fn test_lcom4_constructor_connects_all_fields() {
        // Constructor (returns Self) should connect all methods via shared fields
        let m1 = make_method("get_x", "Foo", &["x"], &[]);
        let m2 = make_method("get_y", "Foo", &["y"], &[]);
        let m3 = make_constructor("new", "Foo", &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        // Constructor touches all fields → connects get_x and get_y → LCOM4 = 1
        assert_eq!(
            lcom4, 1,
            "Constructor should connect disjoint method groups"
        );
    }

    #[test]
    fn test_lcom4_without_constructor_stays_disjoint() {
        // Without constructor, disjoint getters stay separate
        let m1 = make_method("get_x", "Foo", &["x"], &[]);
        let m2 = make_method("get_y", "Foo", &["y"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2];
        let fields = vec!["x".to_string(), "y".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 2, "Without constructor, disjoint groups remain");
    }

    #[test]
    fn test_lcom4_constructor_default_pattern() {
        // fn default() -> Self is also a constructor
        let m1 = make_method("get_a", "Config", &["a"], &[]);
        let m2 = make_method("get_b", "Config", &["b"], &[]);
        let m3 = make_method("get_c", "Config", &["c"], &[]);
        let m4 = make_constructor("default", "Config", &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2, &m3, &m4];
        let fields = vec!["a".to_string(), "b".to_string(), "c".to_string()];
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 1, "Default constructor should unify all clusters");
    }

    #[test]
    fn test_lcom4_ignores_non_struct_fields() {
        // Method accesses a field that's not part of the struct → should be ignored
        let m1 = make_method("a", "Foo", &["x", "foreign_field"], &[]);
        let m2 = make_method("b", "Foo", &["x"], &[]);
        let methods: Vec<&MethodFieldData> = vec![&m1, &m2];
        let fields = vec!["x".to_string()]; // foreign_field is not a struct field
        let (lcom4, _) = compute_lcom4(&methods, &fields);
        assert_eq!(lcom4, 1, "Both methods share 'x', foreign field ignored");
    }
}