rulewright 0.1.2

Executable engineering standards for Rust workspaces
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
// #rw(file: rust_default_hasher) trusted rule-name sets in the cold report path; fast-hasher dependency not warranted

use std::{collections::BTreeMap, io::Write as _, process::ExitCode};

use crate::output;
use crate::{
    file,
    path::{Path, PathBuf},
};
use tabled::Tabled;

use crate::{
    Config, Fix, RuleRegistry, Violation,
    infra::{ignore, walk},
};

/// Print every `#rw(...)` directive found across the selected packages as a grouped table.
pub fn report_suppressions(
    registry: &RuleRegistry,
    package_filter: &[String],
    config: &Config,
    root: &Path,
    workspace_root: &Path,
    quiet: bool,
) -> ExitCode {
    let paths = match walk::rs_paths(workspace_root, package_filter) {
        Ok(paths) => paths,

        Err(error) => {
            if !quiet {
                output::error(&format!("suppression report aborted: {error}"));
            }

            return ExitCode::FAILURE;
        }
    };

    let registered_names: std::collections::HashSet<&str> = registry
        .rules()
        .iter()
        .copied()
        .map(|rule| rule.info.name)
        .collect();

    let all_entries = match load_suppressions(&paths, root, &registered_names) {
        Ok(entries) => entries,
        Err(failures) => return report_read_failures(failures, quiet),
    };

    if quiet {
        // #rw(rust_println) quiet mode only prints count
        eprintln!("{}", all_entries.len());

        return ExitCode::SUCCESS;
    }

    if all_entries.is_empty() {
        output::success("no suppression directives found");
        output::blank();

        return ExitCode::SUCCESS;
    }

    let _ = config;
    let mut by_rule: BTreeMap<String, Vec<&ignore::SuppressionEntry>> = BTreeMap::new();

    for entry in &all_entries {
        if entry.wildcard {
            by_rule.entry("*".to_string()).or_default().push(entry);
        } else {
            for rule in &entry.rules {
                by_rule.entry(rule.clone()).or_default().push(entry);
            }
        }
    }

    let mut rows: Vec<SuppressionRow> = Vec::with_capacity(by_rule.len());

    for (rule, entries) in by_rule {
        output::header(&format!("{rule} ({} suppression(s))", entries.len()));

        for e in &entries {
            output::detail(&format!(
                "  {}:{} [{}] {}",
                e.rel,
                e.line,
                e.scope.prefix(),
                e.reason,
            ));
        }

        output::blank();
        rows.push(SuppressionRow {
            rule,
            count: entries.len(),
        });
    }

    rows.sort_by_key(|a| std::cmp::Reverse(a.count));
    output::separator();
    output::blank();
    output::table(rows);
    output::blank();
    output::detail(&format!(
        "{} directive(s) across {} file(s)",
        all_entries.len(),
        {
            let mut files: std::collections::HashSet<&str> = std::collections::HashSet::new();
            for e in &all_entries {
                files.insert(&e.rel);
            }
            files.len()
        }
    ));
    output::blank();

    ExitCode::SUCCESS
}

fn load_suppressions(
    paths: &[PathBuf],
    root: &Path,
    registered_names: &std::collections::HashSet<&str>,
) -> Result<Vec<ignore::SuppressionEntry>, Vec<String>> {
    let mut all_entries = Vec::new();
    let mut failures = Vec::with_capacity(paths.len());

    for path in paths {
        let relative = path.strip_prefix(root).unwrap_or(path);
        let Some(rel) = relative.to_slash() else {
            failures.push(format!(
                "{}: non-UTF-8 source paths cannot be reported safely",
                path.display()
            ));
            continue;
        };
        let contents = match file::read_text(path) {
            Ok(contents) => contents,

            Err(error) => {
                failures.push(format!("{rel}: failed to read source: {error}"));
                continue;
            }
        };
        let lines: Vec<&str> = contents.lines().collect();
        let directive_lines = crate::infra::scanner::directive_source_lines(&contents, &lines);
        let mut errors = Vec::new();
        let suppressed =
            ignore::suppressed_lines(&rel, &directive_lines, &mut errors, Some(registered_names));

        all_entries.extend(suppressed.entries);
    }

    if failures.is_empty() {
        Ok(all_entries)
    } else {
        failures.sort_unstable();
        failures.dedup();

        Err(failures)
    }
}

