TartanLlama / expected

C++11/14/17 std::expected with functional-style extensions
https://tl.tartanllama.xyz
Creative Commons Zero v1.0 Universal
1.55k stars 135 forks source link

`tl::make_unexpected()` should be `[[nodiscard]]` #124

Open azais-corentin opened 1 year ago

azais-corentin commented 1 year ago

As the titles says, the function tl::make_unexpected() should be marked [[nodiscard]].

It would prevent misuse when forgetting to return the unexpected value:

enum class ErrorType{
    kNullParameter
};
struct ResultType{};

tl::expected<ResultType, ErrorType> myFunction(int nonNullParameter)
{
    if(nonNullParameter== 0) {
        tl::make_unexpected(ErrorType::kNullParameter);
    }

    return ResultType{};
}

Here I forgot to return the unexpected value and there's no warning whatsoever.