Handle exclusive refs in suggestion to copy/clone

This commit is contained in:
clubby789 2023-07-27 23:10:53 +00:00
parent 683b2656bf
commit fafc3d2d0e
4 changed files with 61 additions and 3 deletions

View File

@ -1110,7 +1110,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
{
let expr_inner_ty = args.type_at(0);
let expected_inner_ty = expected_args.type_at(0);
if let &ty::Ref(_, ty, hir::Mutability::Not) = expr_inner_ty.kind()
if let &ty::Ref(_, ty, mutability) = expr_inner_ty.kind()
&& self.can_eq(self.param_env, ty, expected_inner_ty)
{
let def_path = self.tcx.def_path_str(adt_def.did());
@ -1119,7 +1119,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
errors::OptionResultRefMismatch::Copied {
span, def_path
}
} else if let Some(expected_ty_expr) = expected_ty_expr {
} else if let Some(expected_ty_expr) = expected_ty_expr
// FIXME: suggest changes to both expressions to convert both to
// Option/Result<&T>
&& mutability.is_not()
{
errors::OptionResultRefMismatch::AsRef {
span: expected_ty_expr.span.shrink_to_hi(), expected_ty, expr_ty, def_path
}

View File

@ -28,4 +28,18 @@ fn main() {
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
let mut s = ();
let x = Some(s);
let y = Some(&mut s);
println!("{}", x == y.copied());
//~^ ERROR mismatched types
//~| HELP use `Option::copied` to copy the value inside the `Option`
let mut s = String::new();
let x = Some(s.clone());
let y = Some(&mut s);
println!("{}", x == y.cloned());
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`
}

View File

@ -28,4 +28,18 @@ fn main() {
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
let mut s = ();
let x = Some(s);
let y = Some(&mut s);
println!("{}", x == y);
//~^ ERROR mismatched types
//~| HELP use `Option::copied` to copy the value inside the `Option`
let mut s = String::new();
let x = Some(s.clone());
let y = Some(&mut s);
println!("{}", x == y);
//~^ ERROR mismatched types
//~| HELP use `Option::cloned` to clone the value inside the `Option`
}

View File

@ -91,6 +91,32 @@ help: use `Option::as_ref` to convert `Option<String>` to `Option<&String>`
LL | println!("{}", x.as_ref() == y);
| +++++++++
error: aborting due to 5 previous errors
error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:35:25
|
LL | println!("{}", x == y);
| ^ expected `Option<()>`, found `Option<&mut ()>`
|
= note: expected enum `Option<()>`
found enum `Option<&mut ()>`
help: use `Option::copied` to copy the value inside the `Option`
|
LL | println!("{}", x == y.copied());
| +++++++++
error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:42:25
|
LL | println!("{}", x == y);
| ^ expected `Option<String>`, found `Option<&mut String>`
|
= note: expected enum `Option<String>`
found enum `Option<&mut String>`
help: use `Option::cloned` to clone the value inside the `Option`
|
LL | println!("{}", x == y.cloned());
| +++++++++
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0308`.