Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested map_or_else when value partialy moved #13964

Closed
t-webber opened this issue Jan 8, 2025 · 3 comments · Fixed by #14209
Closed

Suggested map_or_else when value partialy moved #13964

t-webber opened this issue Jan 8, 2025 · 3 comments · Fixed by #14209
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@t-webber
Copy link

t-webber commented Jan 8, 2025

Summary

The clippy::map_or_else lint suggests erroneous code when partially moving the value in one arm and using it in another.

The ownership is dropped before the test for a map_or_else, but only after the test when doing a match.

Lint Name

map_or_else

Reproducer

I tried this code

#![warn(clippy::nursery)]

#[derive(Default, Debug)]
struct A(Option<String>);

fn main() {
    let a = A::default();
    let _ = match a.0 {
        Some(x) => x,
        None => panic!("{a:?} is invalid."),
    };
}

I saw this happen

warning: use Option::map_or_else instead of an if let/else
[...]
help: try: `a.0.map_or_else(|| panic!("{a:?} is invalid."), |x| x)`

If I follow the lint

#[derive(Default, Debug)]
struct A(Option<String>);

fn main() {
    let a = A::default();
    let _ = a.0.map_or_else(|| panic!("{a:?} is invalid."), |x| x);
}

I get a compilation error: borrow of partially moved value: a (this is normal as a is moved when accessing the 0 field).

I expected

I expected the warning not to be triggered in this case

Version

rustc 1.85.0-nightly (7f75bfa1a 2024-12-30)
binary: rustc
commit-hash: 7f75bfa1ad4e9a9d33a179a90603001515e91991
commit-date: 2024-12-30
host: x86_64-unknown-linux-gnu
release: 1.85.0-nightly
LLVM version: 19.1.6

Additional Labels

@rustbot label +I-suggestion-causes-error
@rustbot label +E-medium

@t-webber t-webber added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 8, 2025
@rustbot rustbot added I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied E-medium Call for participation: Medium difficulty level problem and requires some initial experience. labels Jan 9, 2025
@yegeunyang

This comment has been minimized.

@yegeunyang

This comment has been minimized.

@profetia
Copy link
Contributor

@rustbot claim

github-merge-queue bot pushed a commit that referenced this issue Mar 19, 2025
fixes #13964

The lint `option_map_unwrap_or` used to have a similar issue in #10579,
so I borrowed its solution to fix this one.

changelog: [`option_if_let_else`]: fix FP when value partially moved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing E-medium Call for participation: Medium difficulty level problem and requires some initial experience. I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants