-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Allow a message on #[must_use] & mark iterator adaptor structs with it #15561
Conversation
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 rust-lang#14666.
👍 though it seems a shame that the same message must be repeated for every single iterator adaptor. |
Long term, it may be better to make it specific to each one |
Any chance we could put |
It's not guaranteed that every However, even just implementing it would require somehow searching all the traits that a type implements (theoretically including ones that aren't in scope). I don't actually know if this is possible atm. |
True, not every Regarding the implementation, I'm not particularly familiar with that part of the compiler, so I can only make guesses. I would assume the type info for a type contains a listing of all implemented traits, not merely the ones in scope, but it's certainly possible for that to be wrong. |
AFAIK, there's a mapping trait -> implementors, but not vice versa. |
You could perhaps do a single pass through all traits to collect the ones that have a This assumes, of course, that supporting |
IMO, that can be done later. The |
True. And no, I think if we're just going to start with this approach, you should do all the adaptors. After all, if you only do |
Actually, that suggests that perhaps type parameters should be inspected for Of course, this too involves looking up attributes on more types, and I wonder how expensive this ends up being. Although it shouldn't be hard to create a cache so you don't have to look up a type twice if it does end up being significant. |
Out of curiosity, do you see us expanding the |
@alexcrichton I think it's reasonable to expand it to all |
@alexcrichton I thought about it, but think it's more obvious that |
@huonw What about Although I think potential third-party adaptors are the most compelling reason to add |
I'm going to r+ due to its containment to the |
Quite reasonable. I'm trying to promote the idea that we should come up with a solution that allows us to annotate |
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.
Add #[must_use] message to Iterator and Future ~~Iterator's message is based on current iterator adaptor's #[must_use] message (added in rust-lang#15561) and https://github.com/rust-lang/rust/pull/56677/files#r241236020~~ Future's message is the same as those used in [futures-rs](https://github.com/rust-lang-nursery/futures-rs/search?q=must_use&unscoped_q=must_use) and [tokio](https://github.com/tokio-rs/tokio/search?q=must_use&unscoped_q=must_use). r? @Centril
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 adaptorsare 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 thereason why.
Fixes #14666.