Skip to content

Commit

Permalink
Fix 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 c284289 commit 12d8556
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions examples/restriction/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions examples/restriction/overscoped_allow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ path = "ui_test/main.rs"

[dependencies]
anyhow = "1.0"
cargo_metadata = "0.18"
clippy_utils = { workspace = true }
if_chain = "1.0"
once_cell = "1.18"
rustfix = "0.6"
serde = "1.0"
serde_json = "1.0"
Expand Down
27 changes: 26 additions & 1 deletion examples/restriction/overscoped_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ extern crate rustc_session;
extern crate rustc_span;

use anyhow::{Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
use clippy_utils::{diagnostics::span_lint_and_help, source::snippet_opt};
use dylint_internal::env::var;
use if_chain::if_chain;
use once_cell::sync::OnceCell;
use rustc_ast::ast::{Attribute, MetaItem, NestedMetaItem};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_hir::{
Expand Down Expand Up @@ -101,6 +103,7 @@ declare_lint! {

#[derive(Default)]
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>>,
Expand Down Expand Up @@ -155,11 +158,28 @@ fn read_diagnostics() -> Result<Vec<Diagnostic>> {
impl OverscopedAllow {
fn new(diagnostics: Vec<Diagnostic>) -> Self {
Self {
metadata: OnceCell::new(),
diagnostics,
ancestor_meta_item_span_map: FxHashMap::default(),
canonical_paths_cache: RefCell::new(FxHashMap::default()),
}
}

fn metadata(&self, source_path_sample: &Path) -> &Metadata {
self.metadata.get_or_init(|| {
let parent = source_path_sample.parent().unwrap();
let source_dir = if parent.as_os_str().is_empty() {
Path::new(".")
} else {
parent
};
MetadataCommand::new()
.current_dir(source_dir)
.no_deps()
.exec()
.unwrap()
})
}
}

impl<'tcx> LateLintPass<'tcx> for OverscopedAllow {
Expand Down Expand Up @@ -315,12 +335,17 @@ impl OverscopedAllow {
let Some(span_local_path) = local_path_from_span(cx, span) else {
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) = path.canonicalize();
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()
Expand Down
2 changes: 1 addition & 1 deletion examples/supplementary/escaping_doc_link/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl<'tcx> LateLintPass<'tcx> for EscapingDocLink {
}

impl EscapingDocLink {
fn metadata(&mut self, source_dir: &Path) -> &Metadata {
fn metadata(&self, source_dir: &Path) -> &Metadata {
self.metadata.get_or_init(|| {
MetadataCommand::new()
.current_dir(source_dir)
Expand Down

0 comments on commit 12d8556

Please sign in to comment.