Merge pull request #13657 from jdonszelmann/disallowed-macros-to-early

collect attribute spans early for disallowed macros
This commit is contained in:
Philipp Krones 2024-11-07 19:23:20 +00:00 committed by GitHub
commit f712eb5cdc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 98 additions and 38 deletions

View file

@ -0,0 +1,40 @@
use std::mem;
use std::sync::OnceLock;
use rustc_ast::{Attribute, Crate};
use rustc_data_structures::sync::Lrc;
use rustc_lint::{EarlyContext, EarlyLintPass};
use rustc_session::impl_lint_pass;
use rustc_span::Span;
#[derive(Clone, Default)]
pub struct AttrStorage(pub Lrc<OnceLock<Vec<Span>>>);
pub struct AttrCollector {
storage: AttrStorage,
attrs: Vec<Span>,
}
impl AttrCollector {
pub fn new(storage: AttrStorage) -> Self {
Self {
storage,
attrs: Vec::new(),
}
}
}
impl_lint_pass!(AttrCollector => []);
impl EarlyLintPass for AttrCollector {
fn check_attribute(&mut self, _cx: &EarlyContext<'_>, attr: &Attribute) {
self.attrs.push(attr.span);
}
fn check_crate_post(&mut self, _: &EarlyContext<'_>, _: &Crate) {
self.storage
.0
.set(mem::take(&mut self.attrs))
.expect("should only be called once");
}
}

View file

@ -1,5 +1,7 @@
pub mod attr_collector;
pub mod author;
pub mod dump_hir;
pub mod format_args_collector;
#[cfg(feature = "internal")]
pub mod internal_lints;