Skip to content

Commit a42d7c7

Browse files
augustin-vsmoelius
authored andcommitted
feat: workspace root path support
1 parent b5cf611 commit a42d7c7

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

examples/supplementary/nonexistent_path_in_comment/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ publish = false
1010
crate-type = ["cdylib"]
1111

1212
[dependencies]
13+
cargo_metadata = { workspace = true }
1314
clippy_utils = { git = "https://github.com/rust-lang/rust-clippy", rev = "19e305bb57a7595f2a8d81f521c0dd8bf854e739" }
1415
dylint_linting = { path = "../../../utils/linting" }
1516
regex = { workspace = true }

examples/supplementary/nonexistent_path_in_comment/src/lib.rs

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
extern crate rustc_span;
66

7+
use cargo_metadata::MetadataCommand;
78
use clippy_utils::diagnostics::span_lint_and_help;
89
use once_cell::sync::Lazy;
910
use regex::Regex;
1011
use rustc_lint::{LateContext, LateLintPass};
1112
use rustc_span::{BytePos, FileName, Span, SyntaxContext};
12-
use std::path::Path;
1313

1414
dylint_linting::declare_late_lint! {
1515
/// ### What it does
@@ -85,36 +85,63 @@ impl<'tcx> LateLintPass<'tcx> for NonexistentPathInComment {
8585

8686
fn check_comment(cx: &LateContext<'_>, span: Span, comment_text: &str, filename: &FileName) {
8787
let base_dir = match filename {
88-
FileName::Real(real_filename) => Path::new(real_filename.local_path().unwrap())
88+
FileName::Real(real_filename) => real_filename
89+
.local_path()
90+
.expect("failed getting path")
8991
.parent()
9092
.unwrap()
9193
.to_path_buf(),
9294
_ => return,
9395
};
9496

95-
for captures in PATH_REGEX.captures_iter(comment_text) {
96-
let path_str = &captures[1];
97+
let metadata = MetadataCommand::new()
98+
.current_dir(&base_dir)
99+
.no_deps()
100+
.exec()
101+
.expect("failed getting metadata");
102+
103+
for caps in PATH_REGEX.captures_iter(comment_text) {
104+
let path_str = &caps[1];
97105
let full_path = base_dir.join(path_str);
98106

99-
if !full_path.exists() {
100-
let path_start = captures.get(1).unwrap().start();
101-
let path_end = captures.get(1).unwrap().end();
102-
let path_span = Span::new(
103-
span.lo() + BytePos(path_start as u32),
104-
span.lo() + BytePos(path_end as u32),
105-
span.ctxt(),
106-
None,
107-
);
108-
109-
span_lint_and_help(
110-
cx,
111-
NONEXISTENT_PATH_IN_COMMENT,
112-
path_span,
113-
"referenced path does not exist",
114-
None,
115-
"verify the path is correct or remove the reference",
116-
);
107+
if full_path.exists() {
108+
continue;
109+
}
110+
111+
if let Some(root_pkg) = metadata.root_package() {
112+
if let Some(manifest_parent) = root_pkg.manifest_path.parent() {
113+
let manifest_dir = manifest_parent.as_std_path();
114+
115+
let candidate_from_root =
116+
if let Some(stripped) = path_str.strip_prefix(&root_pkg.name) {
117+
let stripped = stripped.strip_prefix('/').unwrap_or(stripped);
118+
manifest_dir.join(stripped)
119+
} else {
120+
manifest_dir.join(path_str)
121+
};
122+
123+
if candidate_from_root.exists() {
124+
continue;
125+
}
126+
}
117127
}
128+
129+
let path_start = caps.get(1).unwrap().start();
130+
let path_end = caps.get(1).unwrap().end();
131+
let path_span = Span::new(
132+
span.lo() + BytePos(path_start as u32),
133+
span.lo() + BytePos(path_end as u32),
134+
span.ctxt(),
135+
None,
136+
);
137+
span_lint_and_help(
138+
cx,
139+
NONEXISTENT_PATH_IN_COMMENT,
140+
path_span,
141+
"referenced path does not exist",
142+
None,
143+
"verify the path is correct or remove the reference",
144+
);
118145
}
119146
}
120147

examples/supplementary/nonexistent_path_in_comment/ui/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@
1414
// Single dot path that does not exist
1515
// The span ./ido/not/exist.rs only points to the path
1616

17+
// Workspace root path that does exist
18+
// supplementary/Cargo.toml
19+
1720
fn main() {}

0 commit comments

Comments
 (0)