Skip to content

Commit

Permalink
Rollup merge of #117695 - 3tilley:prioritise-unwrap-expect-over-last-…
Browse files Browse the repository at this point in the history
…method-call, r=compiler-errors

Reorder checks to make sure potential missing expect on Option/Result…

… runs before removing last method call

Fixes #117669
  • Loading branch information
matthiaskrgr committed Nov 13, 2023
2 parents 03d6e7a + 1854776 commit 918c17a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {

// Use `||` to give these suggestions a precedence
let suggested = self.suggest_missing_parentheses(err, expr)
|| self.suggest_missing_unwrap_expect(err, expr, expected, expr_ty)
|| self.suggest_remove_last_method_call(err, expr, expected)
|| self.suggest_associated_const(err, expr, expected)
|| self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr)
Expand All @@ -49,8 +50,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|| self.suggest_into(err, expr, expr_ty, expected)
|| self.suggest_floating_point_literal(err, expr, expected)
|| self.suggest_null_ptr_for_literal_zero_given_to_ptr_arg(err, expr, expected)
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty)
|| self.suggest_missing_unwrap_expect(err, expr, expected, expr_ty);
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty);

if !suggested {
self.note_source_of_type_mismatch_constraint(
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/suggestions/issue-117669.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() {
let abs: i32 = 3i32.checked_abs();
//~^ ERROR mismatched types
}
18 changes: 18 additions & 0 deletions tests/ui/suggestions/issue-117669.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error[E0308]: mismatched types
--> $DIR/issue-117669.rs:2:20
|
LL | let abs: i32 = 3i32.checked_abs();
| --- ^^^^^^^^^^^^^^^^^^ expected `i32`, found `Option<i32>`
| |
| expected due to this
|
= note: expected type `i32`
found enum `Option<i32>`
help: consider using `Option::expect` to unwrap the `Option<i32>` value, panicking if the value is an `Option::None`
|
LL | let abs: i32 = 3i32.checked_abs().expect("REASON");
| +++++++++++++++++

error: aborting due to previous error

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

0 comments on commit 918c17a

Please sign in to comment.