Robbepop / apint

Arbitrary precision integers library.
Other
27 stars 4 forks source link

Try to replace `.expect`s with safe internal APIs #31

Open AaronKutch opened 5 years ago

AaronKutch commented 5 years ago

There are several places in the library with .expects that could be eliminated. However, this is low on our list of priorities and I am just filing this issue to remind me in the future.

Robbepop commented 5 years ago

Could you please provide further information and an example within the code so that we do not forget the track of this in the future?

AaronKutch commented 5 years ago

When searching for .expect in apint, it turns up 29 results as of commit 504. e.x.

    /// Sets the sign bit of this `ApInt` to one (`1`).
    pub fn set_sign_bit(&mut self) {
        let sign_bit_pos = self.width().sign_bit_pos();
        self.set_bit_at(sign_bit_pos)
            .expect("`BitWidth::sign_bit_pos` always returns a valid `BitPos`
                     for usage in the associated `ApInt` for operating on bits.")
    }

I think that the only good use of .expect today is in more informative error messages. unreachable has a much smaller footprint in code and when it panics it returns its location in code, the only two things we really need. I believe that all of these could be removed by doing one of these on a case by case basis: