Skip to content

or_fun_call suggestion causing error on temporary value #9205

@J-ZhengLi

Description

@J-ZhengLi
Member

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

Activity

added
C-bugCategory: Clippy is not doing the correct thing
I-false-positiveIssue: The lint was triggered on code it shouldn't have
on Jul 19, 2022
added
I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
on Jul 19, 2022
changed the title [-]`or_fn_call` suggestion causing error on temporary value[/-] [+]`or_fun_call` suggestion causing error on temporary value[/+] on Mar 13, 2024
J-ZhengLi

J-ZhengLi commented on Mar 13, 2024

@J-ZhengLi
MemberAuthor

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

added
E-hardCall for participation: This a hard problem and requires more experience or effort to work on
on Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingE-hardCall for participation: This a hard problem and requires more experience or effort to work onI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @J-ZhengLi@rustbot

        Issue actions

          `or_fun_call` suggestion causing error on temporary value · Issue #9205 · rust-lang/rust-clippy