-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
clippy::iter_without_into_iter
suggestion does not compile
#11692
Comments
in some non-toy examples in real code, it appears the suggested impl block doesn't define any generics i.e. both types and lifetimes. |
Additionally, I had this lint trigger on a type in a crate that is not publicly reachable. In that case, the suggestion, which appears to be focused on guiding towards idiomatic public APIs that implement ubiquitous traits from |
Yeah, the suggestion currently relies on the user adjusting the code a bit, and the applicability is correctly set to I will also say though that I was fully aware that the suggestion needs adjustments by the user (lifetimes, for example), and that's not really a problem imo so long as the applicability is correct and rustfix doesn't break your code but, again, a suggestion that compiles is always better, so an issue seems justified |
thanks @y21. I don't use rust-analyzer, rustfix, or Looking at the thanks again for taking a look 🙇 |
Looked into- and thought about this for a bit. I think it'd be quite tedious to make this a machine-applicable suggestion that compiles in any case, since our suggestions (except for a few) are built by crafting strings together. We can take snippets of source code from nodes in the HIR (and we're already doing this) and that usually works fine, but in this case it seems very error prone for a few reasons (though you've already mentioned a few of them):
I'm not certain that this covers everything, so I'd be more comfortable in just leaving the applicability where it is and continue relying on the user fixing these kinds of errors themselves. I thought about taking a snippet of the relevant places from the
I assume you are saying that the type is not publicly reachable (and not that you have some kind of internal crate that isn't reachable from the outside, in which case I would say that it's fine to just |
[`iter_without_into_iter`]: fix papercuts in suggestion and restrict linting to exported types See #11692 for more context. tldr: the lint `iter_without_into_iter` has suggestions that don't compile, which imo isn't that problematic because it does have the appropriate `Applicability` that tells external tools that it shouldn't be auto-applied. However there were some obvious "errors" in the suggestion that really should've been included in my initial PR adding the lint, which is fixed by this PR: - `IntoIterator::into_iter` needs a `self` argument. - `IntoIterator::Iter` associated type doesn't exist. This should've just been `Item`. This still doesn't make it machine applicable, and the remaining things are imho quite non-trivial to implement, as I've explained in #11692 (comment). I personally think it's fine to leave it there and let the user change the remaining errors when copy-pasting the suggestion (e.g. errors caused by lifetimes that were permitted in fn return-position but are not in associated types). This is how many of our other lint suggestions already work. Also, we now restrict linting to only exported types. This required moving basically all of the tests around since they were previously in the `main` function. Same for `into_iter_without_iter`. The git diff is a bit useless here... changelog: [`iter_without_into_iter`]: fix papercuts in suggestion and restrict linting to exported types (cc `@lopopolo,` figured I should mention you since you created the issue)
@y21 thanks for your work on this. I wanted to followup that this lint helped me find a spot where I had |
All of these existing `IntoIterator` impls did not include the generic parameter for the `BuildHasher`, meaning the impl was overly (and unnecessarily) restrictive. Found by `clippy::iter_without_into_iter`. See rust-lang/rust-clippy#11692 (comment).
Summary
The suggested implementation of
IntoIterator
does not compile.Reproducer
I tried this code:
I expected to see this happen: clippy emits a
clippy::iter_without_into_iter
and gives a suggested fix that compiles.Instead, this happened:
Adding the suggestion results in this code:
And these compilation errors:
The suggested code is:
type Item
memberself
parameter infn into_iter
Iter
associated type that is not present on theIntoIterator
traitItem
associated typeVersion
Additional Labels
@rustbot label +I-suggestion-causes-error
The text was updated successfully, but these errors were encountered: