-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Delay of iteration side effects is confusing #14666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Seems like a good idea. I wonder if |
(I'm working on this.) |
huonw
added a commit
to huonw/rust
that referenced
this issue
Jul 9, 2014
It can be a little unintuitive that something like `v.iter().map(|x| println!("{}", x));` does nothing: the majority of the iterator adaptors are lazy and do not execute anything until something calls `next`, e.g. a `for` loop, `collect`, `fold`, etc. The majority of such errors can be seen by someone writing something like the above, i.e. just calling an iterator adaptor and doing nothing with it (and doing this is certainly useless), so we can co-opt the `must_use` lint, using the message functionality to give a hint to the reason why. Fixes rust-lang#14666.
bors
added a commit
that referenced
this issue
Jul 10, 2014
Similar to the stability attributes, a type annotated with `#[must_use = "informative snippet"]` will print the normal warning message along with "informative snippet". This allows the type author to provide some guidance about why the type should be used. --- It can be a little unintuitive that something like `v.iter().map(|x| println!("{}", x));` does nothing: the majority of the iterator adaptors are lazy and do not execute anything until something calls `next`, e.g. a `for` loop, `collect`, `fold`, etc. The majority of such errors can be seen by someone writing something like the above, i.e. just calling an iterator adaptor and doing nothing with it (and doing this is certainly useless), so we can co-opt the `must_use` lint, using the message functionality to give a hint to the reason why. Fixes #14666.
This just saved my bacon, and is SUPER useful. ❤️ |
flip1995
pushed a commit
to flip1995/rust
that referenced
this issue
May 1, 2025
…14666) here is my small fix changelog: fix suggestion span to avoid showing macro name in `.div_ceil()` suggestion i changed this line `let divisor_snippet = snippet_with_applicability(cx, rhs.spansource_callsite(), "..", applicability);` to this line `let divisor_snippet = snippet_with_applicability(cx, rhs.span, "..", applicability);` to fix problem where this warning in macro expands like this ```rust 4 | let _ = (x + 7) / 8; | ^^^^^^^^^^^ help: consider using `.div_ceil()`: `x.div_ceil(y!())` ``` [play it yourself](https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=397aa8cd2ffffb24a286fbddbc75446c) as you can see here it looks like `x.div_ceil(y!())` and contains macro signature so i fixed this problem, i will look closely if there any more problems like this and fix them in order **Related issue** fixes [rust-lang/rust-clippy#14665](rust-lang/rust-clippy#14665)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code produces no output:
It's because the iterator returned by
map
is never used. Switching to.map(f).last()
produces the expected output.Perhaps iterator structs should be
#[must_use]
?The text was updated successfully, but these errors were encountered: