-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.I-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn'tL-nurseryLint: Currently in the nursery groupLint: Currently in the nursery group
Description
#[deny(clippy::option_if_let_else)]
pub fn foo(o: Option<i32>, f: impl FnOnce(i32), g: impl FnOnce()) {
if let Some(i) = o {
f(i)
}
else {
g()
}
}
The lint suggests o.map_or(g(), |i| f(i))
, which is wrong because the else
branch should be evaluated lazily because it's a function call. It ought to have suggested o.map_or_else(|| g(), |i| f(i))
Meta
cargo clippy -V
:clippy 0.0.212 (39d5a61 2020-07-17)
rustc -Vv
:rustc 1.47.0-nightly (39d5a61f2 2020-07-17) binary: rustc commit-hash: 39d5a61f2e4e237123837f5162cc275c2fd7e625 commit-date: 2020-07-17 host: x86_64-unknown-linux-gnu release: 1.47.0-nightly LLVM version: 10.0
daira and cd-a
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messagesE-mediumCall for participation: Medium difficulty level problem and requires some initial experience.Call for participation: Medium difficulty level problem and requires some initial experience.I-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn'tL-nurseryLint: Currently in the nursery groupLint: Currently in the nursery group
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Arnavion commentedon Jul 19, 2020
I now see that #5301 (comment) implies this is intentional, and that after the suggestion, the
clippy::or_fun_call
lint would suggest changing it tomap_or_else
. Howeverdoes not raise any errors.
It does cause the
clippy::unit_arg
lint to fire, but that lint itself has a bad suggestion, for which I've filed #5823So if we modify the code to remove the possibility of triggering
clippy::unit_arg
:this code still does not raise any errors.
Perhaps this issue can be repurposed to figure out why
clippy::or_fun_call
did not fire on the code after applyingclippy::if_let_else_
's suggestion.Option::map_or
(_else) forif let Some { y } else { x }
#5301tnielens commentedon Aug 11, 2020
I think
clippy::option_if_let_else
should recommendmap_or_else
by default to preserve the code semantic. If the else block consists in a single identifier (or another pattern without side-effect), thenmap_or
is applicable.tnielens commentedon Aug 12, 2020
doesn't trigger the lint because it only looks for function calls of at least one argument. See in methods/mod.rs.
The reasoning was maybe that functions taking no arguments have probably side-effects.Auto merge of #5937 - montrivo:option_if_let_else, r=flip1995
or_fun_call
lint shouldn't ignore indexing to HashMap types #6266xFrednet commentedon Feb 13, 2021
This issue seems to be fixed. Clippy at least suggests the usage of
map_or_else
for the example code. PlaygroundArnavion commentedon Feb 13, 2021
The change you're seeing is from #5937 , which considered this issue to only be "partially fixed". I'm not sure what the unfixed part is, however.
or_fun_call
doesn't suggestmap_or_else
#8993J-ZhengLi commentedon Jul 10, 2024
as of today, this does not lint at all, not sure when that happened or whether it was on purpose or not