Skip to content

Commit 3e60ba0

Browse files
committed
lintcheck: fix bug where lint messages about macros coming from crate deps would sneak in absolute paths to registry sources.
make the path a relative path that starts at the CARGO_HOME to not print the users home location in the log
1 parent d859a17 commit 3e60ba0

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

lintcheck/src/main.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#![allow(clippy::filter_map, clippy::collapsible_else_if)]
99

10+
use std::ffi::OsStr;
1011
use std::process::Command;
1112
use std::sync::atomic::{AtomicUsize, Ordering};
1213
use std::{collections::HashMap, io::ErrorKind};
@@ -486,13 +487,32 @@ fn read_crates(toml_path: &Path) -> Vec<CrateSource> {
486487
fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning {
487488
let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e));
488489

490+
let file: String = jmsg["message"]["spans"][0]["file_name"]
491+
.to_string()
492+
.trim_matches('"')
493+
.into();
494+
495+
let file = if file.contains(".cargo") {
496+
// if we deal with macros, a filename may show the origin of a macro which can be inside a dep from
497+
// the registry.
498+
// don't show the full path in that case.
499+
500+
// /home/matthias/.cargo/registry/src/i.8713187.xyz-1ecc6299db9ec823/syn-1.0.63/src/custom_keyword.rs
501+
let path = PathBuf::from(file);
502+
let mut piter = path.into_iter();
503+
// consume all elements until we find ".cargo", so that "/home/matthias" is skipped
504+
let _: Option<&OsStr> = piter.find(|x| x == &std::ffi::OsString::from(".cargo"));
505+
// collect the remaining segments
506+
let file = piter.collect::<PathBuf>();
507+
format!("{}", file.display())
508+
} else {
509+
file
510+
};
511+
489512
ClippyWarning {
490513
crate_name: krate.name.to_string(),
491514
crate_version: krate.version.to_string(),
492-
file: jmsg["message"]["spans"][0]["file_name"]
493-
.to_string()
494-
.trim_matches('"')
495-
.into(),
515+
file,
496516
line: jmsg["message"]["spans"][0]["line_start"]
497517
.to_string()
498518
.trim_matches('"')

0 commit comments

Comments
 (0)