about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2023-11-13 21:28:55 +0100
committerGitHub <noreply@github.com>2023-11-13 21:28:55 +0100
commit918c17acc3db07b9d7a78d8d02b5b29e0e939b53 (patch)
tree7ee4adc43d5c07e1734fbc64fc6d2ba34eeac79b
parent03d6e7ade0352b35a3cd586b3384ed2960ebd030 (diff)
parent1854776fa947e6f12dda66ec7858d9c00a11ac95 (diff)
downloadrust-918c17acc3db07b9d7a78d8d02b5b29e0e939b53.tar.gz
rust-918c17acc3db07b9d7a78d8d02b5b29e0e939b53.zip
Rollup merge of #117695 - 3tilley:prioritise-unwrap-expect-over-last-method-call, r=compiler-errors
Reorder checks to make sure potential missing expect on Option/Result…

… runs before removing last method call

Fixes #117669
-rw-r--r--compiler/rustc_hir_typeck/src/demand.rs4
-rw-r--r--tests/ui/suggestions/issue-117669.rs4
-rw-r--r--tests/ui/suggestions/issue-117669.stderr18
3 files changed, 24 insertions, 2 deletions
diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs
index acaa3e02f09..77d6183f862 100644
--- a/compiler/rustc_hir_typeck/src/demand.rs
+++ b/compiler/rustc_hir_typeck/src/demand.rs
@@ -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)
@@ -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(
diff --git a/tests/ui/suggestions/issue-117669.rs b/tests/ui/suggestions/issue-117669.rs
new file mode 100644
index 00000000000..09b4f2bd47c
--- /dev/null
+++ b/tests/ui/suggestions/issue-117669.rs
@@ -0,0 +1,4 @@
+fn main() {
+    let abs: i32 = 3i32.checked_abs();
+    //~^ ERROR mismatched types
+}
diff --git a/tests/ui/suggestions/issue-117669.stderr b/tests/ui/suggestions/issue-117669.stderr
new file mode 100644
index 00000000000..f09675fcc5c
--- /dev/null
+++ b/tests/ui/suggestions/issue-117669.stderr
@@ -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`.