Skip to content

Commit b7199a6

Browse files
authored
Rollup merge of #140419 - Jarcho:ctxt_external, r=Nadrieril
Move `in_external_macro` to `SyntaxContext` There are a few places in clippy where spans are passed solely to use the context, but we can't pass just the context around because of this function.
2 parents 4a8dbe0 + ea68445 commit b7199a6

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

compiler/rustc_span/src/hygiene.rs

+25
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use tracing::{debug, trace};
4141

4242
use crate::def_id::{CRATE_DEF_ID, CrateNum, DefId, LOCAL_CRATE, StableCrateId};
4343
use crate::edition::Edition;
44+
use crate::source_map::SourceMap;
4445
use crate::symbol::{Symbol, kw, sym};
4546
use crate::{DUMMY_SP, HashStableContext, Span, SpanDecoder, SpanEncoder, with_session_globals};
4647

@@ -907,6 +908,30 @@ impl SyntaxContext {
907908
pub fn edition(self) -> Edition {
908909
HygieneData::with(|data| data.expn_data(data.outer_expn(self)).edition)
909910
}
911+
912+
/// Returns whether this context originates in a foreign crate's external macro.
913+
///
914+
/// This is used to test whether a lint should not even begin to figure out whether it should
915+
/// be reported on the current node.
916+
pub fn in_external_macro(self, sm: &SourceMap) -> bool {
917+
let expn_data = self.outer_expn_data();
918+
match expn_data.kind {
919+
ExpnKind::Root
920+
| ExpnKind::Desugaring(
921+
DesugaringKind::ForLoop
922+
| DesugaringKind::WhileLoop
923+
| DesugaringKind::OpaqueTy
924+
| DesugaringKind::Async
925+
| DesugaringKind::Await,
926+
) => false,
927+
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
928+
ExpnKind::Macro(MacroKind::Bang, _) => {
929+
// Dummy span for the `def_site` means it's an external macro.
930+
expn_data.def_site.is_dummy() || sm.is_imported(expn_data.def_site)
931+
}
932+
ExpnKind::Macro { .. } => true, // definitely a plugin
933+
}
934+
}
910935
}
911936

912937
impl fmt::Debug for SyntaxContext {

compiler/rustc_span/src/lib.rs

+3-18
Original file line numberDiff line numberDiff line change
@@ -594,28 +594,13 @@ impl Span {
594594
!self.is_dummy() && sm.is_span_accessible(self)
595595
}
596596

597-
/// Returns whether `span` originates in a foreign crate's external macro.
597+
/// Returns whether this span originates in a foreign crate's external macro.
598598
///
599599
/// This is used to test whether a lint should not even begin to figure out whether it should
600600
/// be reported on the current node.
601+
#[inline]
601602
pub fn in_external_macro(self, sm: &SourceMap) -> bool {
602-
let expn_data = self.ctxt().outer_expn_data();
603-
match expn_data.kind {
604-
ExpnKind::Root
605-
| ExpnKind::Desugaring(
606-
DesugaringKind::ForLoop
607-
| DesugaringKind::WhileLoop
608-
| DesugaringKind::OpaqueTy
609-
| DesugaringKind::Async
610-
| DesugaringKind::Await,
611-
) => false,
612-
ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external"
613-
ExpnKind::Macro(MacroKind::Bang, _) => {
614-
// Dummy span for the `def_site` means it's an external macro.
615-
expn_data.def_site.is_dummy() || sm.is_imported(expn_data.def_site)
616-
}
617-
ExpnKind::Macro { .. } => true, // definitely a plugin
618-
}
603+
self.ctxt().in_external_macro(sm)
619604
}
620605

621606
/// Returns `true` if `span` originates in a derive-macro's expansion.

0 commit comments

Comments
 (0)