rust-lang / rust-lang/rust-clippy
`needless_collect` causes compiler overflow if followed
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Lint name: needless_collect
I tried this code which compiles fine and results in an (expected) panic:
use std::iter;
pub type Ident = String;
#[derive(Debug, PartialEq, Clone)]
pub enum Type {
Var(Ident),
}
/// Type substitutions
type Substitution = (Ident, Type);
type Constraint = (Type, Type);
pub fn unify(
mut constraints: impl Iterator<Item = Constraint>,
) -> Box<dyn Iterator<Item = Result<Substitution, String>>> {
match constraints.next() {
Some((Type::Var(ident), other)) => {
let subst = (ident, other);
let tmp: Vec<_> = constraints.map(|x| x.clone()).collect();
Box::new(unify(tmp.into_iter()).chain(iter::once(Ok(subst))))
}
_ => panic!("Should panic here"),
}
}
fn main() {
let t1 = Type::Var("x".to_string());
let t2 = Type::Var("y".to_string());
unify(iter::once((t1, t2)))
.collect::<Result<Vec<_>, String>>()
.unwrap();
}
I then follow this suggestion from clippy:
warning: avoid using `collect()` when not needed
--> src/main.rs:21:62
|
21 | let tmp: Vec<_> = constraints.map(|x| x.clone()).collect();
| ^^^^^^^
22 | Box::new(unify(tmp.into_iter()).chain(iter::once(Ok(subst))))
| --------------- the iterator could be used here instead
|
= note: `#[warn(clippy::needless_collect)]` on by default
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_collect
help: use the original Iterator instead of collecting it and then producing a new one
|
21 ~
22 ~ Box::new(unify(constraints.map(|x| x.clone())).chain(iter::once(Ok(subst))))
|
And turn my code into the following
use std::iter;
pub type Ident = String;
#[derive(Debug, PartialEq, Clone)]
pub enum Type {
Var(Ident),
}
/// Type substitutions
type Substitution = (Ident, Type);
type Constraint = (Type, Type);
pub fn unify(
mut constraints: impl Iterator<Item = Constraint>,
) -> Box<dyn Iterator<Item = Result<Substitution, String>>> {
match constraints.next() {
Some((Type::Var(ident), other)) => {
let subst = (ident, other);
Box::new(unify(constraints.map(|x| x.clone())).chain(iter::once(Ok(subst))))
}
_ => panic!("Should panic here"),
}
}
fn main() {
let t1 = Type::Var("x".to_string());
let t2 = Type::Var("y".to_string());
unify(iter::once((t1, t2)))
.collect::<Result<Vec<_>, String>>()
.unwrap();
}
But now the compiler gives me the following error:
$ rustc ~/tmp/clippy.rs
error: reached the recursion limit while instantiating `unify::<Map<Map<Map<Map<Map<Map<...rd/tmp/clippy.rs:21:44: 21:57]>>`
--> /home/munksgaard/tmp/clippy.rs:21:22
|
21 | Box::new(unify(constraints.map(|x| x.clone())).chain(iter::once(Ok(subst))))
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: `unify` defined here
--> /home/munksgaard/tmp/clippy.rs:15:1
|
15 | / pub fn unify(
16 | | mut constraints: impl Iterator<Item = Constraint>,
17 | | ) -> Box<dyn Iterator<Item = Result<Substitution, String>>> {
| |___________________________________________________________^
= note: the full type name has been written to 'clippy.long-type.txt'
error: aborting due to previous error
and on the playground it just times out.
Meta
Rust version (rustc -Vv):
$ rustc -Vv
rustc 1.58.0-nightly (c9c4b5d72 2021-11-17)
binary: rustc
commit-hash: c9c4b5d7276297679387189d96a952f2b760e7ad
commit-date: 2021-11-17
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the needless_collect lint and reproduce the issue using the Rust example and Playground links. Investigate why its suggested replacement causes recursive iterator type instantiation, then verify that the suggestion no longer reaches the compiler recursion limit while preserving the intended lint behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 40/100