Skip to content

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

Closed
kmcallister opened this issue Jun 5, 2014 · 3 comments · Fixed by #15561
Closed

Delay of iteration side effects is confusing #14666

kmcallister opened this issue Jun 5, 2014 · 3 comments · Fixed by #15561

Comments

@kmcallister
Copy link
Contributor

This code produces no output:

fn main() {
    let xs = &[1u,2u,3u];
    let f = |x: &uint| {
        println!("{:u}", *x);
    };
    xs.iter().map(f);
}

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]?

@huonw huonw added the A-libs label Jun 5, 2014
@huonw
Copy link
Member

huonw commented Jun 15, 2014

Perhaps iterator structs should be #[must_use]?

Seems like a good idea. I wonder if #[must_use] could allow specifying a explanation that is printed with the other warnings, so they could have something like #[must_use="iterator adaptors execute lazily"].

@huonw
Copy link
Member

huonw commented Jul 9, 2014

(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.
@emberian
Copy link
Member

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
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants