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
// Copyright 2025 Umberto Gotti <umberto.gotti@umbertogotti.dev>
// Licensed under the MIT License
// SPDX-License-Identifier: MIT
// A call reached through a path the file never imported.
//
// The correction is the interesting part. Every such path can be repaired by
// importing enough of it that at most one segment is left at the call site, and
// the two shapes differ: `syn::parse_file` has nothing worth keeping as a
// qualifier, so the whole path is imported and the call becomes `parse_file`,
// while `std::env::args` keeps `env` because it says something -- `use std::env`
// and `env::args`.
pub struct QualifiedCall {
pub path: String,
pub line: usize,
}
impl QualifiedCall {
pub fn new(path: &str, line: usize) -> Self {
Self {
path: path.to_string(),
line,
}
}
// What the call site reads as afterwards: the last segment on its own, or
// the last two when a module qualifier is worth keeping.
pub fn call(&self) -> String {
let segments = self.segments();
if segments.len() <= 2 {
return segments.last().copied().unwrap_or_default().to_string();
}
segments[segments.len() - 2..].join("::")
}
// What to import so that at most one imported segment is left at the call.
pub fn import(&self) -> String {
let segments = self.segments();
if segments.len() <= 2 {
return self.path.clone();
}
segments[..segments.len() - 1].join("::")
}
fn segments(&self) -> Vec<&str> {
self.path.split("::").collect()
}
}