-
Notifications
You must be signed in to change notification settings - Fork 1.7k
new lint: find functions that can be "const fn" #2440
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Yes! This would be great! I would be happy with it even if it just started as a simple lint that encouraged people to make code like this const: impl Game {
// Could be const
pub fn new() -> Self {
Self {
tiles: Default::default(),
current_piece: Piece::X,
winner: None,
}
}
} Of course, it could always grow from there to become more complete. This would just give people a start into using const. Not having const on simple associated functions like this makes it really hard to use these in const declarations in your code. If a library you use doesn't have this, you get completely stuck. That's why I think it's really important to start getting people in the habit of putting const on functions when they can. I think a clippy lint will go a long way. |
So... implementing this lint has become significantly easier lately: Just call https://github.com/rust-lang/rust/blob/master/src/librustc_mir/transform/qualify_min_const_fn.rs#L11 and check the result. There are some false positives, but we can't check them right now outside the compiler (because the check emits errors immediately instead of reporting a |
Trying to write a `const_fn` lint for Clippy. @oli-obk suggested [here][link] to use the `is_min_const_fn` function from the `qualify_min_const_fn` module. However, the module is currently private and this commit makes it public. I lack any historical knowledge of the development of the `const_fn` feature, so I'm not sure if it was private on purpose or not. fwiw, all modules are already public except `qualify_min_const_fn`. [link]: rust-lang/rust-clippy#2440 (comment)
Going to give this a try over the next couple of days. First step is getting rust-lang/rust#57342 merged. |
librustc_mir: Make qualify_min_const_fn module public Trying to write a `const_fn` lint for Clippy. @oli-obk suggested [here][link] to use the `is_min_const_fn` function from the `qualify_min_const_fn` module. However, the module is currently private and this commit makes it public. I lack any historical knowledge of the development of the `const_fn` feature, so I'm not sure if it was private on purpose or not. fwiw, all modules are already public except `qualify_min_const_fn`. r? @oli-obk [link]: rust-lang/rust-clippy#2440 (comment)
Add initial version of const_fn lint This adds an initial version of a lint that can tell if a function could be `const`. TODO: - [x] Finish up the docs - [ ] Fix the ICE cc #2440
Add initial version of const_fn lint This adds an initial version of a lint that can tell if a function could be `const`. TODO: - [x] Finish up the docs - [x] Fix the ICE cc #2440
With @phansch's PR merged is this issue completely implemented? |
@sunjay Yeah, I guess we can handle bugs and further improvements via new issues, so I'm going to close this one, thanks! |
Awesome! Thank you for your work 😁🎉 |
Maybe we can automatically find simple functions that can be
const fn
This was suggested during FOSDEM talk on rusts const evaluation: https://youtu.be/Zz863ksXRhA?t=1102
I didn't see a ticket about this yet so here we go.
The text was updated successfully, but these errors were encountered: