Skip to content

Commit be30c4d

Browse files
committed
chore: change str_ref_to_string to str_ref_to_owned
ToString is implemented by many different types than &str, and represents a serialization into string data. The fact that said data is returned as owned, is an implementation detail. If merely copying borrowed string data to owned string data is all that is desired, ToOwned is a much better choice, because if the user later refactors the code such that the input is no longer an `&str`, then they will get a compiler error instead of a mysterious change-in-behavior.
1 parent 8e379ce commit be30c4d

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypeMismatch) -> Option<Vec<Assi
3535
add_reference(ctx, d, &mut fixes);
3636
add_missing_ok_or_some(ctx, d, &mut fixes);
3737
remove_semicolon(ctx, d, &mut fixes);
38-
str_ref_to_string(ctx, d, &mut fixes);
38+
str_ref_to_owned(ctx, d, &mut fixes);
3939

4040
if fixes.is_empty() {
4141
None
@@ -135,7 +135,7 @@ fn remove_semicolon(
135135
Some(())
136136
}
137137

138-
fn str_ref_to_string(
138+
fn str_ref_to_owned(
139139
ctx: &DiagnosticsContext<'_>,
140140
d: &hir::TypeMismatch,
141141
acc: &mut Vec<Assist>,
@@ -151,12 +151,12 @@ fn str_ref_to_string(
151151
let expr = d.expr.value.to_node(&root);
152152
let expr_range = expr.syntax().text_range();
153153

154-
let to_string = format!(".to_string()");
154+
let to_owned = format!(".to_owned()");
155155

156-
let edit = TextEdit::insert(expr.syntax().text_range().end(), to_string);
156+
let edit = TextEdit::insert(expr.syntax().text_range().end(), to_owned);
157157
let source_change =
158158
SourceChange::from_text_edit(d.expr.file_id.original_file(ctx.sema.db), edit);
159-
acc.push(fix("str_ref_to_string", "Add .to_string() here", source_change, expr_range));
159+
acc.push(fix("str_ref_to_owned", "Add .to_owned() here", source_change, expr_range));
160160

161161
Some(())
162162
}
@@ -527,7 +527,7 @@ fn foo() -> SomeOtherEnum { 0$0 }
527527
}
528528

529529
#[test]
530-
fn str_ref_to_string() {
530+
fn str_ref_to_owned() {
531531
check_fix(
532532
r#"
533533
struct String;
@@ -540,7 +540,7 @@ fn test() -> String {
540540
struct String;
541541
542542
fn test() -> String {
543-
"a".to_string()
543+
"a".to_owned()
544544
}
545545
"#,
546546
);

0 commit comments

Comments
 (0)