mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 08:44:35 +00:00
Move common code to WhereClause
This commit is contained in:
parent
04243710a0
commit
0d18136b77
@ -525,6 +525,13 @@ impl WhereClause<'_> {
|
||||
pub fn span_for_predicates_or_empty_place(&self) -> Span {
|
||||
self.span
|
||||
}
|
||||
|
||||
/// `Span` where further predicates would be suggested, accounting for trailing commas, like
|
||||
/// in `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
|
||||
pub fn tail_span_for_suggestion(&self) -> Span {
|
||||
let end = self.span_for_predicates_or_empty_place().shrink_to_hi();
|
||||
self.predicates.last().map(|p| p.span()).unwrap_or(end).shrink_to_hi().to(end)
|
||||
}
|
||||
}
|
||||
|
||||
/// A single predicate in a where-clause.
|
||||
|
@ -220,22 +220,11 @@ pub fn suggest_constraining_type_param(
|
||||
}
|
||||
}
|
||||
|
||||
// Account for `fn foo<T>(t: T) where T: Foo,` so we don't suggest two trailing commas.
|
||||
let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
|
||||
let where_clause_span = generics
|
||||
.where_clause
|
||||
.predicates
|
||||
.last()
|
||||
.map(|p| p.span())
|
||||
.unwrap_or(end)
|
||||
.shrink_to_hi()
|
||||
.to(end);
|
||||
|
||||
match ¶m_spans[..] {
|
||||
&[¶m_span] => suggest_restrict(param_span.shrink_to_hi()),
|
||||
_ => {
|
||||
err.span_suggestion_verbose(
|
||||
where_clause_span,
|
||||
generics.where_clause.tail_span_for_suggestion(),
|
||||
&msg_restrict_type_further,
|
||||
format!(", {}: {}", param_name, constraint),
|
||||
Applicability::MachineApplicable,
|
||||
|
@ -169,17 +169,8 @@ pub trait InferCtxtExt<'tcx> {
|
||||
}
|
||||
|
||||
fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
|
||||
let end = generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi();
|
||||
(
|
||||
// Account for `where T: Foo,` so we don't suggest two trailing commas.
|
||||
generics
|
||||
.where_clause
|
||||
.predicates
|
||||
.last()
|
||||
.map(|p| p.span())
|
||||
.unwrap_or(end)
|
||||
.shrink_to_hi()
|
||||
.to(end),
|
||||
generics.where_clause.tail_span_for_suggestion(),
|
||||
format!(
|
||||
"{} {}",
|
||||
if !generics.where_clause.predicates.is_empty() { "," } else { " where" },
|
||||
|
Loading…
Reference in New Issue
Block a user