Skip to content

Commit

Permalink
Rollup merge of #114052 - clubby789:suggest-option-asref, r=WaffleLapkin
Browse files Browse the repository at this point in the history
Suggest `{Option,Result}::as_ref()` instead of `cloned()` in some cases

Fixes #114050

When we have an expr available that produces the type expectation, we can suggest appending `.as_ref()` to the span, instead of cloning the expr producing the mismatch
  • Loading branch information
matthiaskrgr authored Jul 25, 2023
2 parents 99f404a + c83dfe9 commit b7a1ff2
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_no_capture_closure(err, expected, expr_ty)
|| self.suggest_boxing_when_appropriate(err, expr.span, expr.hir_id, expected, expr_ty)
|| self.suggest_block_to_brackets_peeling_refs(err, expr, expr_ty, expected)
|| self.suggest_copied_or_cloned(err, expr, expr_ty, expected)
|| self.suggest_copied_cloned_or_as_ref(err, expr, expr_ty, expected, expected_ty_expr)
|| self.suggest_clone_for_ref(err, expr, expr_ty, expected)
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected)
Expand Down
19 changes: 15 additions & 4 deletions compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,12 +1085,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
false
}

pub(crate) fn suggest_copied_or_cloned(
pub(crate) fn suggest_copied_cloned_or_as_ref(
&self,
diag: &mut Diagnostic,
expr: &hir::Expr<'_>,
expr_ty: Ty<'tcx>,
expected_ty: Ty<'tcx>,
expected_ty_expr: Option<&'tcx hir::Expr<'tcx>>,
) -> bool {
let ty::Adt(adt_def, args) = expr_ty.kind() else {
return false;
Expand All @@ -1102,7 +1103,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
return false;
}

let mut suggest_copied_or_cloned = || {
let mut suggest_copied_cloned_or_as_ref = || {
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()
Expand All @@ -1119,6 +1120,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Applicability::MachineApplicable,
);
return true;
} else if let Some(expected_ty_expr) = expected_ty_expr {
diag.span_suggestion_verbose(
expected_ty_expr.span.shrink_to_hi(),
format!(
"use `{def_path}::as_ref()` to convert `{expected_ty}` to `{expr_ty}`"
),
".as_ref()",
Applicability::MachineApplicable,
);
return true;
} else if let Some(clone_did) = self.tcx.lang_items().clone_trait()
&& rustc_trait_selection::traits::type_known_to_meet_bound_modulo_regions(
self,
Expand Down Expand Up @@ -1146,11 +1157,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Check that the error types are equal
&& self.can_eq(self.param_env, args.type_at(1), expected_args.type_at(1))
{
return suggest_copied_or_cloned();
return suggest_copied_cloned_or_as_ref();
} else if let Some(option_did) = self.tcx.get_diagnostic_item(sym::Option)
&& adt_def.did() == option_did
{
return suggest_copied_or_cloned();
return suggest_copied_cloned_or_as_ref();
}

false
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/associated-types/dont-suggest-cyclic-constraint.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// run-rustfix

use std::fmt::Debug;

pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
where
I::Item: Eq + Debug,
{
debug_assert_eq!(iter.next().as_ref(), Some(value));
//~^ ERROR mismatched types
}

fn main() {}
4 changes: 3 additions & 1 deletion tests/ui/associated-types/dont-suggest-cyclic-constraint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// run-rustfix

use std::fmt::Debug;

fn foo<I: Iterator>(mut iter: I, value: &I::Item)
pub fn foo<I: Iterator>(mut iter: I, value: &I::Item)
where
I::Item: Eq + Debug,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
error[E0308]: mismatched types
--> $DIR/dont-suggest-cyclic-constraint.rs:7:35
--> $DIR/dont-suggest-cyclic-constraint.rs:9:35
|
LL | debug_assert_eq!(iter.next(), Some(value));
| ^^^^^^^^^^^ expected `Option<<I as Iterator>::Item>`, found `Option<&<I as Iterator>::Item>`
|
= note: expected enum `Option<<I as Iterator>::Item>`
found enum `Option<&<I as Iterator>::Item>`
help: use `Option::as_ref()` to convert `Option<<I as Iterator>::Item>` to `Option<&<I as Iterator>::Item>`
|
LL | debug_assert_eq!(iter.next().as_ref(), Some(value));
| +++++++++

error: aborting due to previous error

Expand Down
8 changes: 8 additions & 0 deletions tests/ui/suggestions/copied-and-cloned.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ fn main() {
expect::<Result<String, ()>>(x.cloned());
//~^ ERROR mismatched types
//~| HELP use `Result::cloned` to clone the value inside the `Result`

let s = String::new();
let x = Some(s.clone());
let y = Some(&s);
println!("{}", x.as_ref() == y);
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref()` to convert `Option<String>` to `Option<&String>`

}
8 changes: 8 additions & 0 deletions tests/ui/suggestions/copied-and-cloned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,12 @@ fn main() {
expect::<Result<String, ()>>(x);
//~^ ERROR mismatched types
//~| HELP use `Result::cloned` to clone the value inside the `Result`

let s = String::new();
let x = Some(s.clone());
let y = Some(&s);
println!("{}", x == y);
//~^ ERROR mismatched types
//~| HELP use `Option::as_ref()` to convert `Option<String>` to `Option<&String>`

}
15 changes: 14 additions & 1 deletion tests/ui/suggestions/copied-and-cloned.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ help: use `Result::cloned` to clone the value inside the `Result`
LL | expect::<Result<String, ()>>(x.cloned());
| +++++++++

error: aborting due to 4 previous errors
error[E0308]: mismatched types
--> $DIR/copied-and-cloned.rs:27:25
|
LL | println!("{}", x == y);
| ^ expected `Option<String>`, found `Option<&String>`
|
= note: expected enum `Option<String>`
found enum `Option<&String>`
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

For more information about this error, try `rustc --explain E0308`.

0 comments on commit b7a1ff2

Please sign in to comment.