Skip to content

FP unnecessary_fold: no longer able to infer type #10000

Closed

Description

@matthiaskrgr

Summary

.

Lint Name

unnecessary_fold

Reproducer

I tried this code:

use std::collections::HashMap;
use std::hash::BuildHasher;

fn smoketest_map<S: BuildHasher>(mut map: HashMap<i32, i32, S>) {
    map.insert(0, 0);
    assert_eq!(map.values().fold(0, |x, y| x + y), 0);
}

fn main() {
    smoketest_map(HashMap::new());
}

I saw this happen:


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

  * src/main.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[E0283]: type annotations needed
    --> src/main.rs:6:29
     |
6    |     assert_eq!(map.values().sum(), 0);
     |                             ^^^ cannot infer type of the type parameter `S` declared on the associated function `sum`
     |
     = note: cannot satisfy `_: std::iter::Sum<&i32>`
note: required by a bound in `std::iter::Iterator::sum`
    --> /home/matthias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/iter/traits/iterator.rs:3379:12
     |
3379 |         S: Sum<Self::Item>,
     |            ^^^^^^^^^^^^^^^ required by this bound in `std::iter::Iterator::sum`
help: consider specifying the type argument in the function call
     |
6    |     assert_eq!(map.values().sum::<S>(), 0);
     |                                +++++

error: aborting due to previous error

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

warning: this `.fold` can be written more succinctly using another method
 --> src/main.rs:6:29
  |
6 |     assert_eq!(map.values().fold(0, |x, y| x + y), 0);
  |                             ^^^^^^^^^^^^^^^^^^^^^ help: try: `sum()`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold
  = note: `#[warn(clippy::unnecessary_fold)]` on by default

Version

rustc 1.67.0-nightly (e0098a5cc 2022-11-29)
binary: rustc
commit-hash: e0098a5cc3a87d857e597af824d0ce1ed1ad85e0
commit-date: 2022-11-29
host: x86_64-unknown-linux-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4

Additional Labels

No response

Activity

  1. added
    C-bugCategory: Clippy is not doing the correct thing
    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied
    I-false-positiveIssue: The lint was triggered on code it shouldn't have
    on Nov 30, 2022
  2. added a commit that references this issue on Jun 15, 2023
    2dd452f
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 thingI-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