Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

must_use on throws fn #45

Open
ijackson opened this issue Apr 24, 2020 · 3 comments
Open

must_use on throws fn #45

ijackson opened this issue Apr 24, 2020 · 3 comments

Comments

@ijackson
Copy link

Hi. Thanks for this nice crate. I have only been using it a few hours and it is already improving my life. I have found a situation where I can write something with an obvious meaning but which doesn't do what I would have hoped:

This function

#[must_use] #[throws(X)] fn x() ->  usize { 42 }

can be called, and the value 42 discarded, without any warning. This is probably not what the programmer intended when they wrote #[must_use].

I suspect that fixing this is not going to be simple. When considering Rust without Fehler, there does not appear to be a way to write a function that returns a Result<T,E> where the caller is required to both handle E and do something with T. (Other than making the whole type T must_use, which may not be desirable or possible.)

My full test program:

use fehler::throws;
use thiserror::Error;

#[derive(Error,Debug)]
#[error("X")]
struct X ();

#[must_use] #[throws(X)] fn x() ->        usize    {    42  }
#[must_use]              fn y() -> Result<usize,X> { Ok(43) }
#[must_use]              fn z() ->        usize    {    44  }

#[throws(X)]
fn main() {
                               x()?;
                               y()?;
  { #![allow(unused_must_use)] z(); }
}
@withoutboats
Copy link
Owner

Unfortunately there's no way to fix this in fehler, because its implemented as a procedural macro.

@ijackson
Copy link
Author

ijackson commented May 6, 2020

Would it be easy to spot this case and at least warn about it? [throws] implies that the Result must be used. So adding [must_use] is an attempt to do this sadly-impossible thing and it would be nice to let the programmer know that this constraint wasn't going to be enforced.

@withoutboats
Copy link
Owner

The compiler could have a warning that a must_use on a function that returns a must_use type is redundant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants