rust-lang / rust-lang/rust-clippy

or_fun_call suggests invalid code by removing type information

Open
#9,790 2 comments 2 reactions 1 assignee View on GitHub

@J-ZhengLi is already working on this.

Since Jul 19, 2024.

C-bug I-suggestion-causes-error L-nursery
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

Clippy suggests/auto-fixes to invalid code for lint or_fun_call. It's invalid if the inserted value contains type information that is not available elsewhere.

https://rust-lang.github.io/rust-clippy/master/#or_fun_call

Reproducer

Minimal example (working minimal repro available to run at https://github.com/tommilligan/rust-clippy-repro-9790):

use std::collections::HashMap;

pub fn example() {
    let mut parent = HashMap::new();
    let grandchild = parent.entry(2).or_insert(Vec::new());
    grandchild.push(-1);
}

Output:

warning: failed to automatically apply fixes suggested by rustc to crate `clippy_new_after_insert`

after fixes were automatically applied the compiler reported errors within these files:

  * src/lib.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0282]: type annotations needed for `&mut V`
 --> src/lib.rs:5:9
  |
5 |     let grandchild = parent.entry(2).or_default();
  |         ^^^^^^^^^^
6 |     grandchild.push(-1);
  |                ---- type must be known at this point
  |
help: consider giving `grandchild` an explicit type, where the type for type parameter `V` is specified
  |
5 |     let grandchild: &mut V = parent.entry(2).or_default();
  |                   ++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
Original diagnostics will follow.

warning: use of `or_insert` followed by a call to `new`
 --> src/lib.rs:5:38
  |
5 |     let grandchild = parent.entry(2).or_insert(Vec::new());
  |                                      ^^^^^^^^^^^^^^^^^^^^^ help: try this: `or_default()`
  |
  = note: `#[warn(clippy::or_fun_call)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
Version
rustc 1.65.0 (897e37553 2022-11-02)
binary: rustc
commit-hash: 897e37553bba8b42751c67658967889d11ecd120
commit-date: 2022-11-02
host: x86_64-unknown-linux-gnu
release: 1.65.0
LLVM version: 15.0.0
Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.