fn report_read_failures(failures: Vec<String>, quiet: bool) -> ExitCode {
    if !quiet {
        for failure in failures {
            output::error(&failure);
        }

        output::error("suppression report aborted because analysis was incomplete");
    }

    ExitCode::FAILURE
}

#[derive(Tabled)]
#[tabled(crate = "tabled")]
struct SuppressionRow {
    #[tabled(rename = "Rule")]
    rule: String,
    #[tabled(rename = "Count")]
    count: usize,
}

#[cfg(test)]
mod tests {
    use std::collections::HashSet;

    use super::*;

    #[cfg(unix)]
    #[test]
    fn suppression_report_rejects_non_utf8_path_identity() {
        use std::os::unix::ffi::OsStringExt as _;

        let directory = crate::temporary::Directory::new().expect("temporary directory");
        let name = std::ffi::OsString::from_vec(b"source-\xff.rs".to_vec());
        let path = directory.path().join(std::path::PathBuf::from(name));

        let errors = load_suppressions(&[path], directory.path(), &HashSet::new())
            .expect_err("lossy report identity must fail");

        assert!(errors[0].contains("non-UTF-8 source paths"));
    }
}

pub(super) fn print_grouped(
    violations: &[Violation],
    _registry: &RuleRegistry,
    packages: &[super::PackageRoot],
    root: &Path,
) {
    let mut by_package: BTreeMap<&str, Vec<&Violation>> = BTreeMap::new();
    let mut by_rule: BTreeMap<&str, (usize, usize)> = BTreeMap::new();

    for v in violations {
        by_package
            .entry(package_name(&v.rel, packages, root))
            .or_default()
            .push(v);
        let (total, fixable) = by_rule.entry(v.rule_name()).or_default();

        *total += 1;
        *fixable += usize::from(v.is_fixable());
    }

    for (package_name, violations) in &by_package {
        output::header(&format!("{package_name} ({} issues)", violations.len()));

        for v in violations {
            let location = v.column.map_or_else(
                || format!("{}:{}", v.rel, v.line),
                |column| format!("{}:{}:{column}", v.rel, v.line),
            );

            output::error(&format!("{location}: {}", v.message));
        }

        output::blank();
    }

    output::separator();
    output::blank();

    let mut rule_counts: Vec<RuleSummaryRow> = by_rule
        .into_iter()
        .map(|(rule, (count, fixable))| RuleSummaryRow {
            rule: rule.to_string(),
            fixable: if fixable > 0 {
                fixable.to_string()
            } else {
                String::new()
            },
            count,
        })
        .collect();

    rule_counts.sort_by_key(|a| std::cmp::Reverse(a.count));

    output::table(rule_counts);
    output::blank();

    let fixable_count: usize = violations
        .iter()
        .filter(|violation| violation.is_fixable())
        .count();

    output::error(&format!(
        "{} violation(s) in {} package(s)",
        violations.len(),
        by_package.len()
    ));

    if fixable_count > 0 {
        output::detail(&format!(
            "{fixable_count} of these can be auto-fixed with --fix"
        ));
    }
}

#[derive(serde::Serialize)]
struct FindingsDocument<'a> {
    schema_version: u32,
    findings: Vec<StructuredFinding<'a>>,
}

#[derive(serde::Serialize)]
struct StructuredFinding<'a> {
    id: String,
    path: &'a str,
    line: usize,
    column: Option<usize>,
    rule: &'a str,
    severity: &'a str,
    message: &'a str,
    description: &'a str,
    guidance: &'a str,
    fixable: bool,
}

pub(super) fn print_json(violations: &[Violation], registry: &RuleRegistry) {
    let findings = violations
        .iter()
        .map(|violation| {
            let rule = registry
                .rules()
                .iter()
                .copied()
                .find(|rule| rule.info.name == violation.rule_name());

            StructuredFinding {
                id: finding_id(violation),
                path: &violation.rel,
                line: violation.line,
                column: violation.column,
                rule: violation.rule_name(),
                severity: rule.map_or("unknown", |rule| rule.info.severity.as_str()),
                message: &violation.message,
                description: rule.map_or("", |rule| rule.info.description),
                guidance: rule.map_or("", |rule| rule.info.justification),
                fixable: violation.is_fixable(),
            }
        })
        .collect();
    let document = FindingsDocument {
        schema_version: 2,
        findings,
    };
    let stdout = std::io::stdout();
    let mut output = stdout.lock();

    if serde_json::to_writer_pretty(&mut output, &document).is_ok() {
        let _ = output.write_all(b"\n");
    }
}

fn finding_id(violation: &Violation) -> String {
    let mut hasher = blake3::Hasher::new();

    for part in [
        violation.rule_name().as_bytes(),
        violation.rel.as_bytes(),
        violation.message.as_bytes(),
    ] {
        hasher.update(&(part.len() as u64).to_le_bytes());
        hasher.update(part);
    }

    hasher.finalize().to_hex().to_string()
}

#[cfg(test)]
mod structured_tests {
    use super::*;

    #[test]
    fn finding_ids_survive_line_and_column_movement() {
        let original = crate::violation("src/lib.rs", 4, "finding")
            .with_column(8)
            .with_rule("rust_example");
        let moved = crate::violation("src/lib.rs", 400, "finding")
            .with_column(16)
            .with_rule("rust_example");

        assert_eq!(finding_id(&original), finding_id(&moved));
    }

    #[test]
    fn finding_ids_distinguish_rules_paths_and_messages() {
        let finding = crate::violation("src/lib.rs", 4, "finding").with_rule("rust_example");

        for distinct in [
            crate::violation("src/lib.rs", 4, "other").with_rule("rust_example"),
            crate::violation("src/other.rs", 4, "finding").with_rule("rust_example"),
            crate::violation("src/lib.rs", 4, "finding").with_rule("rust_other"),
        ] {
            assert_ne!(finding_id(&finding), finding_id(&distinct));
        }
    }
}

#[derive(Tabled)]
#[tabled(crate = "tabled")]
struct RuleSummaryRow {
    #[tabled(rename = "Rule")]
    rule: String,
    #[tabled(rename = "Auto-fix")]
    fixable: String,
    #[tabled(rename = "Count")]
    count: usize,
}

pub(super) fn print_dry_run(fixes: &[(String, Fix)]) {
    let mut by_file: BTreeMap<&str, Vec<&Fix>> = BTreeMap::new();

    for (rel, fix) in fixes {
        by_file.entry(rel).or_default().push(fix);
    }

    for (rel, file_fixes) in &by_file {
        output::header(&format!("{rel} ({} changed span(s))", file_fixes.len()));

        for f in file_fixes {
            if f.start_line > f.end_line {
                output::detail(&format!(
                    "  insert {} line(s) at line {}",
                    f.replacement.lines().count(),
                    f.start_line
                ));
            } else if f.replacement.is_empty() {
                output::detail(&format!("  delete lines {}-{}", f.start_line, f.end_line));
            } else {
                let new_lines = f.replacement.lines().count();

                output::detail(&format!(
                    "  replace lines {}-{} ({} line(s))",
                    f.start_line, f.end_line, new_lines
                ));
            }
        }

        output::blank();
    }
}

fn package_name<'a>(rel: &'a str, packages: &'a [super::PackageRoot], root: &Path) -> &'a str {
    super::package_name_for_path(packages, &root.join(rel)).unwrap_or(rel)
}