|
1 | 1 | mod char_indices_as_byte_indices; |
| 2 | +mod const_sized_chunks; |
2 | 3 | mod empty_loop; |
3 | 4 | mod explicit_counter_loop; |
4 | 5 | mod explicit_into_iter_loop; |
@@ -784,6 +785,104 @@ declare_clippy_lint! { |
784 | 785 | "using the character position yielded by `.chars().enumerate()` in a context where a byte index is expected" |
785 | 786 | } |
786 | 787 |
|
| 788 | +declare_clippy_lint! { |
| 789 | + /// ### What it does |
| 790 | + /// Checks for usage of `.chunks_exact()` on slices where the `chunk_size` is a constant and suggests |
| 791 | + /// replacing it with its const generic equivalent, `.array_chunks()`, in a way that the value of the |
| 792 | + /// const parameter `N` can be inferred. |
| 793 | + /// |
| 794 | + /// Specifically, in a for-loop, consuming the iterator over the (non-overlapping) chunks, the lint |
| 795 | + /// suggests destructuring the chunks to allow for `N`, the number of elements in a chunk, to be inferred. |
| 796 | + /// |
| 797 | + /// ### Why is this bad? |
| 798 | + /// When `.chunks_exact()` is used, a bounds check is required before an element can be accessed. |
| 799 | + /// |
| 800 | + /// ### Example |
| 801 | + /// ```no_run |
| 802 | + /// let numbers = Vec::from_iter(0..10); |
| 803 | + /// for chunk in numbers.chunks_exact(2) { |
| 804 | + /// println!("{n} is an odd number", n = chunk[1]); |
| 805 | + /// } |
| 806 | + /// ``` |
| 807 | + /// Use instead: |
| 808 | + /// ```no_run |
| 809 | + /// let numbers = Vec::from_iter(0..10); |
| 810 | + /// for [_, n] in numbers.array_chunks() { |
| 811 | + /// println!("{n} is an odd number"); |
| 812 | + /// } |
| 813 | + /// ``` |
| 814 | + #[clippy::version = "1.89.0"] |
| 815 | + pub CONST_SIZED_CHUNKS_EXACT, |
| 816 | + nursery, |
| 817 | + "calling `.chunks_exact()` with a constant `chunk_size` where `.array_chunks()` could be used instead" |
| 818 | +} |
| 819 | + |
| 820 | +declare_clippy_lint! { |
| 821 | + /// ### What it does |
| 822 | + /// Checks for usage of `.chunks_exact_mut()` on slices where the `chunk_size` is a constant and suggests |
| 823 | + /// replacing it with its const generic equivalent, `.array_chunks_mut()`, in a way that the value of the |
| 824 | + /// const parameter `N` can be inferred. |
| 825 | + /// |
| 826 | + /// Specifically, in a for-loop, consuming the iterator over the (non-overlapping) chunks, the lint |
| 827 | + /// suggests destructuring the chunks to allow for `N`, the number of elements in a chunk, to be inferred. |
| 828 | + /// |
| 829 | + /// ### Why is this bad? |
| 830 | + /// When `.chunks_exact_mut()` is used, a bounds check is required before an element can be accessed. |
| 831 | + /// |
| 832 | + /// ### Example |
| 833 | + /// ```no_run |
| 834 | + /// let mut numbers = Vec::from_iter(0..10); |
| 835 | + /// for chunk in numbers.chunks_exact_mut(2) { |
| 836 | + /// chunk[1] += chunk[0]; |
| 837 | + /// println!("{n} is an odd number", n = chunk[1]); |
| 838 | + /// } |
| 839 | + /// ``` |
| 840 | + /// Use instead: |
| 841 | + /// ```no_run |
| 842 | + /// let mut numbers = Vec::from_iter(0..10); |
| 843 | + /// for [m, n] in numbers.array_chunks_mut() { |
| 844 | + /// *n += *m; |
| 845 | + /// println!("{n} is an odd number"); |
| 846 | + /// } |
| 847 | + /// ``` |
| 848 | + #[clippy::version = "1.89.0"] |
| 849 | + pub CONST_SIZED_CHUNKS_EXACT_MUT, |
| 850 | + nursery, |
| 851 | + "calling `.chunks_exact_mut()` with a constant `chunk_size` where `.array_chunks_mut()` could be used instead" |
| 852 | +} |
| 853 | + |
| 854 | +declare_clippy_lint! { |
| 855 | + /// ### What it does |
| 856 | + /// Checks for usage of `.windows()` on slices where the `size` is a constant and suggests replacing it with |
| 857 | + /// its const generic equivalent, `.array_windows()`, in a way that the value of the const parameter `N` can |
| 858 | + /// be inferred. |
| 859 | + /// |
| 860 | + /// Specifically, in a for-loop, consuming the windowed iterator over the overlapping chunks, the lint |
| 861 | + /// suggests destructuring the chunks to allow for `N`, the number of elements in a chunk, to be inferred. |
| 862 | + /// |
| 863 | + /// ### Why is this bad? |
| 864 | + /// When `.windows()` is used, a bounds check is required before an element can be accessed. |
| 865 | + /// |
| 866 | + /// ### Example |
| 867 | + /// ```no_run |
| 868 | + /// let numbers = Vec::from_iter(0..10); |
| 869 | + /// for chunk in numbers.windows(2) { |
| 870 | + /// println!("{n} is an odd number", n = chunk[0] + chunk[1]); |
| 871 | + /// } |
| 872 | + /// ``` |
| 873 | + /// Use instead: |
| 874 | + /// ```no_run |
| 875 | + /// let numbers = Vec::from_iter(0..10); |
| 876 | + /// for [m, n] in numbers.array_windows() { |
| 877 | + /// println!("{n} is an odd number", n = m + n); |
| 878 | + /// } |
| 879 | + /// ``` |
| 880 | + #[clippy::version = "1.89.0"] |
| 881 | + pub CONST_SIZED_WINDOWS, |
| 882 | + nursery, |
| 883 | + "calling `.windows()` with a constant `size` where `.array_windows()` could be used instead" |
| 884 | +} |
| 885 | + |
787 | 886 | pub struct Loops { |
788 | 887 | msrv: Msrv, |
789 | 888 | enforce_iter_loop_reborrow: bool, |
@@ -822,6 +921,9 @@ impl_lint_pass!(Loops => [ |
822 | 921 | INFINITE_LOOP, |
823 | 922 | MANUAL_SLICE_FILL, |
824 | 923 | CHAR_INDICES_AS_BYTE_INDICES, |
| 924 | + CONST_SIZED_CHUNKS_EXACT, |
| 925 | + CONST_SIZED_CHUNKS_EXACT_MUT, |
| 926 | + CONST_SIZED_WINDOWS, |
825 | 927 | ]); |
826 | 928 |
|
827 | 929 | impl<'tcx> LateLintPass<'tcx> for Loops { |
@@ -906,6 +1008,7 @@ impl Loops { |
906 | 1008 | manual_find::check(cx, pat, arg, body, span, expr); |
907 | 1009 | unused_enumerate_index::check(cx, pat, arg, body); |
908 | 1010 | char_indices_as_byte_indices::check(cx, pat, arg, body); |
| 1011 | + const_sized_chunks::check(cx, pat, arg); |
909 | 1012 | } |
910 | 1013 |
|
911 | 1014 | fn check_for_loop_arg(&self, cx: &LateContext<'_>, _: &Pat<'_>, arg: &Expr<'_>) { |
|
0 commit comments