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 code does not compile with or_fun_call #2186

Closed
jDomantas opened this issue Oct 28, 2017 · 3 comments
Closed

Suggested code does not compile with or_fun_call #2186

jDomantas opened this issue Oct 28, 2017 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions

Comments

@jDomantas
Copy link

In some cases suggested change regarding or_fun_call lint does not compile. Here's an example:

struct Pair {
    a: u32,
    b: u32,
}

fn foo(x: u32) -> u32 { x }

fn bar(pair: &mut Pair) {
    let borrow = &mut pair.a;
    let b = None.unwrap_or(foo(pair.b));
}

pair is partially borrowed. Calling id(pair.b) is valid, because compiler can see that only field a is borrowed, so field b can be freely used. Clippy suggests to use unwrap_or_else, and move function call into a closure. However, the closure will borrow pair fully which is not possible, and the code will not compile.

@phansch phansch added C-bug Category: Clippy is not doing the correct thing L-suggestion Lint: Improving, adding or fixing lint suggestions labels Dec 11, 2018
@phansch phansch added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jan 4, 2019
@tesuji
Copy link
Contributor

tesuji commented May 2, 2020

I believed NLL solved this.
The code after applying suggestions compiles successfully on Rust 1.42.

@flip1995 flip1995 closed this as completed May 2, 2020
@jDomantas
Copy link
Author

This specific example is only solved just because borrow is not used later, you only need to add a use after unwrap_or to make the suggestion incorrect again:

struct Pair {
    a: u32,
    b: u32,
}

fn foo(x: u32) -> u32 { x }

fn bar(pair: &mut Pair) {
    let borrow = &mut pair.a;
    let b = None.unwrap_or(foo(pair.b));
    let x = *borrow;
}

@tesuji
Copy link
Contributor

tesuji commented May 15, 2020

I think with this RFC (https://rust-lang.github.io/rfcs/2229-capture-disjoint-fields.html) the code should be accepted and compiled.

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-suggestion Lint: Improving, adding or fixing lint suggestions
Projects
None yet
Development

No branches or pull requests

4 participants