Skip to content

Commit 31fd282

Browse files
committed
[get_first]: lint on non-primitive types
1 parent b00236d commit 31fd282

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

clippy_lints/src/methods/get_first.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::is_slice_of_primitives;
32
use clippy_utils::source::snippet_with_applicability;
43
use if_chain::if_chain;
54
use rustc_ast::LitKind;
@@ -20,7 +19,6 @@ pub(super) fn check<'tcx>(
2019
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id);
2120
if let Some(impl_id) = cx.tcx.impl_of_method(method_id);
2221
if cx.tcx.type_of(impl_id).instantiate_identity().is_slice();
23-
if let Some(_) = is_slice_of_primitives(cx, recv);
2422
if let hir::ExprKind::Lit(Spanned { node: LitKind::Int(0, _), .. }) = arg.kind;
2523
then {
2624
let mut app = Applicability::MachineApplicable;

tests/ui/get_first.fixed

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ impl Bar {
1414

1515
fn main() {
1616
let x = vec![2, 3, 5];
17-
let _ = x.first(); // Use x.first()
17+
let _ = x.first();
18+
//~^ ERROR: accessing first element with `x.get(0)`
1819
let _ = x.get(1);
1920
let _ = x[0];
2021

2122
let y = [2, 3, 5];
22-
let _ = y.first(); // Use y.first()
23+
let _ = y.first();
24+
//~^ ERROR: accessing first element with `y.get(0)`
2325
let _ = y.get(1);
2426
let _ = y[0];
2527

2628
let z = &[2, 3, 5];
27-
let _ = z.first(); // Use z.first()
29+
let _ = z.first();
30+
//~^ ERROR: accessing first element with `z.get(0)`
2831
let _ = z.get(1);
2932
let _ = z[0];
3033

@@ -37,4 +40,8 @@ fn main() {
3740

3841
let bar = Bar { arr: [0, 1, 2] };
3942
let _ = bar.get(0); // Do not lint, because Bar is struct.
43+
44+
let non_primitives = [vec![1, 2], vec![3, 4]];
45+
let _ = non_primitives.first();
46+
//~^ ERROR: accessing first element with `non_primitives.get(0)`
4047
}

tests/ui/get_first.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@ impl Bar {
1414

1515
fn main() {
1616
let x = vec![2, 3, 5];
17-
let _ = x.get(0); // Use x.first()
17+
let _ = x.get(0);
18+
//~^ ERROR: accessing first element with `x.get(0)`
1819
let _ = x.get(1);
1920
let _ = x[0];
2021

2122
let y = [2, 3, 5];
22-
let _ = y.get(0); // Use y.first()
23+
let _ = y.get(0);
24+
//~^ ERROR: accessing first element with `y.get(0)`
2325
let _ = y.get(1);
2426
let _ = y[0];
2527

2628
let z = &[2, 3, 5];
27-
let _ = z.get(0); // Use z.first()
29+
let _ = z.get(0);
30+
//~^ ERROR: accessing first element with `z.get(0)`
2831
let _ = z.get(1);
2932
let _ = z[0];
3033

@@ -37,4 +40,8 @@ fn main() {
3740

3841
let bar = Bar { arr: [0, 1, 2] };
3942
let _ = bar.get(0); // Do not lint, because Bar is struct.
43+
44+
let non_primitives = [vec![1, 2], vec![3, 4]];
45+
let _ = non_primitives.get(0);
46+
//~^ ERROR: accessing first element with `non_primitives.get(0)`
4047
}

tests/ui/get_first.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
error: accessing first element with `x.get(0)`
22
--> $DIR/get_first.rs:17:13
33
|
4-
LL | let _ = x.get(0); // Use x.first()
4+
LL | let _ = x.get(0);
55
| ^^^^^^^^ help: try: `x.first()`
66
|
77
= note: `-D clippy::get-first` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::get_first)]`
99

1010
error: accessing first element with `y.get(0)`
11-
--> $DIR/get_first.rs:22:13
11+
--> $DIR/get_first.rs:23:13
1212
|
13-
LL | let _ = y.get(0); // Use y.first()
13+
LL | let _ = y.get(0);
1414
| ^^^^^^^^ help: try: `y.first()`
1515

1616
error: accessing first element with `z.get(0)`
17-
--> $DIR/get_first.rs:27:13
17+
--> $DIR/get_first.rs:29:13
1818
|
19-
LL | let _ = z.get(0); // Use z.first()
19+
LL | let _ = z.get(0);
2020
| ^^^^^^^^ help: try: `z.first()`
2121

22-
error: aborting due to 3 previous errors
22+
error: accessing first element with `non_primitives.get(0)`
23+
--> $DIR/get_first.rs:45:13
24+
|
25+
LL | let _ = non_primitives.get(0);
26+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `non_primitives.first()`
27+
28+
error: aborting due to 4 previous errors
2329

0 commit comments

Comments
 (0)