-
Notifications
You must be signed in to change notification settings - Fork 1.7k
or_fun_call triggers on const fn #5658
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
cc #1609 |
Linking this relevant comment so that it does not get buried in a closed PR: #5682 (comment) |
…arth Avoid or_fun_call for const_fn with no args Based on #5682 by @lzutao This avoids a subset of false positives, specifically those related to `const fn`s that take no arguments. For the rest, a much more involved fix would be needed, see #5682 (comment). So this does *not* solve #5658 changelog: Avoid triggering [`or_fun_call`] with `const fn`s that take no arguments. Fixes #5886
Are const fn calls with const arguments guaranteed to be evaluated at compile-time? From the unstable book I understand that they can be called in a const context, but nothing about evaluation guarantees. |
Good question. @oli-obk can you give some insights here? |
@montrivo No, it is only guaranteed to evaluate at compile time in const item or static item. |
If so, then I don't think clippy should handle Continuing on @oli-obk 's example: const fn compute_nth_prime(n: usize) -> Option<usize> {
// super heavy logic here
}
// here recommending `unwrap_or_else` is questionable since there is no runtime performance improvement
// (although it might improve compilation time if `compute_nth_prime` is really heavy)
const a_prime_number: i32 = Some(13).unwrap_or(comput_nth_prime(42));
// here the lint is applicable, as unwrap_or will cause an unnecessary runtime cost
// given compute_nth_prime(42) is not evaluated during compilation
fn main() {
println!("{}", Some(42).unwrap_or(compute_nth_prime(42)));
} |
(I was mistaken) |
Relevant Zulip conversation, IIUC @montrivo is on the right track. On my side, I was confused between constant evaluation and constant propagation, the latter has nothing to do with |
@montrivo I think you're right, I've opened #6077 to revert them. Continuing with your example, IIUC none of the methods linted by |
@mahkoh I'm going to close this issue as I think this is not a false positive, feel free to reopen in case you think otherwise. |
The text was updated successfully, but these errors were encountered: