fallow_api/audit_run/
own_config.rs1use std::path::{Path, PathBuf};
4
5use fallow_config::{ResolvedConfig, Severity};
6use fallow_types::results::AnalysisResults;
7use rustc_hash::FxHashSet;
8
9use crate::audit_keys::collection_keys;
10
11#[must_use]
17pub fn config_path_under_root(config_path: &Path, root: &Path) -> Option<PathBuf> {
18 let config_path = dunce::canonicalize(config_path).ok()?;
19 let root = dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf());
20 config_path.strip_prefix(&root).ok().map(Path::to_path_buf)
21}
22
23#[must_use]
35pub fn own_config_unused_code_keys(
36 own: &AnalysisResults,
37 own_config: &ResolvedConfig,
38 candidates: &AnalysisResults,
39 root: &Path,
40) -> FxHashSet<String> {
41 let mut keys: FxHashSet<String> = collection_keys(&own.unused_files, root)
42 .into_iter()
43 .chain(collection_keys(&own.unused_exports, root))
44 .collect();
45 let files = collection_keys(&candidates.unused_files, root);
46 for (finding, key) in candidates.unused_files.iter().zip(files) {
47 if hides_unused_file(own_config, relative(&finding.file.path, root)) {
48 keys.insert(key);
49 }
50 }
51 let unused_files: FxHashSet<&Path> = own
52 .unused_files
53 .iter()
54 .map(|finding| relative(&finding.file.path, root))
55 .collect();
56 let exports = collection_keys(&candidates.unused_exports, root);
57 for (finding, key) in candidates.unused_exports.iter().zip(exports) {
58 let export = &finding.export;
59 let path = relative(&export.path, root);
60 if unused_files.contains(path) || hides_unused_export(own_config, path, &export.export_name)
61 {
62 keys.insert(key);
63 }
64 }
65 keys
66}
67
68fn relative<'a>(path: &'a Path, root: &Path) -> &'a Path {
69 path.strip_prefix(root).unwrap_or(path)
70}
71
72fn hides_path(config: &ResolvedConfig, relative: &Path) -> bool {
75 config.ignore_patterns.is_match(relative)
76 || config
77 .ignore_findings
78 .is_ignored_path(&config.root, relative)
79}
80
81fn hides_unused_file(config: &ResolvedConfig, relative: &Path) -> bool {
82 hides_path(config, relative)
83 || config.resolve_rules_for_path(relative).unused_files == Severity::Off
84}
85
86fn hides_unused_export(config: &ResolvedConfig, relative: &Path, name: &str) -> bool {
87 if hides_path(config, relative)
88 || config.resolve_rules_for_path(relative).unused_exports == Severity::Off
89 {
90 return true;
91 }
92 let file = relative.to_string_lossy();
93 config.compiled_ignore_exports.iter().any(|rule| {
94 rule.matcher.is_match(file.as_ref())
95 && rule
96 .exports
97 .iter()
98 .any(|export| export == "*" || export == name)
99 })
100}