Skip to content

Wrong coloring on "consider removing this method call" #134485

Closed
@est31

Description

@est31

Consider this code:

use std::collections::{HashMap, HashSet};
fn foo() -> Vec<(bool, HashSet<u8>)> {
	let mut hm = HashMap::<bool, Vec<HashSet<u8>>>::new();
	hm.into_iter()
		.map(|(is_true, ts)| {
			ts.into_iter()
				.map(|t| (is_true, t))
				.flatten()
		})
		.flatten()
		.collect()
}

It gives:

error[E0277]: `(bool, HashSet<u8>)` is not an iterator
    --> a.rs:8:6
     |
8    |                 .flatten()
     |                  ^^^^^^^ `(bool, HashSet<u8>)` is not an iterator
     |
     = help: the trait `Iterator` is not implemented for `(bool, HashSet<u8>)`
     = note: required for `(bool, HashSet<u8>)` to implement `IntoIterator`
note: required by a bound in `flatten`
    --> src/rust/library/core/src/iter/traits/iterator.rs:1517:21
     |
1514 |     fn flatten(self) -> Flatten<Self>
     |        ------- required by a bound in this associated function
...
1517 |         Self::Item: IntoIterator,
     |                     ^^^^^^^^^^^^ required by this bound in `Iterator::flatten`
help: consider removing this method call, as the receiver has type `std::vec::IntoIter<HashSet<u8>>` and `std::vec::IntoIter<HashSet<u8>>: Iterator` trivially holds
     |
6    -             ts.into_iter()
7    -                 .map(|t| (is_true, t))
6    +             ts.into_iter()
     |

error[E0277]: `(bool, HashSet<u8>)` is not an iterator
   --> a.rs:4:2
    |
4   | /     hm.into_iter()
5   | |         .map(|(is_true, ts)| {
6   | |             ts.into_iter()
7   | |                 .map(|t| (is_true, t))
8   | |                 .flatten()
9   | |         })
    | |__________^ `(bool, HashSet<u8>)` is not an iterator
    |
    = help: the trait `Iterator` is not implemented for `(bool, HashSet<u8>)`
    = note: required for `(bool, HashSet<u8>)` to implement `IntoIterator`
note: required by a bound in `Flatten`
   --> src/rust/library/core/src/iter/adapters/flatten.rs:254:38
    |
254 | pub struct Flatten<I: Iterator<Item: IntoIterator>> {
    |                                      ^^^^^^^^^^^^ required by this bound in `Flatten`

error[E0599]: the method `collect` exists for struct `Flatten<Map<IntoIter<bool, Vec<HashSet<u8>>>, ...>>`, but its trait bounds were not satisfied
   --> a.rs:11:4
    |
4   | /     hm.into_iter()
5   | |         .map(|(is_true, ts)| {
6   | |             ts.into_iter()
7   | |                 .map(|t| (is_true, t))
...   |
10  | |         .flatten()
11  | |         .collect()
    | |         -^^^^^^^ method cannot be called due to unsatisfied trait bounds
    | |_________|
    |
    |
   ::: src/rust/library/core/src/iter/adapters/flatten.rs:254:1
    |
254 |   pub struct Flatten<I: Iterator<Item: IntoIterator>> {
    |   --------------------------------------------------- doesn't satisfy `<_ as IntoIterator>::IntoIter = _`, `<_ as IntoIterator>::Item = _`, `_: IntoIterator` or `_: Iterator`
    |
    = note: the full type name has been written to 'a.long-type-10511036807638515192.txt'
    = note: consider using `--verbose` to print the full type name to the console
    = note: the following trait bounds were not satisfied:
            `<Flatten<Map<std::vec::IntoIter<HashSet<u8>>, {closure@a.rs:7:10: 7:13}>> as IntoIterator>::IntoIter = _`
            which is required by `Flatten<Map<std::collections::hash_map::IntoIter<bool, Vec<HashSet<u8>>>, {closure@a.rs:5:8: 5:23}>>: Iterator`
            `<Flatten<Map<std::vec::IntoIter<HashSet<u8>>, {closure@a.rs:7:10: 7:13}>> as IntoIterator>::Item = _`
            which is required by `Flatten<Map<std::collections::hash_map::IntoIter<bool, Vec<HashSet<u8>>>, {closure@a.rs:5:8: 5:23}>>: Iterator`
            `Flatten<Map<std::vec::IntoIter<HashSet<u8>>, {closure@a.rs:7:10: 7:13}>>: IntoIterator`
            which is required by `Flatten<Map<std::collections::hash_map::IntoIter<bool, Vec<HashSet<u8>>>, {closure@a.rs:5:8: 5:23}>>: Iterator`
            `Flatten<Map<std::collections::hash_map::IntoIter<bool, Vec<HashSet<u8>>>, {closure@a.rs:5:8: 5:23}>>: Iterator`
            which is required by `&mut Flatten<Map<std::collections::hash_map::IntoIter<bool, Vec<HashSet<u8>>>, {closure@a.rs:5:8: 5:23}>>: Iterator`

My bug report is about the coloring in this particular suggestion:

Image

removing the red parts will obviously make the situation worse and cause parsing errors.

Even if you remove the entire map function call, it won't fix all the errors, what is needed is the removal of flatten. I want to make this particular issue about the wrong coloring of that red part: there is some mistake about it.

Appears on latest nightly rustc 1.85.0-nightly (a4cb3c831 2024-12-17) as well as beta rustc 1.84.0-beta.4 (202008a1b 2024-12-07).

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-confusingDiagnostics: Confusing error or lint that should be reworked.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions