Skip to content

Commit 08b5b88

Browse files
authored
Merge pull request rust-lang#22457 from A4-Tacks/add-deref-assign
fix: use add deref in assign instead add `&mut` for value
2 parents 0c8788b + 5ffa3d4 commit 08b5b88

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/type_mismatch.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ fn add_or_fix_reference(
120120
return None;
121121
}
122122

123+
let expr = expr_ptr.to_node(ctx.db());
124+
let assign = expr
125+
.syntax()
126+
.parent()
127+
.and_then(ast::BinExpr::cast)
128+
.filter(|it| it.op_kind() == Some(ast::BinaryOp::Assignment { op: None }));
129+
if let Some(assign) = assign
130+
&& expected_mutability.is_mut()
131+
&& let Some(range) = ctx.sema.original_range_opt(assign.syntax())
132+
{
133+
let edit = TextEdit::insert(range.range.start(), "*".to_owned());
134+
let source_change = SourceChange::from_text_edit(range.file_id.file_id(ctx.db()), edit);
135+
acc.push(fix("add_deref_here", "Add deref here", source_change, range.range));
136+
return Some(());
137+
}
138+
123139
let ampersands = format!("&{}", expected_mutability.as_keyword_for_ref());
124140

125141
let edit = TextEdit::insert(range.range.start(), ampersands);
@@ -507,6 +523,22 @@ fn test(_arg: &mut i32) {}
507523
);
508524
}
509525

526+
#[test]
527+
fn add_deref_in_assign() {
528+
check_fix(
529+
r#"
530+
fn test(arg: &mut i32) {
531+
arg = $02;
532+
}
533+
"#,
534+
r#"
535+
fn test(arg: &mut i32) {
536+
*arg = 2;
537+
}
538+
"#,
539+
);
540+
}
541+
510542
#[test]
511543
fn add_reference_to_array() {
512544
check_fix(

0 commit comments

Comments
 (0)