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
use crate::analysis::{Language, ParsedFile, ParsedFunction};
use crate::index::{ImportResolution, RepositoryIndex};
use crate::model::{Finding, Severity};
pub(crate) const BINDING_LOCATION: &str = file!();
use super::common::{import_alias_lookup, is_builtin, is_global_sym};
pub(super) fn hallucination_findings(
file: &ParsedFile,
function: &ParsedFunction,
index: &RepositoryIndex,
) -> Vec<Finding> {
// Rust uses a dedicated resolution family that understands module imports,
// scoped aliases, wildcard uses, local bindings, and constructor-like call
// sites better than this shared uppercase-symbol fallback.
if file.language == Language::Rust {
return Vec::new();
}
let mut findings = Vec::new();
let package_name = match &file.package_name {
Some(package_name) => package_name,
None => return findings,
};
let Some(current_package) = index.package_for_file(file.language, &file.path, package_name)
else {
return findings;
};
let import_aliases = import_alias_lookup(&file.imports);
for call in &function.calls {
match &call.receiver {
Some(receiver) => {
if let Some(import_path) = import_aliases.get(receiver) {
match index.resolve_import_path(file.language, import_path) {
ImportResolution::Resolved(target_package) => {
if !(target_package.has_function(&call.name)
|| target_package.has_symbol(&call.name))
{
findings.push(Finding {
rule_id: "hallucinated_import_call".to_string(),
severity: Severity::Warning,
path: file.path.clone(),
function_name: Some(function.fingerprint.name.clone()),
start_line: call.line,
end_line: call.line,
message: format!(
"call to {}.{} has no matching symbol in locally indexed package {}",
receiver, call.name, import_path
),
evidence: vec![
format!("import alias {} resolves to {}", receiver, import_path),
format!(
"matched local package {} in directory {}",
target_package.package_name,
target_package.directory_display()
),
format!(
"locally indexed package {} in {} does not expose {}",
target_package.package_name,
target_package.directory_display(),
call.name
),
],
});
}
}
ImportResolution::Ambiguous(candidates) => {
let _candidate_count = candidates.len();
continue;
}
ImportResolution::Unresolved => continue,
}
} else if current_package.has_method(receiver, &call.name) {
continue;
}
}
None => {
if is_builtin(&call.name) || !is_global_sym(&call.name) {
continue;
}
if let Some(import_path) = import_aliases.get(&call.name) {
match index.resolve_import_path(file.language, import_path) {
ImportResolution::Resolved(target_package) => {
let imported_name =
import_path.rsplit('.').next().unwrap_or(call.name.as_str());
if target_package.has_function(imported_name)
|| target_package.has_symbol(imported_name)
{
continue;
}
findings.push(Finding {
rule_id: "hallucinated_import_call".to_string(),
severity: Severity::Warning,
path: file.path.clone(),
function_name: Some(function.fingerprint.name.clone()),
start_line: call.line,
end_line: call.line,
message: format!(
"call to {} has no matching imported symbol in locally indexed package {}",
call.name, import_path
),
evidence: vec![
format!("import alias {} resolves to {}", call.name, import_path),
format!(
"locally indexed package {} in {} does not expose {}",
target_package.package_name,
target_package.directory_display(),
imported_name
),
],
});
continue;
}
ImportResolution::Ambiguous(_) | ImportResolution::Unresolved => {
continue;
}
}
}
if !(current_package.has_function(&call.name)
|| current_package.has_symbol(&call.name))
{
findings.push(Finding {
rule_id: "hallucinated_local_call".to_string(),
severity: Severity::Info,
path: file.path.clone(),
function_name: Some(function.fingerprint.name.clone()),
start_line: call.line,
end_line: call.line,
message: format!(
"call to {} has no matching symbol in package {}",
call.name, package_name
),
evidence: vec![format!(
"package {} in directory {} was indexed locally but {} was not found",
package_name,
current_package.directory_display(),
call.name
)],
});
}
}
}
}
findings
}