stern4rust/rules/source/
imported_paths_rule.rs1use crate::finding::model::qualified_call::QualifiedCall;
6use crate::finding::parsing::qualified_call_finder::QualifiedCallFinder;
7use crate::reporting::offence::Offence;
8use crate::rule::Rule;
9use crate::source_file::SourceFile;
10
11pub struct ImportedPathsRule;
26
27impl ImportedPathsRule {
28 pub fn new() -> Self {
29 Self
30 }
31
32 fn offence(&self, file: &SourceFile, call: &QualifiedCall) -> Offence {
33 Offence::new(
34 file.relative_path(),
35 call.line,
36 self.name(),
37 format!(
38 "`{}` is reached through a path; no import of this file names it",
39 call.path
40 ),
41 format!("add `use {};` and call `{}`", call.import(), call.call()),
42 )
43 .with_subject(&call.path)
44 }
45}
46
47impl Default for ImportedPathsRule {
48 fn default() -> Self {
49 Self::new()
50 }
51}
52
53impl Rule for ImportedPathsRule {
54 fn name(&self) -> &'static str {
55 "imported-paths"
56 }
57
58 fn check(&self, file: &SourceFile) -> Vec<Offence> {
59 QualifiedCallFinder::find(file)
60 .unwrap_or_default()
61 .iter()
62 .map(|call| self.offence(file, call))
63 .collect()
64 }
65
66 fn check_workspace(&self, _files: &[SourceFile]) -> Vec<Offence> {
67 Vec::new()
68 }
69
70 fn requirement(&self) -> Option<&'static str> {
71 None
72 }
73
74 fn is_configured(&self) -> bool {
75 true
76 }
77}