-
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
Lint .into_iter()
if that only forwards to .iter()
#1565
Comments
So to clarify this, would this mean calling |
Yea. Some care needs to be taken to ensure that the for loop desugaring is not linted |
That should be simple, the for loop uses |
rust-lang/rust#49000 also makes it clear that this lint will also help us detect incorrect usages of |
There are actually two ways that
I think we only need to lint cases that would be intercepted by I was investigating how I might implement such a lint in the compiler, but a library-specific lint like this is probably not a great idea to hard-code. Doing it in clippy seems safer, even though it might not get as much reach. For |
I think the best place would be https://github.com/rust-lang-nursery/rust-clippy/blob/c5559c1648b0810e5dff9f94935fba52e9a9e833/clippy_lints/src/methods.rs#L691 which contains all our method call lints |
@cuviper any plans to return to this? rust-lang/rust#49000 is quite useful, and seems blocked on this. |
I do, yes. I managed to prototype the changes already -- I'll try to get back to clean that up and post a PR for comment soon. |
I'm not sure if this has been taken into account by #1565 (comment) — If it is not too difficult to do, I suggest we also lint Complete list
If we disregard the chance of false positive, we could even check:
|
Sorry I haven't found the time for this, but I just rebased my branch and pushed it as-is, in case somebody else wants to take over. I haven't gone through to see if I covered all the cases @kennytm mentioned. I'll try to be responsive about questions of what I was thinking, since I didn't get around to writing many comments either. |
One thing in particular -- I did not cover so many collection types, just arrays and slices. My motivation was primarily linting to avoid collisions with a future |
The generic procedure at the end should be able to cover all collections except |
@kennytm I think your suggestion is different from the Your suggestion is more like a style lint (which is like the lints on for loop), and none of them involves deref coercion or unsizing. The array part, however, involves coercion and thus is harder to implement than the one that doesn't. I guess I'll only implement for the array case for now. |
@ishitatsuyuki "adjusted type" means coercion + auto-reference/deref. Check the compiler docs for "adjustment". |
@kennytm I mean that the motivation/purpose of these two types of lints are different. |
@ishitatsuyuki Yes, and I mean if you need to cover the collection types, it is better to generalize the lint than cover each one individually. |
3344: Added lints `into_iter_on_ref` and `into_iter_on_array`. r=phansch a=kennytm Fixes #1565. `into_iter_on_array` is a correctness lint against `into_iter()` on `[T; n]` and `PathBuf` (please provide a concise noun that covers both types 🙃). `into_iter_on_ref` is a style lint against `into_iter()` on `&C` where `C` is `[T]`, `Vec<T>`, `BTreeSet<T>` etc. Both suggests replacing the `into_iter()` with `iter()` or `iter_mut()`. `into_iter_on_array` is a correctness lint since it is very likely the standard library would provide an `into_iter()` method for `[T; n]` (rust-lang/rust#25725) and existing type inference of `[a, b, c].into_iter()` will be broken. `PathBuf` is also placed under this lint since `PathBuf::into_iter` currently doesn't exist and it makes some sense to implement `IntoIterator` on it yielding `OsString`s. Co-authored-by: kennytm <kennytm@gmail.com>
Should fix clippy toolstat. Changes: ```` rustup rust-lang/rust#55330 Update stderr Rename test files Also lint cfg_attr(.., rustfmt::skip) Add tests from rustfmt::skip test file Run update_lints.py script Add test for non-crate-level inner attributes Differ between inner and outer attributes Add tests Add cfg_attr(rustfmt) lint Addressed comments. Fix dogfood error. Added lints `into_iter_on_ref` and `into_iter_on_array`. Fix rust-lang#1565. Allow single_match_else Update stderr Add copyright statement© Fix typos Fix dogfood error Fix typo and indentation run update_lints script Add tests for unknwon_clippy_lints lint Add new lint: unknwon_clippy_lintsg clippy: fix pedantic warnings and run clippy::pedantic lints on the codebase. Fix a false-positive of needless_borrow UI test cleanup: Extract match_overlapping_arm tests UI test cleanup: Extract expect_fun_call tests Add missing code of conduct file Use slice patterns instead of padding Fix dogfood and pedantic lints ci: when installing rust-toolchain-installer-master, install it in debug mode to save some time in ci. RIIR update lints: Generate deprecated lints Replace big if/else expression with match ````
cc @bluss
Types like
[T; N]
don't implementIntoIter
themselves, but instead provide it through theirDeref<Target=[T]>
impl, which is simply a wrapper around calling.iter()
..into_iter()
suggests that some ownership transfer is going on, if that is not the case.iter()
should be used.The text was updated successfully, but these errors were encountered: