rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
10.89k stars 1.46k forks source link

Let `qualify_min_const_fn` deal with drop terminators #12681

Open y21 opened 4 weeks ago

y21 commented 4 weeks ago

Fixes #12677

The method_accepts_droppable check that was there seemed overly conservative.

Returns true if any of the method parameters is a type that implements Drop. The method can't be made const then, because drop can't be const-evaluated.

Accepting parameters that implement Drop should still be fine as long as the parameter isn't actually dropped, as is the case in the linked issue where the droppable is moved into the return place. This more accurate analysis ("is there a drop terminator") is already done by qualify_min_const_fn here, so I don't think this additional check is really necessary?

Fixing the other, second case in the linked issue was only slightly more involved, since Vec::new() is a function call that has the ability to panic, so there must be a drop() terminator for cleanup, however we should be able to freely ignore that. Const checking ignores cleanup blocks, so we should, too?

r? @Jarcho


changelog: [missing_const_for_fn]: continue linting on fns with parameters implementing Drop if they're not actually dropped

y21 commented 4 weeks ago

I do wonder what https://github.com/rust-lang/rust-clippy/blob/f5e250180c342bb52808c9a934c962a8fe40afc7/clippy_lints/src/missing_const_for_fn.rs#L154-L158 is for... That seems like dead code essentially. We are early-returning if already_const before this, so I don't see how is_const_fn_raw() could be true.