Skip to content

Commit

Permalink
Simplify overscoped_allow path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Nov 11, 2023
1 parent 12d8556 commit 3d9f8fa
Showing 1 changed file with 12 additions and 22 deletions.
34 changes: 12 additions & 22 deletions examples/restriction/overscoped_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use rustc_span::{sym, BytePos, CharPos, FileLines, FileName, RealFileName, Span,
use rustfix::diagnostics::{Diagnostic, DiagnosticSpan};
use serde::Deserialize;
use std::{
cell::RefCell,
borrow::Cow,
fs::OpenOptions,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -106,7 +106,6 @@ struct OverscopedAllow {
metadata: OnceCell<Metadata>,
diagnostics: Vec<Diagnostic>,
ancestor_meta_item_span_map: FxHashMap<HirId, FxHashMap<Span, FxHashSet<Option<Span>>>>,
canonical_paths_cache: RefCell<FxHashMap<PathBuf, PathBuf>>,
}

impl_lint_pass!(OverscopedAllow => [OVERSCOPED_ALLOW]);
Expand Down Expand Up @@ -161,7 +160,6 @@ impl OverscopedAllow {
metadata: OnceCell::new(),
diagnostics,
ancestor_meta_item_span_map: FxHashMap::default(),
canonical_paths_cache: RefCell::new(FxHashMap::default()),
}
}

Expand Down Expand Up @@ -336,26 +334,9 @@ impl OverscopedAllow {
return false;
};
let metadata = self.metadata(&span_local_path);
let lhs = &span_local_path;
let rhs = Path::new(&diagnostic_span.file_name);
for path in [lhs, rhs] {
if_chain! {
if self.canonical_paths_cache.borrow().get(path).is_none();
if let Ok(canonical_path) = if path.is_absolute() {
path.canonicalize()
} else {
metadata.workspace_root.as_std_path().join(path).canonicalize()
};
then {
self.canonical_paths_cache
.borrow_mut()
.insert(path.to_path_buf(), canonical_path);
}
}
}
let lhs = &absolutize(metadata, &span_local_path);
let rhs = &absolutize(metadata, Path::new(&diagnostic_span.file_name));
if_chain! {
if let Some(lhs) = self.canonical_paths_cache.borrow().get(lhs);
if let Some(rhs) = self.canonical_paths_cache.borrow().get(rhs);
if lhs == rhs;
if let Ok(FileLines { lines, .. }) = cx.sess().source_map().span_to_lines(span);
if let Some(first_line) = lines.first();
Expand All @@ -376,6 +357,15 @@ impl OverscopedAllow {
}
}

fn absolutize<'a>(metadata: &Metadata, path: &'a Path) -> Cow<'a, Path> {
if path.is_absolute() {
assert!(path.starts_with(metadata.workspace_root.as_std_path()));
Cow::Borrowed(path)
} else {
Cow::Owned(metadata.workspace_root.as_std_path().join(path))
}
}

fn include_trailing_semicolons(cx: &LateContext<'_>, mut span: Span) -> Span {
// smoelius: I have seen `span_to_lines` fail on real code.
let Ok(FileLines { file, .. }) = cx.sess().source_map().span_to_lines(span) else {
Expand Down

0 comments on commit 3d9f8fa

Please sign in to comment.