return_and_then
: suggests using ?
operator on Option
in function which returns Result
#14781
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-positive
Issue: The lint was triggered on code it shouldn't have
I-suggestion-causes-bug
Issue: The suggestion compiles but changes the code to behave in an unintended way
Summary
Related: #14051
CC: @aaron-ang
It seems that
return_and_then
does not take into account the surrounding context when suggesting a transformation.The transformation suggested, changes meaning of the code (early return) and is only correct inside blocks returning an
Option
. Anywhere else it produces code which does not compile, and if it did compile, it would behave differently.I discovered this by running
So this fix is marked for autofix (or whatever it is called) but is not currently safe to use.
Really good lint I'd like to enable, but needs some more work.
I think 2 parts are missing here (the list is not exhaustive)
let
binding)Option
(my block does not; it returnsResult
)(I don't know the terminology, but by "parent block" I mean the block that the suggested
?
operator will return from)Lint Name
return_and_then
Reproducer
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ed73a58ce166b40d8c9567a5843aa773
I tried this code (a bit contrived, but derived from some real code):
I expected to see this happen:
No warning, or warning with the suggested code being correct.
Instead, this happened:
After applying the transform, the code becomes:
Not only it changes meaning (early return, which original code has no intention to do), but also triggering compiler error:
Version
Additional Labels
No response
The text was updated successfully, but these errors were encountered: