fix where clause rustfix suggestion spacing

This commit is contained in:
yukang
2026-03-30 10:08:12 +08:00
parent a25435bcf7
commit 785b511360
8 changed files with 75 additions and 19 deletions
@@ -173,17 +173,19 @@ fn check_type_alias_where_clause_location(
{
let mut state = State::new();
let mut needs_comma = !ty_alias.after_where_clause.predicates.is_empty();
if !ty_alias.after_where_clause.has_where_token {
state.space();
state.word_space("where");
} else if !needs_comma {
state.space();
}
let mut first = ty_alias.after_where_clause.predicates.is_empty();
for p in &ty_alias.generics.where_clause.predicates {
if !first {
if needs_comma {
state.word_space(",");
}
first = false;
needs_comma = true;
state.print_where_predicate(p);
}
@@ -1832,7 +1834,11 @@ fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
Some((right, snippet))
}
};
let left_sp = err.span;
let left_sp = self
.sess
.source_map()
.span_extend_prev_while(err.span, char::is_whitespace)
.unwrap_or(err.span);
self.lint_buffer.dyn_buffer_lint(
DEPRECATED_WHERE_CLAUSE_LOCATION,
item.id,
@@ -1846,9 +1852,9 @@ fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
sugg,
}
}
None => {
errors::DeprecatedWhereClauseLocationSugg::RemoveWhere { span: left_sp }
}
None => errors::DeprecatedWhereClauseLocationSugg::RemoveWhere {
span: err.span,
},
};
errors::DeprecatedWhereClauseLocation { suggestion }.into_diag(dcx, level)
},
@@ -0,0 +1,17 @@
//@ check-pass
//@ run-rustfix
//@ compile-flags: --force-warn deprecated_where_clause_location
#![allow(dead_code)]
trait Trait {
type Assoc;
}
impl Trait for i32 {
type Assoc = () where i32: Copy;
//~^ WARNING where clause not allowed here
}
fn main() {}
@@ -0,0 +1,17 @@
//@ check-pass
//@ run-rustfix
//@ compile-flags: --force-warn deprecated_where_clause_location
#![allow(dead_code)]
trait Trait {
type Assoc;
}
impl Trait for i32 {
type Assoc where i32: Copy = () where;
//~^ WARNING where clause not allowed here
}
fn main() {}
@@ -0,0 +1,16 @@
warning: where clause not allowed here
--> $DIR/alias-type-where-suggest-issue-153567.rs:13:16
|
LL | type Assoc where i32: Copy = () where;
| ^^^^^^^^^^^^^^^
|
= note: see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information
= note: requested on the command line with `--force-warn deprecated-where-clause-location`
help: move it to the end of the type declaration
|
LL - type Assoc where i32: Copy = () where;
LL + type Assoc = () where i32: Copy;
|
warning: 1 warning emitted
@@ -14,12 +14,12 @@ trait Trait {
impl Trait for u32 {
// Not fine, suggests moving.
type Assoc = () where u32: Copy;
type Assoc = () where u32: Copy;
//~^ WARNING where clause not allowed here
// Not fine, suggests moving `u32: Copy`
type Assoc2 = () where i32: Copy, u32: Copy;
type Assoc2 = () where i32: Copy, u32: Copy;
//~^ WARNING where clause not allowed here
type Assoc3 = () where;
type Assoc3 = () where;
//~^ WARNING where clause not allowed here
}
@@ -27,7 +27,7 @@ impl Trait for i32 {
// Fine.
type Assoc = () where u32: Copy;
// Not fine, suggests moving both.
type Assoc2 = () where u32: Copy, i32: Copy;
type Assoc2 = () where u32: Copy, i32: Copy;
//~^ WARNING where clause not allowed here
type Assoc3 = () where;
//~^ WARNING where clause not allowed here
@@ -9,7 +9,7 @@ LL | type Assoc where u32: Copy = ();
help: move it to the end of the type declaration
|
LL - type Assoc where u32: Copy = ();
LL + type Assoc = () where u32: Copy;
LL + type Assoc = () where u32: Copy;
|
warning: where clause not allowed here
@@ -22,7 +22,7 @@ LL | type Assoc2 where u32: Copy = () where i32: Copy;
help: move it to the end of the type declaration
|
LL - type Assoc2 where u32: Copy = () where i32: Copy;
LL + type Assoc2 = () where i32: Copy, u32: Copy;
LL + type Assoc2 = () where i32: Copy, u32: Copy;
|
warning: where clause not allowed here
@@ -35,7 +35,7 @@ LL | type Assoc3 where = ();
help: move it to the end of the type declaration
|
LL - type Assoc3 where = ();
LL + type Assoc3 = () where;
LL + type Assoc3 = () where;
|
warning: where clause not allowed here
@@ -48,7 +48,7 @@ LL | type Assoc2 where u32: Copy, i32: Copy = ();
help: move it to the end of the type declaration
|
LL - type Assoc2 where u32: Copy, i32: Copy = ();
LL + type Assoc2 = () where u32: Copy, i32: Copy;
LL + type Assoc2 = () where u32: Copy, i32: Copy;
|
warning: where clause not allowed here
@@ -6,10 +6,10 @@
trait Trait {
// Not fine, suggests moving.
type Assoc = () where u32: Copy;
type Assoc = () where u32: Copy;
//~^ WARNING where clause not allowed here
// Not fine, suggests moving `u32: Copy`
type Assoc2 = () where i32: Copy, u32: Copy;
type Assoc2 = () where i32: Copy, u32: Copy;
//~^ WARNING where clause not allowed here
}
@@ -9,7 +9,7 @@ LL | type Assoc where u32: Copy = ();
help: move it to the end of the type declaration
|
LL - type Assoc where u32: Copy = ();
LL + type Assoc = () where u32: Copy;
LL + type Assoc = () where u32: Copy;
|
warning: where clause not allowed here
@@ -22,7 +22,7 @@ LL | type Assoc2 where u32: Copy = () where i32: Copy;
help: move it to the end of the type declaration
|
LL - type Assoc2 where u32: Copy = () where i32: Copy;
LL + type Assoc2 = () where i32: Copy, u32: Copy;
LL + type Assoc2 = () where i32: Copy, u32: Copy;
|
warning: 2 warnings emitted