Skip to content

Commit 81561ff

Browse files
committed
fix: Trailing excess comma in "Convert to named struct" assist
1 parent e66f3db commit 81561ff

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_struct_to_named_struct.rs

+27-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use either::Either;
22
use ide_db::defs::{Definition, NameRefClass};
33
use syntax::{
44
ast::{self, AstNode, HasGenericParams, HasVisibility},
5-
match_ast, SyntaxNode,
5+
match_ast, SyntaxKind, SyntaxNode,
66
};
77

88
use crate::{assist_context::SourceChangeBuilder, AssistContext, AssistId, AssistKind, Assists};
@@ -100,7 +100,9 @@ fn edit_struct_def(
100100
ast::make::tokens::single_newline().text(),
101101
);
102102
edit.insert(tuple_fields_text_range.start(), w.syntax().text());
103-
edit.insert(tuple_fields_text_range.start(), ",");
103+
if !w.syntax().last_token().is_some_and(|t| t.kind() == SyntaxKind::COMMA) {
104+
edit.insert(tuple_fields_text_range.start(), ",");
105+
}
104106
edit.insert(
105107
tuple_fields_text_range.start(),
106108
ast::make::tokens::single_newline().text(),
@@ -879,6 +881,29 @@ use crate::{A::Variant, Inner};
879881
fn f() {
880882
let a = Variant { field1: Inner };
881883
}
884+
"#,
885+
);
886+
}
887+
888+
#[test]
889+
fn where_clause_with_trailing_comma() {
890+
check_assist(
891+
convert_tuple_struct_to_named_struct,
892+
r#"
893+
trait Foo {}
894+
895+
struct Bar$0<T>(pub T)
896+
where
897+
T: Foo,;
898+
"#,
899+
r#"
900+
trait Foo {}
901+
902+
struct Bar<T>
903+
where
904+
T: Foo,
905+
{ pub field1: T }
906+
882907
"#,
883908
);
884909
}

0 commit comments

Comments
 (0)