File tree Expand file tree Collapse file tree 4 files changed +32
-14
lines changed Expand file tree Collapse file tree 4 files changed +32
-14
lines changed Original file line number Diff line number Diff line change 1
1
use clippy_utils:: diagnostics:: span_lint_and_sugg;
2
- use clippy_utils:: is_slice_of_primitives;
3
2
use clippy_utils:: source:: snippet_with_applicability;
4
3
use if_chain:: if_chain;
5
4
use rustc_ast:: LitKind ;
@@ -20,7 +19,6 @@ pub(super) fn check<'tcx>(
20
19
if let Some ( method_id) = cx. typeck_results( ) . type_dependent_def_id( expr. hir_id) ;
21
20
if let Some ( impl_id) = cx. tcx. impl_of_method( method_id) ;
22
21
if cx. tcx. type_of( impl_id) . instantiate_identity( ) . is_slice( ) ;
23
- if let Some ( _) = is_slice_of_primitives( cx, recv) ;
24
22
if let hir:: ExprKind :: Lit ( Spanned { node: LitKind :: Int ( 0 , _) , .. } ) = arg. kind;
25
23
then {
26
24
let mut app = Applicability :: MachineApplicable ;
Original file line number Diff line number Diff line change @@ -14,17 +14,20 @@ impl Bar {
14
14
15
15
fn main() {
16
16
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)`
18
19
let _ = x.get(1);
19
20
let _ = x[0];
20
21
21
22
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)`
23
25
let _ = y.get(1);
24
26
let _ = y[0];
25
27
26
28
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)`
28
31
let _ = z.get(1);
29
32
let _ = z[0];
30
33
@@ -37,4 +40,8 @@ fn main() {
37
40
38
41
let bar = Bar { arr: [0, 1, 2] };
39
42
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)`
40
47
}
Original file line number Diff line number Diff line change @@ -14,17 +14,20 @@ impl Bar {
14
14
15
15
fn main ( ) {
16
16
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)`
18
19
let _ = x. get ( 1 ) ;
19
20
let _ = x[ 0 ] ;
20
21
21
22
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)`
23
25
let _ = y. get ( 1 ) ;
24
26
let _ = y[ 0 ] ;
25
27
26
28
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)`
28
31
let _ = z. get ( 1 ) ;
29
32
let _ = z[ 0 ] ;
30
33
@@ -37,4 +40,8 @@ fn main() {
37
40
38
41
let bar = Bar { arr : [ 0 , 1 , 2 ] } ;
39
42
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)`
40
47
}
Original file line number Diff line number Diff line change 1
1
error: accessing first element with `x.get(0)`
2
2
--> $DIR/get_first.rs:17:13
3
3
|
4
- LL | let _ = x.get(0); // Use x.first()
4
+ LL | let _ = x.get(0);
5
5
| ^^^^^^^^ help: try: `x.first()`
6
6
|
7
7
= note: `-D clippy::get-first` implied by `-D warnings`
8
8
= help: to override `-D warnings` add `#[allow(clippy::get_first)]`
9
9
10
10
error: accessing first element with `y.get(0)`
11
- --> $DIR/get_first.rs:22 :13
11
+ --> $DIR/get_first.rs:23 :13
12
12
|
13
- LL | let _ = y.get(0); // Use y.first()
13
+ LL | let _ = y.get(0);
14
14
| ^^^^^^^^ help: try: `y.first()`
15
15
16
16
error: accessing first element with `z.get(0)`
17
- --> $DIR/get_first.rs:27 :13
17
+ --> $DIR/get_first.rs:29 :13
18
18
|
19
- LL | let _ = z.get(0); // Use z.first()
19
+ LL | let _ = z.get(0);
20
20
| ^^^^^^^^ help: try: `z.first()`
21
21
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
23
29
You can’t perform that action at this time.
0 commit comments