Skip to content

compiler: suggest .collect() when String is expected and Iterator is found#156121

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
thiago-fealves:suggest-collect-string
May 18, 2026
Merged

compiler: suggest .collect() when String is expected and Iterator is found#156121
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
thiago-fealves:suggest-collect-string

Conversation

@thiago-fealves
Copy link
Copy Markdown
Contributor

@thiago-fealves thiago-fealves commented May 3, 2026

View all comments

This commit adds a diagnostic suggestion to help users who forget to call .collect() when they have an iterator and the function or variable expects a String.

The logic checks if the expected type is std::string::String and if the found type implements the Iterator trait, if so the compiler provides a suggestion to add .collect()

Includes also a UI test to verify if the suggestion appears correctly

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 3, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 3, 2026

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 73 candidates
  • Random selection from 22 candidates

@thiago-fealves thiago-fealves force-pushed the suggest-collect-string branch from 5513a24 to 41aa2be Compare May 3, 2026 23:25
Comment thread tests/ui/suggestions/suggest-collect.rs
@rust-log-analyzer

This comment has been minimized.

@teor2345
Copy link
Copy Markdown
Contributor

teor2345 commented May 4, 2026

The job aarch64-gnu-llvm-21-1 failed!

Looks like you need to run ./x test --bless tests/ui to update the expected test outputs for existing tests with the new suggestion.

Most of the failures are:

help: consider using .collect() to convert the Iterator into a ()

Is this a useful suggestion?
We might want to skip the unit type if other suggestions are more useful, or put it lower down in the suggestion priority list.

@thiago-fealves
Copy link
Copy Markdown
Contributor Author

The job aarch64-gnu-llvm-21-1 failed!

Looks like you need to run ./x test --bless tests/ui to update the expected test outputs for existing tests with the new suggestion.

Most of the failures are:

help: consider using .collect() to convert the Iterator into a ()

Is this a useful suggestion? We might want to skip the unit type if other suggestions are more useful, or put it lower down in the suggestion priority list.

I didn't anticipate this could happen, i was alway thinking about more concrete types like String and int, honestly i think in this case the suggestion is not being useful for the user anymore, i can't think of a situation where you would collect into () at least. What do you suggest in this case?

I'm gonna implement more tests for non-String objects in the meantime

@teor2345
Copy link
Copy Markdown
Contributor

teor2345 commented May 4, 2026

I didn't anticipate this could happen, i was alway thinking about more concrete types like String and int, honestly i think in this case the suggestion is not being useful for the user anymore, i can't think of a situation where you would collect into () at least. What do you suggest in this case?

The docs say this is intended for cases like Result<(), E>:
https://doc.rust-lang.org/std/iter/trait.FromIterator.html#impl-FromIterator%3C()%3E-for-()

So skipping the suggestion when the expected type is just () seems like a good idea.

@thiago-fealves
Copy link
Copy Markdown
Contributor Author

I didn't anticipate this could happen, i was alway thinking about more concrete types like String and int, honestly i think in this case the suggestion is not being useful for the user anymore, i can't think of a situation where you would collect into () at least. What do you suggest in this case?

The docs say this is intended for cases like Result<(), E>: https://doc.rust-lang.org/std/iter/trait.FromIterator.html#impl-FromIterator%3C()%3E-for-()

So skipping the suggestion when the expected type is just () seems like a good idea.

I agree, gonna finish implementing that and adding the UI tests.

…r` is found

This commit adds a diagnostic suggestion to help users who forget to
call `.collect()` when they have an iterator and the function or
variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if
the found type implements the `Iterator` trait, if so the compiler
provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
@thiago-fealves thiago-fealves force-pushed the suggest-collect-string branch from 41aa2be to a4bd5f5 Compare May 4, 2026 13:55
@thiago-fealves
Copy link
Copy Markdown
Contributor Author

Just added a check for the unit type, let's see if the tests pass now

@thiago-fealves
Copy link
Copy Markdown
Contributor Author

Looks like tests succeeded!

Comment thread tests/ui/suggestions/suggest-collect.rs
Comment thread compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs Outdated
Co-authored-by: Qai Juang <qaijuang@gmail.com>
Copy link
Copy Markdown
Contributor

@teor2345 teor2345 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me, but I'm not a typeck expert.

Feel free to resolve my earlier feedback, I think GitHub is having issues and dropping my attempts to do it.

View changes since this review

@estebank estebank changed the title compiler: suggest .collect() when String is expected and `Iterato… compiler: suggest .collect() when String is expected and Iterator is found May 15, 2026
@estebank
Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 15, 2026

📌 Commit e1941ff has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 15, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 16, 2026
…bank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found

This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 16, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 16, 2026

💔 Test for f2434d3 failed: CI. Failed job:

@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
#13 DONE 32.2s

#14 [ 6/25] RUN apt-key adv --batch --yes --keyserver keyserver.ubuntu.com --recv-keys 74DA7924C5513486
#14 0.159 Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
#14 0.176 Executing: /tmp/apt-key-gpghome.zToD4iKYCd/gpg.1.sh --batch --yes --keyserver keyserver.ubuntu.com --recv-keys 74DA7924C5513486
#14 1.978 gpg: key 74DA7924C5513486: public key "Igor Kozhukhov <igor@dilos.org>" imported
#14 1.982 gpg: Total number processed: 1
#14 1.982 gpg:               imported: 1
#14 DONE 2.1s

@Kobzol
Copy link
Copy Markdown
Member

Kobzol commented May 16, 2026

@bors retry

Spurious failure fixed by #156640.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 16, 2026
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 16, 2026
…ing, r=estebank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found

This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
rust-bors Bot pushed a commit that referenced this pull request May 16, 2026
Rollup of 6 pull requests

Successful merges:

 - #152852 (Remove driver_lint_caps)
 - #156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - #156518 ([style] rustfmt `match`es with comments in or-patterns)
 - #156596 (Split `LintExpectationId`s)
 - #156577 (Test EII UI tests with prefer-dynamic)
 - #156633 (Add regression test for issue #41261)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 17, 2026
…bank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found



This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

The job x86_64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test expr_store::scope::tests::test_shadow_variable ... ok
test expr_store::tests::body::array_element_cfg ... ok
test expr_store::tests::body::async_fn_weird_param_patterns ... ok
test expr_store::tests::body::block::inner_item_smoke ... ok
error: test failed, to rerun pass `-p hir-def --lib`

Caused by:
  process didn't exit successfully: `/checkout/obj/build/x86_64-unknown-linux-gnu/stage2-tools/x86_64-unknown-linux-gnu/release/deps/hir_def-f381e8bd2208ec8b '--skip=tests::smoke_test_real_sysroot_cargo' --skip=check_code_formatting -Z unstable-options --format json` (signal: 11, SIGSEGV: invalid memory reference)
Bootstrap failed while executing `--stage 2 test --skip tests --skip coverage-map --skip coverage-run --skip library --skip tidyselftest`
Build completed unsuccessfully in 1:04:34
  local time: Sun May 17 01:26:49 UTC 2026
  network time: Sun, 17 May 2026 01:26:49 GMT
##[error]Process completed with exit code 1.

@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels May 17, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 17, 2026

💔 Test for c9ce8ae failed: CI. Failed job:

@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@bors retry

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 17, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 17, 2026
…ing, r=estebank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found

This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request May 17, 2026
…ing, r=estebank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found

This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
rust-bors Bot pushed a commit that referenced this pull request May 17, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #151742 (Remove redundant information in `rustc_abi::Variants`)
 - #151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - #156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - #156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - #156596 (Split `LintExpectationId`s)
 - #156607 (ci: Update FreeBSD version to FreeBSD 14)
 - #156376 (suggest hex escapes for C-style escapes)
 - #156577 (Test EII UI tests with prefer-dynamic)
 - #156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - #156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - #156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - #156633 (Add regression test for issue #41261)
 - #156635 (rename unexpected_try_recover function)
 - #156636 (minor `rustc_mir_transform` cleanup)
rust-bors Bot pushed a commit that referenced this pull request May 17, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #151742 (Remove redundant information in `rustc_abi::Variants`)
 - #151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - #156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - #156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - #156596 (Split `LintExpectationId`s)
 - #156607 (ci: Update FreeBSD version to FreeBSD 14)
 - #156376 (suggest hex escapes for C-style escapes)
 - #156577 (Test EII UI tests with prefer-dynamic)
 - #156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - #156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - #156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - #156633 (Add regression test for issue #41261)
 - #156635 (rename unexpected_try_recover function)
 - #156636 (minor `rustc_mir_transform` cleanup)
@rust-bors rust-bors Bot merged commit fe32c70 into rust-lang:main May 18, 2026
11 of 12 checks passed
@rustbot rustbot added this to the 1.97.0 milestone May 18, 2026
rust-timer added a commit that referenced this pull request May 18, 2026
Rollup merge of #156121 - thiago-fealves:suggest-collect-string, r=estebank

compiler: suggest `.collect()` when `String` is expected and `Iterator` is found

This commit adds a diagnostic suggestion to help users who forget to call `.collect()` when they have an iterator and the function or variable expects a `String`.

The logic checks if the expected type is `std::string::String` and if the found type implements the `Iterator` trait, if so the compiler provides a suggestion to add `.collect()`

Includes also a UI test to verify if the suggestion appears correctly
@thiago-fealves thiago-fealves deleted the suggest-collect-string branch May 18, 2026 03:02
github-actions Bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request May 18, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - rust-lang/rust#151742 (Remove redundant information in `rustc_abi::Variants`)
 - rust-lang/rust#151362 (Add interior-mutability suggestion to `static_mut_refs`)
 - rust-lang/rust#156121 (compiler: suggest `.collect()` when `String` is expected and `Iterator` is found)
 - rust-lang/rust#156208 (Emit retags in codegen to support BorrowSanitizer (part 1))
 - rust-lang/rust#156596 (Split `LintExpectationId`s)
 - rust-lang/rust#156607 (ci: Update FreeBSD version to FreeBSD 14)
 - rust-lang/rust#156376 (suggest hex escapes for C-style escapes)
 - rust-lang/rust#156577 (Test EII UI tests with prefer-dynamic)
 - rust-lang/rust#156585 (explicit tail calls: ignore some tests on unsupported LLVM targets)
 - rust-lang/rust#156598 (Avoid rustfix suggestions for macro-expanded unused imports)
 - rust-lang/rust#156616 (rustdoc: add test case for `-Drustdoc::` and `--cap-lints`)
 - rust-lang/rust#156633 (Add regression test for issue rust-lang/rust#41261)
 - rust-lang/rust#156635 (rename unexpected_try_recover function)
 - rust-lang/rust#156636 (minor `rustc_mir_transform` cleanup)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants