rust-lang / rust-lang/rust-clippy
`or_fun_call` suggestion causing error on temporary value
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the or_fun_call diagnostic with the src/main.rs example and the reported Rust version. Inspect the or_fun_call lint entry point and its tests, then verify that the suggestion either compiles for temporary values or that no warning is emitted. Done means the reproducer no longer receives an invalid suggestion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100