Skip to content

or_fun_call suggestion causing error on temporary value #9205

Open

Description

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

    Issue actions