Auto merge of #11609 - y21:get_first_non_primitives, r=giraffate
[`get_first`]: lint on non-primitive slices Fixes #11594 I left the issue open for a couple days before making the PR to see if anyone has something to say, but it looks like there aren't any objections to removing this check that prevented linting on non-primitive slices, so here's the PR now. There's a couple of instances in clippy itself where we now emit the lint. The actual relevant change is in the first commit and fixing the `.get(0)` instances in clippy itself is in the 2nd commit. changelog: [`get_first`]: lint on non-primitive slices
This commit is contained in:
commit
ef95be517c
14 changed files with 45 additions and 27 deletions
|
|
@ -14,17 +14,20 @@ impl Bar {
|
|||
|
||||
fn main() {
|
||||
let x = vec![2, 3, 5];
|
||||
let _ = x.first(); // Use x.first()
|
||||
let _ = x.first();
|
||||
//~^ ERROR: accessing first element with `x.get(0)`
|
||||
let _ = x.get(1);
|
||||
let _ = x[0];
|
||||
|
||||
let y = [2, 3, 5];
|
||||
let _ = y.first(); // Use y.first()
|
||||
let _ = y.first();
|
||||
//~^ ERROR: accessing first element with `y.get(0)`
|
||||
let _ = y.get(1);
|
||||
let _ = y[0];
|
||||
|
||||
let z = &[2, 3, 5];
|
||||
let _ = z.first(); // Use z.first()
|
||||
let _ = z.first();
|
||||
//~^ ERROR: accessing first element with `z.get(0)`
|
||||
let _ = z.get(1);
|
||||
let _ = z[0];
|
||||
|
||||
|
|
@ -37,4 +40,8 @@ fn main() {
|
|||
|
||||
let bar = Bar { arr: [0, 1, 2] };
|
||||
let _ = bar.get(0); // Do not lint, because Bar is struct.
|
||||
|
||||
let non_primitives = [vec![1, 2], vec![3, 4]];
|
||||
let _ = non_primitives.first();
|
||||
//~^ ERROR: accessing first element with `non_primitives.get(0)`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,17 +14,20 @@ impl Bar {
|
|||
|
||||
fn main() {
|
||||
let x = vec![2, 3, 5];
|
||||
let _ = x.get(0); // Use x.first()
|
||||
let _ = x.get(0);
|
||||
//~^ ERROR: accessing first element with `x.get(0)`
|
||||
let _ = x.get(1);
|
||||
let _ = x[0];
|
||||
|
||||
let y = [2, 3, 5];
|
||||
let _ = y.get(0); // Use y.first()
|
||||
let _ = y.get(0);
|
||||
//~^ ERROR: accessing first element with `y.get(0)`
|
||||
let _ = y.get(1);
|
||||
let _ = y[0];
|
||||
|
||||
let z = &[2, 3, 5];
|
||||
let _ = z.get(0); // Use z.first()
|
||||
let _ = z.get(0);
|
||||
//~^ ERROR: accessing first element with `z.get(0)`
|
||||
let _ = z.get(1);
|
||||
let _ = z[0];
|
||||
|
||||
|
|
@ -37,4 +40,8 @@ fn main() {
|
|||
|
||||
let bar = Bar { arr: [0, 1, 2] };
|
||||
let _ = bar.get(0); // Do not lint, because Bar is struct.
|
||||
|
||||
let non_primitives = [vec![1, 2], vec![3, 4]];
|
||||
let _ = non_primitives.get(0);
|
||||
//~^ ERROR: accessing first element with `non_primitives.get(0)`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,29 @@
|
|||
error: accessing first element with `x.get(0)`
|
||||
--> $DIR/get_first.rs:17:13
|
||||
|
|
||||
LL | let _ = x.get(0); // Use x.first()
|
||||
LL | let _ = x.get(0);
|
||||
| ^^^^^^^^ help: try: `x.first()`
|
||||
|
|
||||
= note: `-D clippy::get-first` implied by `-D warnings`
|
||||
= help: to override `-D warnings` add `#[allow(clippy::get_first)]`
|
||||
|
||||
error: accessing first element with `y.get(0)`
|
||||
--> $DIR/get_first.rs:22:13
|
||||
--> $DIR/get_first.rs:23:13
|
||||
|
|
||||
LL | let _ = y.get(0); // Use y.first()
|
||||
LL | let _ = y.get(0);
|
||||
| ^^^^^^^^ help: try: `y.first()`
|
||||
|
||||
error: accessing first element with `z.get(0)`
|
||||
--> $DIR/get_first.rs:27:13
|
||||
--> $DIR/get_first.rs:29:13
|
||||
|
|
||||
LL | let _ = z.get(0); // Use z.first()
|
||||
LL | let _ = z.get(0);
|
||||
| ^^^^^^^^ help: try: `z.first()`
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
error: accessing first element with `non_primitives.get(0)`
|
||||
--> $DIR/get_first.rs:45:13
|
||||
|
|
||||
LL | let _ = non_primitives.get(0);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `non_primitives.first()`
|
||||
|
||||
error: aborting due to 4 previous errors
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue