-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied
Description
Summary
With clippy 0.1.61 (2022-03-01 f0c4da4)
, the iter_overeager_cloned
lint suggests switching cloned().flatten()
to flatten().cloned()
on an iterator of a slice of ranges.
This doesn't work because &std::ops::Range<{integer}>
doesn't implement Iterator
; only std::ops::Range<{integer}>
does (so the cloned
is important).
Reproducer
I tried this code that compiles:
fn main() {
let ranges = vec![1..3, 4..8];
let _something: Vec<_> = ranges.iter().cloned().flatten().collect();
}
and clippy warned:
warning: called `cloned().flatten()` on an `Iterator`. It may be more efficient to call `flatten().cloned()` instead
[--> src/main.rs:3:30
](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021#) |
3 | let _something: Vec<_> = ranges.iter().cloned().flatten().collect();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `ranges.iter().flatten().cloned()`
|
= note: `#[warn(clippy::iter_overeager_cloned)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#iter_overeager_cloned
so I changed my code to:
fn main() {
let ranges = vec![1..3, 4..8];
let _something: Vec<_> = ranges.iter().flatten().cloned().collect();
}
and got these compiler errors:
error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): `&std::ops::Range<{integer}>` is not an iterator
[--> src/main.rs:3:44
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#) |
3 | let _something: Vec<_> = ranges.iter().flatten().cloned().collect();
| ^^^^^^^ `&std::ops::Range<{integer}>` is not an iterator
|
= help: the trait `Iterator` is not implemented for `&std::ops::Range<{integer}>`
= note: required because of the requirements on the impl of `IntoIterator` for `&std::ops::Range<{integer}>`
note: required by a bound in `flatten`
error[[E0599]](https://doc.rust-lang.org/stable/error-index.html#E0599): the method `cloned` exists for struct `Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>`, but its trait bounds were not satisfied
[--> src/main.rs:3:54
](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021#) |
3 | let _something: Vec<_> = ranges.iter().flatten().cloned().collect();
| ^^^^^^ method cannot be called on `Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`<&std::ops::Range<{integer}> as IntoIterator>::IntoIter = _`
which is required by `Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>: Iterator`
`<&std::ops::Range<{integer}> as IntoIterator>::Item = _`
which is required by `Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>: Iterator`
`&std::ops::Range<{integer}>: IntoIterator`
which is required by `Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>: Iterator`
`Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>: Iterator`
which is required by `&mut Flatten<std::slice::Iter<'_, std::ops::Range<{integer}>>>: Iterator`
Some errors have detailed explanations: E0277, E0599.
I expected to get a suggestion from clippy that would compile.
Instead, I got a suggestion that doesn't compile.
Version
rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: aarch64-apple-darwin
release: 1.59.0
LLVM version: 13.0.0
Additional Labels
No response
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when appliedIssue: The suggestions provided by this Lint cause an ICE/error when applied