Open aatifsyed opened 1 year ago
library authors create
const fn
s that can't usefully be used inconst
contexts
Do you remember any specific examples? Would be good to see what we could reasonably suggest to do instead
Here's a couple :)
https://github.com/multiformats/rust-multihash/issues/330 (addressing in https://github.com/multiformats/rust-multihash/pull/331) https://github.com/multiformats/rust-cid/issues/138
In the first case, the solution is creating a new Error
type.
A candidate is "is the return type const-droppable", but that would flag
Vec::new
. Maybe that's okay?
I'd say it's fine if it lints this as well, Vec::new
is essentially useless in constants as it of course doesn't allocate anything; You can't do anything with it. Of course, unless it's wrapped in a Mutex
+ Lazy
as it can then be changed at runtime (or is static mut
). There's probably other cases where that doesn't hold
But this does contradict missing_const_for_fn
. Having these be const
is fine imo so I think this lint should be a subtle hint to modify the API to make it useful (rather than remove the const
)
Vec::new is essentially useless in constants as it of course doesn't allocate anything;
A const Vec::new()
used to be the only way to initialize an array of vecs using [val; n]
syntax.
That is, [Vec::<u8>::new(); 10]
doesn't compile because Vec<u8>
isnt Copy
, but this does compile because paths to constants are allowed:
const EMPTY: Vec<u8> = Vec::new();
let arr = [EMPTY; 10];
So if for some reason you can't use array::from_fn
or array::map
(msrv reasons?), I'd say it's quite useful for this alone.
Though maybe it's rare enough that linting is fine? Not sure
Related issue for the aforementioned lint:
(Arg) | -> Ret | Evaluation | Clippy |
---|---|---|---|
impl Copy | impl Drop | const fn | impractical_const |
impl Copy | impl Drop | fn | |
impl Copy | impl Copy | const fn | |
impl Copy | impl Copy | fn | missing_const_for_fn (if body is all const) |
impl Drop |
This is my current thinking for this lint - are there things I'm missing?
What it does
A couple of times, I've seen library authors create
const fn
s that can't usefully be used inconst
contexts.This lint would catch these
Advantage
Catch API bugs
Drawbacks
It's not clear what
useful
means.A candidate is "is the return type const-droppable", but that would flag
Vec::new
. Maybe that's okay?Or maybe the first iteration is to catch
enum
s that can't be dropped in a const context? Or maybe even justResult
sExample