Skip to content

Commit

Permalink
Add a helper for extending a span to include any trailing whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 7, 2024
1 parent fc06582 commit 8c89fa8
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 27 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'a> AstValidator<'a> {
in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
const_context_label: parent_constness,
remove_const_sugg: (
self.session.source_map().span_extend_while(span, |c| c == ' ').unwrap_or(span),
self.session.source_map().span_extend_while_whitespace(span),
match parent_constness {
Some(_) => rustc_errors::Applicability::MachineApplicable,
None => rustc_errors::Applicability::MaybeIncorrect,
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2031,8 +2031,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.tcx
.sess
.source_map()
.span_extend_while(range_start.span, |c| c.is_whitespace())
.unwrap_or(range_start.span)
.span_extend_while_whitespace(range_start.span)
.shrink_to_hi()
.to(range_end.span);

Expand Down
8 changes: 2 additions & 6 deletions compiler/rustc_infer/src/infer/error_reporting/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
self.tcx
.sess
.source_map()
.span_extend_while(last_expr.span, |c| c.is_whitespace())
.ok()?
.span_extend_while_whitespace(last_expr.span)
.shrink_to_hi()
.with_hi(last_stmt.span.hi())
};
Expand Down Expand Up @@ -874,10 +873,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
format!(" {ident} ")
};
let left_span = sm.span_through_char(blk.span, '{').shrink_to_hi();
(
sm.span_extend_while(left_span, |c| c.is_whitespace()).unwrap_or(left_span),
sugg,
)
(sm.span_extend_while_whitespace(left_span), sugg)
};
Some(SuggestRemoveSemiOrReturnBinding::Add { sp: span, code: sugg, ident: *ident })
}
Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_lint/src/context/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,7 @@ pub(super) fn builtin(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Di
if let Some(deletion_span) = deletion_span {
let msg = "elide the single-use lifetime";
let (use_span, replace_lt) = if elide {
let use_span = sess
.source_map()
.span_extend_while(use_span, char::is_whitespace)
.unwrap_or(use_span);
let use_span = sess.source_map().span_extend_while_whitespace(use_span);
(use_span, String::new())
} else {
(use_span, "'_".to_owned())
Expand Down
15 changes: 9 additions & 6 deletions compiler/rustc_span/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ impl SourceMap {
})
}

/// Extends the span to include any trailing whitespace, or returns the original
/// span if a `SpanSnippetError` was encountered.
pub fn span_extend_while_whitespace(&self, span: Span) -> Span {
self.span_extend_while(span, char::is_whitespace).unwrap_or(span)
}

/// Extends the given `Span` to previous character while the previous character matches the predicate
pub fn span_extend_prev_while(
&self,
Expand Down Expand Up @@ -1034,12 +1040,9 @@ impl SourceMap {
/// // ^^^^^^ input
/// ```
pub fn mac_call_stmt_semi_span(&self, mac_call: Span) -> Option<Span> {
let span = self.span_extend_while(mac_call, char::is_whitespace).ok()?;
let span = span.shrink_to_hi().with_hi(BytePos(span.hi().0.checked_add(1)?));
if self.span_to_snippet(span).as_deref() != Ok(";") {
return None;
}
Some(span)
let span = self.span_extend_while_whitespace(mac_call);
let span = self.next_point(span);
if self.span_to_snippet(span).as_deref() == Ok(";") { Some(span) } else { None }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1592,8 +1592,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
.tcx
.sess
.source_map()
.span_extend_while(expr_span, char::is_whitespace)
.unwrap_or(expr_span)
.span_extend_while_whitespace(expr_span)
.shrink_to_hi()
.to(await_expr.span.shrink_to_hi());
err.span_suggestion(
Expand Down Expand Up @@ -4851,10 +4850,7 @@ pub fn suggest_desugaring_async_fn_to_impl_future_in_trait<'tcx>(
let hir::IsAsync::Async(async_span) = sig.header.asyncness else {
return None;
};
let Ok(async_span) = tcx.sess.source_map().span_extend_while(async_span, |c| c.is_whitespace())
else {
return None;
};
let async_span = tcx.sess.source_map().span_extend_while_whitespace(async_span);

let future = tcx.hir_node_by_def_id(opaque_def_id).expect_item().expect_opaque_ty();
let [hir::GenericBound::Trait(trait_ref, _)] = future.bounds else {
Expand Down
3 changes: 1 addition & 2 deletions src/tools/clippy/clippy_lints/src/lifetimes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ fn elision_suggestions(
let span = cx
.sess()
.source_map()
.span_extend_while(usage.ident.span, |ch| ch.is_ascii_whitespace())
.unwrap_or(usage.ident.span);
.span_extend_while_whitespace(usage.ident.span);

(span, String::new())
},
Expand Down

0 comments on commit 8c89fa8

Please sign in to comment.