From 1b342f70dfb999e19026979f28a76952dcc216f4 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 19 Feb 2020 12:59:35 +0100 Subject: [PATCH] Clean up E0317 explanation --- src/librustc_error_codes/error_codes/E0317.md | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0317.md b/src/librustc_error_codes/error_codes/E0317.md index e31a2b56be3..230911c2086 100644 --- a/src/librustc_error_codes/error_codes/E0317.md +++ b/src/librustc_error_codes/error_codes/E0317.md @@ -1,14 +1,30 @@ -This error occurs when an `if` expression without an `else` block is used in a -context where a type other than `()` is expected, for example a `let` -expression: +An `if` expression is missing an `else` block. + +Erroneous code example: ```compile_fail,E0317 -fn main() { - let x = 5; - let a = if x == 5 { 1 }; -} +let x = 5; +let a = if x == 5 { + 1 +}; ``` +This error occurs when an `if` expression without an `else` block is used in a +context where a type other than `()` is expected. In the previous code example, +the `let` expression was expecting a value but since there was no `else`, no +value was returned. + An `if` expression without an `else` block has the type `()`, so this is a type error. To resolve it, add an `else` block having the same type as the `if` block. + +So to fix the previous code example: + +``` +let x = 5; +let a = if x == 5 { + 1 +} else { + 2 +}; +```