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 11use clippy_utils:: diagnostics:: span_lint_and_sugg;
2- use clippy_utils:: is_slice_of_primitives;
32use clippy_utils:: source:: snippet_with_applicability;
43use if_chain:: if_chain;
54use 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 ;
Original file line number Diff line number Diff line change @@ -14,17 +14,20 @@ impl Bar {
1414
1515fn 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}
Original file line number Diff line number Diff line change @@ -14,17 +14,20 @@ impl Bar {
1414
1515fn 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}
Original file line number Diff line number Diff line change 11error: 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
1010error: 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
1616error: 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
You can’t perform that action at this time.
0 commit comments