]> git.lizzy.rs Git - rust.git/commit
Extend `indexing_slicing` lint
authorShea Newton <shnewto@gmail.com>
Wed, 23 May 2018 04:56:02 +0000 (21:56 -0700)
committerShea Newton <shnewto@gmail.com>
Tue, 19 Jun 2018 16:27:08 +0000 (16:27 +0000)
commit7af0c678559ef75e9ec6359ec3e05d76dfc355f5
treef935f3c4968633636ff74f64258bf3a07115a2ea
parentc573186245a66205959f0075f72f4a4f4821c33e
Extend `indexing_slicing` lint

    Hey there clippy team! I've made some assumptions in this PR and I'm not at all certain they'll look like the right approach to you. I'm looking forward to any feedback or revision requests you have, thanks!

    Prior to this commit the `indexing_slicing` lint was limited to indexing/slicing operations on arrays. This meant that the scope of a really useful lint didn't include vectors. In order to include vectors in the `indexing_slicing` lint a few steps were taken.

    The `array_indexing.rs` source file in `clippy_lints` was renamed to `indexing_slicing.rs` to more accurately reflect the lint's new scope. The `OUT_OF_BOUNDS_INDEXING` lint persists through these changes so if we can know that a constant index or slice on an array is in bounds no lint is triggered.

    The `array_indexing` tests in the `tests/ui` directory were also extended and moved to `indexing_slicing.rs` and `indexing_slicing.stderr`.

    The `indexing_slicing` lint was moved to the `clippy_pedantic` lint group.

    A specific "Consider using" string was added to each of the `indexing_slicing` lint reports.

    At least one of the test scenarios might look peculiar and I'll leave it up to y'all to decide if it's palatable. It's the result of indexing the array `x` after `let x = [1, 2, 3, 4];`

    ```
    error: slicing may panic. Consider using `.get(..n)`or `.get_mut(..n)`instead
      --> $DIR/indexing_slicing.rs:23:6
       |
    23 |     &x[0..][..3];
       |      ^^^^^^^^^^^
    ```

    The error string reports only on the second half's range-to, because the range-from is in bounds!

    Again, thanks for taking a look.

    Closes #2536
clippy_lints/src/array_indexing.rs
clippy_lints/src/lib.rs
tests/ui/array_indexing.rs
tests/ui/array_indexing.stderr