Refactor how to get the span of a function header
This commit is contained in:
@@ -2284,6 +2284,33 @@ pub struct FnSig {
|
|||||||
pub span: Span,
|
pub span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl FnSig {
|
||||||
|
/// Return a span encompassing the header, or where to insert it if empty.
|
||||||
|
pub fn header_span(&self) -> Span {
|
||||||
|
match self.header.ext {
|
||||||
|
Extern::Implicit(span) | Extern::Explicit(_, span) => {
|
||||||
|
return self.span.with_hi(span.hi());
|
||||||
|
}
|
||||||
|
Extern::None => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
match self.header.safety {
|
||||||
|
Safety::Unsafe(span) | Safety::Safe(span) => return self.span.with_hi(span.hi()),
|
||||||
|
Safety::Default => {}
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(coroutine_kind) = self.header.coroutine_kind {
|
||||||
|
return self.span.with_hi(coroutine_kind.span().hi());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Const::Yes(span) = self.header.constness {
|
||||||
|
return self.span.with_hi(span.hi());
|
||||||
|
}
|
||||||
|
|
||||||
|
self.span.shrink_to_lo()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// A constraint on an associated item.
|
/// A constraint on an associated item.
|
||||||
///
|
///
|
||||||
/// ### Examples
|
/// ### Examples
|
||||||
@@ -3534,12 +3561,12 @@ impl Extern {
|
|||||||
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
|
/// included in this struct (e.g., `async unsafe fn` or `const extern "C" fn`).
|
||||||
#[derive(Clone, Copy, Encodable, Decodable, Debug, Walkable)]
|
#[derive(Clone, Copy, Encodable, Decodable, Debug, Walkable)]
|
||||||
pub struct FnHeader {
|
pub struct FnHeader {
|
||||||
/// Whether this is `unsafe`, or has a default safety.
|
|
||||||
pub safety: Safety,
|
|
||||||
/// Whether this is `async`, `gen`, or nothing.
|
|
||||||
pub coroutine_kind: Option<CoroutineKind>,
|
|
||||||
/// The `const` keyword, if any
|
/// The `const` keyword, if any
|
||||||
pub constness: Const,
|
pub constness: Const,
|
||||||
|
/// Whether this is `async`, `gen`, or nothing.
|
||||||
|
pub coroutine_kind: Option<CoroutineKind>,
|
||||||
|
/// Whether this is `unsafe`, or has a default safety.
|
||||||
|
pub safety: Safety,
|
||||||
/// The `extern` keyword and corresponding ABI string, if any.
|
/// The `extern` keyword and corresponding ABI string, if any.
|
||||||
pub ext: Extern,
|
pub ext: Extern,
|
||||||
}
|
}
|
||||||
@@ -3553,38 +3580,6 @@ impl FnHeader {
|
|||||||
|| matches!(constness, Const::Yes(_))
|
|| matches!(constness, Const::Yes(_))
|
||||||
|| !matches!(ext, Extern::None)
|
|| !matches!(ext, Extern::None)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a span encompassing the header, or none if all options are default.
|
|
||||||
pub fn span(&self) -> Option<Span> {
|
|
||||||
fn append(a: &mut Option<Span>, b: Span) {
|
|
||||||
*a = match a {
|
|
||||||
None => Some(b),
|
|
||||||
Some(x) => Some(x.to(b)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut full_span = None;
|
|
||||||
|
|
||||||
match self.safety {
|
|
||||||
Safety::Unsafe(span) | Safety::Safe(span) => append(&mut full_span, span),
|
|
||||||
Safety::Default => {}
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(coroutine_kind) = self.coroutine_kind {
|
|
||||||
append(&mut full_span, coroutine_kind.span());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Const::Yes(span) = self.constness {
|
|
||||||
append(&mut full_span, span);
|
|
||||||
}
|
|
||||||
|
|
||||||
match self.ext {
|
|
||||||
Extern::Implicit(span) | Extern::Explicit(_, span) => append(&mut full_span, span),
|
|
||||||
Extern::None => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
full_span
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for FnHeader {
|
impl Default for FnHeader {
|
||||||
|
|||||||
@@ -492,7 +492,7 @@ impl<'a> AstValidator<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !spans.is_empty() {
|
if !spans.is_empty() {
|
||||||
let header_span = sig.header.span().unwrap_or(sig.span.shrink_to_lo());
|
let header_span = sig.header_span();
|
||||||
let suggestion_span = header_span.shrink_to_hi().to(sig.decl.output.span());
|
let suggestion_span = header_span.shrink_to_hi().to(sig.decl.output.span());
|
||||||
let padding = if header_span.is_empty() { "" } else { " " };
|
let padding = if header_span.is_empty() { "" } else { " " };
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user