rust-doctor 0.6.0

Local-first health audit for Cargo workspaces: curated Clippy lints and native detectors, scored out of 100
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
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use super::*;
use crate::report::{DiagnosticSource, Severity};

mod oracle;
use std::sync::atomic::{AtomicUsize, Ordering};

static NEXT_ROOT: AtomicUsize = AtomicUsize::new(0);

fn root(name: &str) -> PathBuf {
    let sequence = NEXT_ROOT.fetch_add(1, Ordering::Relaxed);
    let root = Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("target/delta-kernel")
        .join(format!("{}-{name}-{sequence}", std::process::id()));
    if root.exists() {
        fs::remove_dir_all(&root).unwrap();
    }
    fs::create_dir_all(root.join("src")).unwrap();
    root
}

/// The line every oracle case starts from on both sides.
const ORIGINAL: &str = "todo!();\n";

/// A scratch tree removed when the test that made it ends.
///
/// Deleting at the end of a body leaves the tree behind on exactly the run whose
/// fixture is worth keeping out of the next one: the one whose assertion failed.
struct Scratch(PathBuf);

impl Drop for Scratch {
    fn drop(&mut self) {
        let _ = fs::remove_dir_all(&self.0);
    }
}

fn write_source(root: &Path, path: &str, source: &str) {
    let path = root.join(path);
    fs::create_dir_all(path.parent().unwrap()).unwrap();
    fs::write(path, source).unwrap();
}

/// Two source trees and the two diagnostic lists compared over them.
///
/// Naming the trees, writing them, listing the diagnostics, computing the delta
/// and removing the trees is the whole shape of a test here, and spelling it out
/// in each of them is what made three of them near-duplicates of one another.
struct Scenario {
    baseline_root: Scratch,
    current_root: Scratch,
    baseline: Vec<Diagnostic>,
    current: Vec<Diagnostic>,
}

impl Scenario {
    fn new(name: &str) -> Self {
        Self::at(
            root(&format!("{name}-base")),
            root(&format!("{name}-current")),
        )
    }

    /// A scenario under an existing pair of parents, which is how the oracle
    /// keeps its thirty-two cases side by side.
    fn under(baseline_parent: &Path, current_parent: &Path, name: &str) -> Self {
        let baseline_root = baseline_parent.join(name);
        let current_root = current_parent.join(name);
        fs::create_dir_all(&baseline_root).unwrap();
        fs::create_dir_all(&current_root).unwrap();
        Self::at(baseline_root, current_root)
    }

    fn at(baseline_root: PathBuf, current_root: PathBuf) -> Self {
        Self {
            baseline_root: Scratch(baseline_root),
            current_root: Scratch(current_root),
            baseline: Vec::new(),
            current: Vec::new(),
        }
    }

    fn write_baseline(&self, path: &str, source: &str) {
        write_source(&self.baseline_root.0, path, source);
    }

    fn write_current(&self, path: &str, source: &str) {
        write_source(&self.current_root.0, path, source);
    }

    fn write_both(&self, path: &str, source: &str) {
        self.write_baseline(path, source);
        self.write_current(path, source);
    }

    fn delta(&self) -> DeltaReport {
        compute(
            &self.baseline,
            &self.current,
            &self.baseline_root.0,
            &self.current_root.0,
        )
        .unwrap()
    }
}

fn diagnostic(id: &str, path: Option<&str>, span: Option<DiagnosticSpan>) -> Diagnostic {
    Diagnostic {
        context: None,
        id: id.to_owned(),
        source: DiagnosticSource::Clippy,
        code: Some("clippy::todo".to_owned()),
        base_severity: Severity::Warning,
        severity: Severity::Warning,
        category: Some("maintainability".to_owned()),
        message: "todo macro is used".to_owned(),
        help: None,
        package: Some("fixture".to_owned()),
        target: Some("fixture".to_owned()),
        path: path.map(str::to_owned),
        span,
        related: Vec::new(),
        similarity_basis_points: None,
        complexity: None,
        occurrences: 1,
    }
}

fn span(line: usize, start: usize, end: usize) -> DiagnosticSpan {
    DiagnosticSpan {
        line_start: line,
        column_start: start,
        line_end: line,
        column_end: end,
    }
}

fn test_proof(source: &str, span: &DiagnosticSpan, remaining_budget: usize) -> Option<String> {
    extract_proof(source, &LineIndex::of(source), span, remaining_budget)
}

#[test]
fn proof_normalizes_unicode_whitespace_and_rejects_invalid_or_oversized_ranges() {
    let source = "préfixe\r\n\t todo!(  \"échec\" ) ;\n";
    let proof = test_proof(
        source,
        &DiagnosticSpan {
            line_start: 2,
            column_start: 1,
            line_end: 2,
            column_end: 22,
        },
        PROOF_BYTES_BUDGET,
    )
    .unwrap();
    assert_eq!(proof, "todo!( \"échec\" ) ;");
    let lf = "préfixe\n\t todo!(  \"échec\" ) ;\n";
    assert_eq!(
        test_proof(lf, &span(2, 1, 22), PROOF_BYTES_BUDGET).unwrap(),
        proof,
        "CRLF and LF must normalize identically"
    );
    let multiline = "call(\n\tvalue,\n  other)\n";
    assert_eq!(
        test_proof(
            multiline,
            &DiagnosticSpan {
                line_start: 1,
                column_start: 1,
                line_end: 3,
                column_end: 9,
            },
            PROOF_BYTES_BUDGET,
        )
        .unwrap(),
        "call( value, other)"
    );
    assert!(test_proof(source, &span(0, 1, 2), PROOF_BYTES_BUDGET).is_none());
    assert!(test_proof(source, &span(2, 22, 1), PROOF_BYTES_BUDGET).is_none());

    let oversized = "x".repeat(PROOF_BYTES_LIMIT + 1);
    assert!(
        test_proof(
            &oversized,
            &span(1, 1, PROOF_BYTES_LIMIT.saturating_add(2)),
            PROOF_BYTES_BUDGET,
        )
        .is_none()
    );
}

/// What a delta is expected to say, as the three lists a reader checks.
#[derive(Debug, PartialEq, Eq)]
struct Verdict {
    introduced: Vec<String>,
    /// Each pairing as the current id and the baseline id it was matched with.
    pre_existing: Vec<(String, String)>,
    fixed: Vec<String>,
    cross_file_matches: usize,
}

fn verdict(
    introduced: &[&str],
    pre_existing: &[(&str, &str)],
    fixed: &[&str],
    cross_file_matches: usize,
) -> Verdict {
    Verdict {
        introduced: introduced.iter().map(|id| (*id).to_owned()).collect(),
        pre_existing: pre_existing
            .iter()
            .map(|(current, baseline)| ((*current).to_owned(), (*baseline).to_owned()))
            .collect(),
        fixed: fixed.iter().map(|id| (*id).to_owned()).collect(),
        cross_file_matches,
    }
}

/// One named comparison and the verdict it has to produce.
///
/// Writing two trees, naming two diagnostic lists and checking three vectors is
/// one shape, and the six tests that each spelled it out were near-duplicates of
/// one another and of nothing else. The shape is written once here and the cases
/// are data, which is what the thirty-two-case oracle below already does.
struct MatchingCase {
    name: &'static str,
    build: fn(&mut Scenario),
    expected: Verdict,
}

impl Scenario {
    fn verdict(&self) -> Verdict {
        let delta = self.delta();
        Verdict {
            introduced: delta.introduced,
            pre_existing: delta
                .pre_existing
                .into_iter()
                .map(|matched| (matched.current_id, matched.baseline_id))
                .collect(),
            fixed: delta
                .fixed
                .into_iter()
                .map(|diagnostic| diagnostic.id)
                .collect(),
            cross_file_matches: delta.summary.cross_file_matches,
        }
    }
}

/// What makes two diagnostics one finding, and what is deliberately left out of
/// that answer.
fn identity_cases() -> Vec<MatchingCase> {
    vec![
        MatchingCase {
            name: "severity is not part of the identity",
            build: |scenario| {
                scenario.baseline = vec![diagnostic("base", None, None)];
                scenario.current = vec![diagnostic("current", None, None)];
                scenario.current[0].severity = Severity::Error;
            },
            expected: verdict(&[], &[("current", "base")], &[], 0),
        },
        MatchingCase {
            name: "the rule is part of the identity",
            build: |scenario| {
                scenario.baseline = vec![diagnostic("base", None, None)];
                scenario.current = vec![diagnostic("current", None, None)];
                scenario.current[0].code = Some("clippy::dbg_macro".to_owned());
            },
            expected: verdict(&["current"], &[], &["base"], 0),
        },
        MatchingCase {
            name: "the message is part of the identity",
            build: |scenario| {
                scenario.baseline = vec![diagnostic("base", None, None)];
                scenario.current = vec![diagnostic("current", None, None)];
                scenario.baseline[0].message = "old message".to_owned();
            },
            expected: verdict(&["current"], &[], &["base"], 0),
        },
        MatchingCase {
            name: "changed code under an unchanged span is a different finding",
            build: |scenario| {
                scenario.write_baseline("src/changed.rs", ORIGINAL);
                scenario.write_current("src/changed.rs", "todo!(\"changed\");\n");
                scenario.baseline = vec![diagnostic(
                    "base",
                    Some("src/changed.rs"),
                    Some(span(1, 1, 8)),
                )];
                scenario.current = vec![diagnostic(
                    "current",
                    Some("src/changed.rs"),
                    Some(span(1, 1, 18)),
                )];
            },
            expected: verdict(&["current"], &[], &["base"], 0),
        },
        // `workspace_path` percent-encodes `%` and every control character of a
        // published path. Opening the published spelling literally resolved to
        // no file at all, so every finding in a file whose name carries one
        // silently lost its proof and fell back to its message, which is the
        // weakest identity the module has. Here the two excerpts differ, so a
        // proof that was read says introduced and a proof that was not says
        // pre-existing.
        MatchingCase {
            name: "an encoded path is decoded before the evidence is read",
            build: |scenario| {
                scenario.write_baseline("src/100%.rs", "todo!(\"old\");\n");
                scenario.write_current("src/100%.rs", "todo!(\"new\");\n");
                let published = "src/100%25.rs";
                scenario.baseline = vec![diagnostic("base", Some(published), Some(span(1, 1, 14)))];
                scenario.current =
                    vec![diagnostic("current", Some(published), Some(span(1, 1, 14)))];
            },
            expected: verdict(&["current"], &[], &["base"], 0),
        },
    ]
}

/// The order the passes run in, on the inputs that put two of them in
/// competition.
fn pairing_cases() -> Vec<MatchingCase> {
    vec![
        MatchingCase {
            name: "a copy does not consume the original",
            build: |scenario| {
                scenario.write_both("src/original.rs", ORIGINAL);
                scenario.write_current("src/a_copy.rs", ORIGINAL);
                scenario.baseline = vec![diagnostic(
                    "base-original",
                    Some("src/original.rs"),
                    Some(span(1, 1, 8)),
                )];
                scenario.current = vec![
                    diagnostic("current-copy", Some("src/a_copy.rs"), Some(span(1, 1, 8))),
                    diagnostic(
                        "current-original",
                        Some("src/original.rs"),
                        Some(span(1, 1, 8)),
                    ),
                ];
            },
            expected: verdict(
                &["current-copy"],
                &[("current-original", "base-original")],
                &[],
                0,
            ),
        },
        MatchingCase {
            name: "the same proof in another file is a move",
            build: |scenario| {
                scenario.write_baseline("src/old.rs", ORIGINAL);
                scenario.write_current("src/moved.rs", ORIGINAL);
                scenario.baseline =
                    vec![diagnostic("base", Some("src/old.rs"), Some(span(1, 1, 8)))];
                scenario.current = vec![diagnostic(
                    "moved",
                    Some("src/moved.rs"),
                    Some(span(1, 1, 8)),
                )];
            },
            expected: verdict(&[], &[("moved", "base")], &[], 1),
        },
        // A finding whose proof could not be read falls back to its message on
        // its own file; a finding that moved carries its proof to another file.
        // When both claim the same baseline the message match on the original
        // file wins, because the moved pass runs last. That is a decision rather
        // than a consequence, and reversing the two would report the move as
        // pre-existing and the proofless finding as introduced. It is asserted
        // so changing it is a moved expectation rather than a silent shift in
        // what `--scope baseline` calls new.
        MatchingCase {
            name: "a message match on the original file wins over a moved proof",
            build: |scenario| {
                scenario.write_both("src/old.rs", ORIGINAL);
                scenario.write_current("src/new.rs", ORIGINAL);
                scenario.baseline =
                    vec![diagnostic("base", Some("src/old.rs"), Some(span(1, 1, 8)))];
                scenario.current = vec![
                    // No span, so no proof: only its message can identify it.
                    diagnostic("unproven", Some("src/old.rs"), None),
                    // The proof the baseline carries, in another file.
                    diagnostic("moved", Some("src/new.rs"), Some(span(1, 1, 8))),
                ];
            },
            expected: verdict(&["moved"], &[("unproven", "base")], &[], 0),
        },
        MatchingCase {
            name: "two findings with no path at all pair on their message",
            build: |scenario| {
                scenario.baseline = vec![diagnostic("base", None, None)];
                scenario.current = vec![diagnostic("current", None, None)];
            },
            expected: verdict(&[], &[("current", "base")], &[], 0),
        },
        MatchingCase {
            name: "a proofless finding does not pair across paths",
            build: |scenario| {
                scenario.baseline = vec![diagnostic("base", None, None)];
                scenario.current = vec![diagnostic("moved", Some("src/missing.rs"), None)];
            },
            expected: verdict(&["moved"], &[], &["base"], 0),
        },
    ]
}

#[test]
fn every_matching_case_produces_the_verdict_it_names() {
    for (index, case) in identity_cases()
        .into_iter()
        .chain(pairing_cases())
        .enumerate()
    {
        let mut scenario = Scenario::new(&format!("case-{index:02}"));
        (case.build)(&mut scenario);
        assert_eq!(scenario.verdict(), case.expected, "case: {}", case.name);
    }
}

#[test]
fn source_file_over_the_source_kernel_bound_is_not_read() {
    let root = Scratch(root("source-limit"));
    let path = root.0.join("src/large.rs");
    File::create(&path)
        .unwrap()
        .set_len((SOURCE_FILE_BYTES_LIMIT + 1) as u64)
        .unwrap();
    let mut loader = EvidenceLoader::new(&root.0);
    let canonical_root = loader.root.clone().unwrap();

    assert!(
        loader
            .read_source(&canonical_root, "src/large.rs")
            .is_none()
    );
    assert_eq!(loader.source_bytes_read, 0);
    assert_eq!(loader.proof_bytes, 0);
}

#[test]
fn aggregate_proof_budget_disables_the_first_excess_proof() {
    let root = Scratch(root("proof-budget"));
    fs::write(root.0.join("src/lib.rs"), "x\n").unwrap();
    let mut loader = EvidenceLoader::new(&root.0);
    loader.proof_bytes = PROOF_BYTES_BUDGET;
    let source = "x\n";
    let lines = LineIndex::of(source);

    assert!(loader.proof(source, &lines, &span(1, 1, 2)).is_none());
    assert_eq!(loader.proof_bytes, PROOF_BYTES_BUDGET);
}

#[test]
fn fallback_matching_reserves_unavailable_baselines_for_available_currents() {
    let baseline = [
        diagnostic("base-unavailable", None, None),
        diagnostic("base-stable", None, None),
    ];
    let current = [
        diagnostic("current-unavailable", None, None),
        diagnostic("current-stable", None, None),
    ];
    let baseline_candidates = vec![
        Candidate {
            diagnostic: &baseline[0],
            fingerprint: None,
        },
        Candidate {
            diagnostic: &baseline[1],
            fingerprint: Some(DeltaFingerprintV1([1; 32])),
        },
    ];
    let current_candidates = vec![
        Candidate {
            diagnostic: &current[0],
            fingerprint: None,
        },
        Candidate {
            diagnostic: &current[1],
            fingerprint: Some(DeltaFingerprintV1([2; 32])),
        },
    ];

    let delta = match_candidates(&baseline_candidates, &current_candidates);

    assert_eq!(delta.pre_existing.len(), 2);
    assert!(delta.introduced.is_empty());
    assert!(delta.fixed.is_empty());
}

#[test]
fn multiset_matching_is_deterministic_and_bounded() {
    let mut scenario = Scenario::new("multiset");
    scenario.baseline = (0..32)
        .map(|index| diagnostic(&format!("base-{index:02}"), None, None))
        .collect::<Vec<_>>();
    scenario.current = (0..40)
        .map(|index| diagnostic(&format!("current-{index:02}"), None, None))
        .collect::<Vec<_>>();
    let expected = scenario.delta();
    for _ in 0..20 {
        assert_eq!(scenario.delta(), expected);
    }
    assert_eq!(expected.pre_existing.len(), 32);
    assert_eq!(expected.introduced.len(), 8);
    assert!(expected.fixed.is_empty());

    scenario.baseline = vec![diagnostic("same", None, None); DIAGNOSTIC_LIMIT + 1];
    scenario.current = Vec::new();
    let error = compute(
        &scenario.baseline,
        &scenario.current,
        &scenario.baseline_root.0,
        &scenario.current_root.0,
    )
    .unwrap_err();
    assert_eq!(error.stage, "delta");
    assert_eq!(error.code, "delta-limit-exceeded");
}

/// US-003: a structural finding is matched on the identity its own pass
/// computed, never on the message it published.
///
/// Every structural message states a number the next edit moves: the line count
/// of an oversized file, the occurrence count of a clone family, the two figures
/// of a hotspot. Matched on the message and on an excerpt of the unit, a finding
/// older than the branch reads as introduced by it and the one it replaced reads
/// as fixed, which is the opposite of what a baseline scope is asked for.
#[test]
fn a_structural_finding_survives_the_count_its_message_states() {
    let scenario = Scenario::new("structural");
    let baseline_root = &scenario.baseline_root.0;
    let current_root = &scenario.current_root.0;

    let mut before = diagnostic("family", Some("src/lib.rs"), Some(span(1, 1, 2)));
    before.source = DiagnosticSource::RustDoctor;
    before.code = Some(crate::policy::STRUCTURE_OVERSIZED_UNIT.id.to_owned());
    before.message = "src/lib.rs is 1200 lines long.".to_owned();

    let mut grown = before.clone();
    grown.message = "src/lib.rs is 1207 lines long.".to_owned();
    let delta = compute(
        std::slice::from_ref(&before),
        &[grown],
        baseline_root,
        current_root,
    )
    .unwrap();
    assert_eq!(delta.summary.introduced, 0, "{delta:?}");
    assert_eq!(delta.summary.pre_existing, 1, "{delta:?}");
    assert!(delta.fixed.is_empty(), "{delta:?}");

    // The same family reported from another file, which is what a clone family
    // does when a new member sorts ahead of the old first one. The identity does
    // not carry the path, so the family is still the same family.
    let mut moved = before.clone();
    moved.path = Some("src/other.rs".to_owned());
    let delta = compute(
        std::slice::from_ref(&before),
        &[moved],
        baseline_root,
        current_root,
    )
    .unwrap();
    assert_eq!(delta.summary.pre_existing, 1, "{delta:?}");
    assert_eq!(delta.summary.cross_file_matches, 1, "{delta:?}");

    // A per-site diagnostic keeps the matching it had: its message is part of
    // what identifies it, because nothing else does.
    let site = diagnostic("site", Some("src/lib.rs"), Some(span(1, 1, 2)));
    let mut reworded = site.clone();
    reworded.message = "another message".to_owned();
    let delta = compute(&[site], &[reworded], baseline_root, current_root).unwrap();
    assert_eq!(delta.summary.introduced, 1, "{delta:?}");
}

/// The one arithmetic the evidence rests on, at its edges.
#[test]
fn a_reported_position_resolves_to_the_byte_the_line_index_says() {
    let source = "ab\ncd\n";
    let lines = LineIndex::of(source);
    let at = |line, column| lines.offset(source, SourcePosition { line, column });

    assert_eq!(at(1, 1), Some(0));
    assert_eq!(at(1, 2), Some(1));
    // A line owns its terminator, so the newline is the last column it answers.
    assert_eq!(at(1, 3), Some(2));
    assert_eq!(at(1, 4), None);
    assert_eq!(at(2, 1), Some(3));
    // The empty line a trailing newline opens.
    assert_eq!(at(3, 1), Some(6));
    assert_eq!(at(3, 2), None);
    assert_eq!(at(4, 1), None);
    // Neither a line nor a column is ever zero.
    assert_eq!(at(0, 1), None);
    assert_eq!(at(1, 0), None);

    // A file with no trailing newline answers the position one past its last
    // character, which is what a span covering that line reports.
    let unterminated = "é!";
    let lines = LineIndex::of(unterminated);
    let at = |line, column| lines.offset(unterminated, SourcePosition { line, column });
    assert_eq!(at(1, 1), Some(0));
    assert_eq!(at(1, 2), Some(2));
    assert_eq!(at(1, 3), Some(3));
    assert_eq!(at(1, 4), None);
}

/// Every file of the module stays under the bound `oversized_unit` reports at,
/// tests included: the pass that decides what a branch introduced has to pass
/// the rule it publishes.
#[test]
fn the_delta_holds_the_size_bound_it_matches_for() {
    for own in [
        include_str!("../delta.rs"),
        include_str!("tests.rs"),
        include_str!("tests/oracle.rs"),
    ] {
        let lines = own.lines().count();
        assert!(
            lines < crate::structure::FILE_LINES,
            "a file of the delta is {lines} lines long, over the {} it publishes",
            crate::structure::FILE_LINES
        );
    }
}