Skip to content

Commit ed3a427

Browse files
committed
Reorder type mismatch suggestions
Suggestions are reordered to to make sure potential missing expect on Option/Result runs before the suggestion to remove the last method call
1 parent d1611e3 commit ed3a427

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4343

4444
// Use `||` to give these suggestions a precedence
4545
let suggested = self.suggest_missing_parentheses(err, expr)
46+
|| self.suggest_missing_unwrap_expect(err, expr, expected, expr_ty)
4647
|| self.suggest_remove_last_method_call(err, expr, expected)
4748
|| self.suggest_associated_const(err, expr, expected)
4849
|| self.suggest_deref_ref_or_into(err, expr, expected, expr_ty, expected_ty_expr)
@@ -58,8 +59,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5859
|| self.suggest_into(err, expr, expr_ty, expected)
5960
|| self.suggest_floating_point_literal(err, expr, expected)
6061
|| self.suggest_null_ptr_for_literal_zero_given_to_ptr_arg(err, expr, expected)
61-
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty)
62-
|| self.suggest_missing_unwrap_expect(err, expr, expected, expr_ty);
62+
|| self.suggest_coercing_result_via_try_operator(err, expr, expected, expr_ty);
6363

6464
if !suggested {
6565
self.note_source_of_type_mismatch_constraint(

tests/ui/suggestions/issue-117669.rs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fn main() {
2+
let abs: i32 = 3i32.checked_abs();
3+
//~^ ERROR mismatched types
4+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-177699.rs:2:20
3+
|
4+
2 | let abs: i32 = 3i32.checked_abs();
5+
| --- ^^^^^^^^^^^^^^^^^^ expected `i32`, found `Option<i32>`
6+
| |
7+
| expected due to this
8+
|
9+
= note: expected type `i32`
10+
found enum `Option<i32>`
11+
help: consider using `Option::expect` to unwrap the `Option<i32>` value, panicking if the value is an `Option::None`
12+
|
13+
2 | let abs: i32 = 3i32.checked_abs().expect("REASON");
14+
| +++++++++++++++++
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)