From 3e60ba073b1d7f6a44c0ad146a951445c1ae4a92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Thu, 11 Mar 2021 14:29:00 +0100 Subject: [PATCH] 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 --- lintcheck/src/main.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/lintcheck/src/main.rs b/lintcheck/src/main.rs index c027f11df..1964afd36 100644 --- a/lintcheck/src/main.rs +++ b/lintcheck/src/main.rs @@ -7,6 +7,7 @@ #![allow(clippy::filter_map, clippy::collapsible_else_if)] +use std::ffi::OsStr; use std::process::Command; use std::sync::atomic::{AtomicUsize, Ordering}; use std::{collections::HashMap, io::ErrorKind}; @@ -486,13 +487,32 @@ fn read_crates(toml_path: &Path) -> Vec { fn parse_json_message(json_message: &str, krate: &Crate) -> ClippyWarning { let jmsg: Value = serde_json::from_str(&json_message).unwrap_or_else(|e| panic!("Failed to parse json:\n{:?}", e)); + let file: String = jmsg["message"]["spans"][0]["file_name"] + .to_string() + .trim_matches('"') + .into(); + + let file = if file.contains(".cargo") { + // if we deal with macros, a filename may show the origin of a macro which can be inside a dep from + // the registry. + // don't show the full path in that case. + + // /home/matthias/.cargo/registry/src/github.com-1ecc6299db9ec823/syn-1.0.63/src/custom_keyword.rs + let path = PathBuf::from(file); + let mut piter = path.into_iter(); + // consume all elements until we find ".cargo", so that "/home/matthias" is skipped + let _: Option<&OsStr> = piter.find(|x| x == &std::ffi::OsString::from(".cargo")); + // collect the remaining segments + let file = piter.collect::(); + format!("{}", file.display()) + } else { + file + }; + ClippyWarning { crate_name: krate.name.to_string(), crate_version: krate.version.to_string(), - file: jmsg["message"]["spans"][0]["file_name"] - .to_string() - .trim_matches('"') - .into(), + file, line: jmsg["message"]["spans"][0]["line_start"] .to_string() .trim_matches('"')