diff --git a/src/bans.rs b/src/bans.rs index 2ec551df..eeb95253 100644 --- a/src/bans.rs +++ b/src/bans.rs @@ -286,7 +286,7 @@ pub fn check( let mut kids = smallvec::SmallVec::<[Dupe; 2]>::new(); for dup in multi_detector.dupes.iter().cloned() { - let span = &ctx.krate_spans[dup]; + let span = &ctx.krate_spans[dup].total; if span.start < all_start { all_start = span.start; diff --git a/src/diag.rs b/src/diag.rs index 65f65f67..c2d082a1 100644 --- a/src/diag.rs +++ b/src/diag.rs @@ -146,7 +146,8 @@ where pub type Span = std::ops::Range; pub struct KrateSpan { - span: Span, + pub total: Span, + pub source: usize, } pub struct KrateSpans { @@ -155,11 +156,11 @@ pub struct KrateSpans { } impl std::ops::Index for KrateSpans { - type Output = Span; + type Output = KrateSpan; #[inline] fn index(&self, i: usize) -> &Self::Output { - &self.spans[i].span + &self.spans[i] } } @@ -182,24 +183,18 @@ impl KrateSpans { krates.sort_unstable_by_key(|a| (&a.name, &a.version)); for krate in krates { let span_start = sl.len(); - match &krate.source { - Some(src) => writeln!(sl, "{} {} {}", krate.name, krate.version, src) - .expect("unable to synthesize lockfile"), - None => writeln!( - sl, - "{} {} {}", - krate.name, - krate.version, - krate.manifest_path.parent().unwrap() - ) - .expect("unable to synthesize lockfile"), + let source = if krate.source.is_some() { + krate.id.source() + } else { + krate.manifest_path.parent().unwrap().as_str() }; - let span_end = sl.len() - 1; + writeln!(sl, "{} {} {source}", krate.name, krate.version) + .expect("unable to synthesize lockfile"); - spans.push(KrateSpan { - span: span_start..span_end, - }); + let total = span_start..sl.len() - 1; + let source = total.end - source.len(); + spans.push(KrateSpan { total, source }); let mut sl2 = String::with_capacity(4 * 1024); let mut deps_map = HashMap::new(); @@ -223,14 +218,14 @@ impl KrateSpans { #[inline] pub fn label_for_index(&self, krate_index: usize, msg: impl Into) -> Label { - Label::secondary(self.file_id, self.spans[krate_index].span.clone()).with_message(msg) + Label::secondary(self.file_id, self.spans[krate_index].total.clone()).with_message(msg) } #[inline] pub fn get_coord(&self, krate_index: usize) -> KrateCoord { KrateCoord { file: self.file_id, - span: self.spans[krate_index].span.clone(), + span: self.spans[krate_index].total.clone(), } } } diff --git a/src/sources.rs b/src/sources.rs index 1c485d24..c57219ca 100644 --- a/src/sources.rs +++ b/src/sources.rs @@ -47,18 +47,9 @@ pub fn check(ctx: crate::CheckCtx<'_, ValidConfig>, sink: impl Into) let mut sl = None; let label = || { - let mut span = ctx.krate_spans[i].clone(); - - // The krate span is the complete id, but we only want - // to highlight the source component - if let Some(last_space) = krate.id.repr.rfind(' ') { - span.start = span.start + last_space + 1; - } else { - // Nightly 1.77.0 has changed the internal representation - span.end = krate.id.repr.rfind('#').unwrap() - 1; - } - - Label::primary(ctx.krate_spans.file_id, span).with_message("source") + let span = &ctx.krate_spans[i]; + Label::primary(ctx.krate_spans.file_id, span.source..span.total.end) + .with_message("source") }; // get allowed list of sources to check