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

or_fun_call suggestion causing error on temporary value #9205

Open
J-ZhengLi opened this issue Jul 19, 2022 · 1 comment
Open

or_fun_call suggestion causing error on temporary value #9205

J-ZhengLi opened this issue Jul 19, 2022 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing E-hard Call for participation: This a hard problem and requires more experience or effort to work on 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

@J-ZhengLi
Copy link
Member

J-ZhengLi commented Jul 19, 2022

Summary

I found this error when using a reference of function return as optb in or() method, which was fine, but clippy suggesting me to use or_else but applying the suggestion will breaks the code.

Lint Name

or_fun_call

Reproducer

I tried this code:

struct Paths {
    some_path: Option<PathBuf>,
}

fn some_other_path() -> Option<PathBuf> {
    Some(PathBuf::from("path/b"))
}

fn clippy_or_fn_call(paths: &Paths) {
    let _ = paths.some_path.as_deref().or(some_other_path().as_deref());
}

I saw this happen:

warning: use of `or` followed by a function call
  --> src\main.rs:47:40
   |
47 |     let _ = paths.some_path.as_deref().or(some_other_path().as_deref());
   |                                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_else(|| some_other_path().as_deref())`
   |
   = note: `#[warn(clippy::or_fun_call)]` on by default

but if I use the suggestion my code won't compile and gives below error:

error[E0515]: cannot return reference to temporary value
  --> src\main.rs:47:51
   |
47 |     let _ = paths.some_path.as_deref().or_else(|| some_other_path().as_deref());
   |                                                   -----------------^^^^^^^^^^^
   |                                                   |
   |                                                   returns a reference to data owned by the current function
   |                                                   temporary value created here

For more information about this error, try `rustc --explain E0515`.

I expected to see this happen:
offers a valid suggestion or no warning

Version

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-pc-windows-msvc
release: 1.62.0
LLVM version: 14.0.5

Additional Labels

@rustbot label +I-suggestion-causes-error

@J-ZhengLi J-ZhengLi 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 Jul 19, 2022
@rustbot rustbot added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Jul 19, 2022
J-ZhengLi added a commit to J-ZhengLi/rust-clippy that referenced this issue Mar 12, 2024
J-ZhengLi added a commit to J-ZhengLi/rust-clippy that referenced this issue Mar 12, 2024
@J-ZhengLi J-ZhengLi changed the title or_fn_call suggestion causing error on temporary value or_fun_call suggestion causing error on temporary value Mar 13, 2024
@J-ZhengLi
Copy link
Member Author

this is very similar to #6761 , probably could be fixed together. I tried to fix it but got no luck 😭

Also, this code also triggers it:

Some("ok".to_string()).as_ref().or(Some(&"err".to_string()));

@rustbot label +E-hard

@rustbot rustbot added the E-hard Call for participation: This a hard problem and requires more experience or effort to work on label Mar 13, 2024
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-hard Call for participation: This a hard problem and requires more experience or effort to work on 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

No branches or pull requests

2 participants