Skip to content

or_fun_call suggests invalid code by removing type information #9790

@tommilligan

Description

@tommilligan

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedL-nurseryLint: Currently in the nursery group

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions