@@ -189,15 +189,6 @@ impl IteratorFalsePositives {
189
189
}
190
190
}
191
191
192
- #[ derive( Copy , Clone ) ]
193
- struct HasChars ;
194
-
195
- impl HasChars {
196
- fn chars ( self ) -> std:: str:: Chars < ' static > {
197
- "HasChars" . chars ( )
198
- }
199
- }
200
-
201
192
/// Checks implementation of `FILTER_NEXT` lint
202
193
fn filter_next ( ) {
203
194
let v = vec ! [ 3 , 2 , 1 , 0 , -1 , -2 , -3 ] ;
@@ -358,232 +349,8 @@ fn iter_skip_next() {
358
349
let _ = foo. filter ( ) . skip ( 42 ) . next ( ) ;
359
350
}
360
351
361
- struct GetFalsePositive {
362
- arr : [ u32 ; 3 ] ,
363
- }
364
-
365
- impl GetFalsePositive {
366
- fn get ( & self , pos : usize ) -> Option < & u32 > { self . arr . get ( pos) }
367
- fn get_mut ( & mut self , pos : usize ) -> Option < & mut u32 > { self . arr . get_mut ( pos) }
368
- }
369
-
370
- /// Checks implementation of `GET_UNWRAP` lint
371
- fn get_unwrap ( ) {
372
- let mut boxed_slice: Box < [ u8 ] > = Box :: new ( [ 0 , 1 , 2 , 3 ] ) ;
373
- let mut some_slice = & mut [ 0 , 1 , 2 , 3 ] ;
374
- let mut some_vec = vec ! [ 0 , 1 , 2 , 3 ] ;
375
- let mut some_vecdeque: VecDeque < _ > = some_vec. iter ( ) . cloned ( ) . collect ( ) ;
376
- let mut some_hashmap: HashMap < u8 , char > = HashMap :: from_iter ( vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) ] ) ;
377
- let mut some_btreemap: BTreeMap < u8 , char > = BTreeMap :: from_iter ( vec ! [ ( 1 , 'a' ) , ( 2 , 'b' ) ] ) ;
378
- let mut false_positive = GetFalsePositive { arr : [ 0 , 1 , 2 ] } ;
379
-
380
- { // Test `get().unwrap()`
381
- let _ = boxed_slice. get ( 1 ) . unwrap ( ) ;
382
- let _ = some_slice. get ( 0 ) . unwrap ( ) ;
383
- let _ = some_vec. get ( 0 ) . unwrap ( ) ;
384
- let _ = some_vecdeque. get ( 0 ) . unwrap ( ) ;
385
- let _ = some_hashmap. get ( & 1 ) . unwrap ( ) ;
386
- let _ = some_btreemap. get ( & 1 ) . unwrap ( ) ;
387
- let _ = false_positive. get ( 0 ) . unwrap ( ) ;
388
- }
389
-
390
- { // Test `get_mut().unwrap()`
391
- * boxed_slice. get_mut ( 0 ) . unwrap ( ) = 1 ;
392
- * some_slice. get_mut ( 0 ) . unwrap ( ) = 1 ;
393
- * some_vec. get_mut ( 0 ) . unwrap ( ) = 1 ;
394
- * some_vecdeque. get_mut ( 0 ) . unwrap ( ) = 1 ;
395
- // Check false positives
396
- * some_hashmap. get_mut ( & 1 ) . unwrap ( ) = 'b' ;
397
- * some_btreemap. get_mut ( & 1 ) . unwrap ( ) = 'b' ;
398
- * false_positive. get_mut ( 0 ) . unwrap ( ) = 1 ;
399
- }
400
- }
401
-
402
-
403
352
#[ allow( similar_names) ]
404
353
fn main ( ) {
405
- use std:: io;
406
-
407
354
let opt = Some ( 0 ) ;
408
355
let _ = opt. unwrap ( ) ;
409
-
410
- let res: Result < i32 , ( ) > = Ok ( 0 ) ;
411
- let _ = res. unwrap ( ) ;
412
-
413
- res. ok ( ) . expect ( "disaster!" ) ;
414
- // the following should not warn, since `expect` isn't implemented unless
415
- // the error type implements `Debug`
416
- let res2: Result < i32 , MyError > = Ok ( 0 ) ;
417
- res2. ok ( ) . expect ( "oh noes!" ) ;
418
- let res3: Result < u32 , MyErrorWithParam < u8 > > = Ok ( 0 ) ;
419
- res3. ok ( ) . expect ( "whoof" ) ;
420
- let res4: Result < u32 , io:: Error > = Ok ( 0 ) ;
421
- res4. ok ( ) . expect ( "argh" ) ;
422
- let res5: io:: Result < u32 > = Ok ( 0 ) ;
423
- res5. ok ( ) . expect ( "oops" ) ;
424
- let res6: Result < u32 , & str > = Ok ( 0 ) ;
425
- res6. ok ( ) . expect ( "meh" ) ;
426
- }
427
-
428
- struct MyError ( ( ) ) ; // doesn't implement Debug
429
-
430
- #[ derive( Debug ) ]
431
- struct MyErrorWithParam < T > {
432
- x : T
433
- }
434
-
435
- #[ allow( unnecessary_operation) ]
436
- fn starts_with ( ) {
437
- "" . chars ( ) . next ( ) == Some ( ' ' ) ;
438
- Some ( ' ' ) != "" . chars ( ) . next ( ) ;
439
- }
440
-
441
- fn str_extend_chars ( ) {
442
- let abc = "abc" ;
443
- let def = String :: from ( "def" ) ;
444
- let mut s = String :: new ( ) ;
445
-
446
- s. push_str ( abc) ;
447
- s. extend ( abc. chars ( ) ) ;
448
-
449
- s. push_str ( "abc" ) ;
450
- s. extend ( "abc" . chars ( ) ) ;
451
-
452
- s. push_str ( & def) ;
453
- s. extend ( def. chars ( ) ) ;
454
-
455
- s. extend ( abc. chars ( ) . skip ( 1 ) ) ;
456
- s. extend ( "abc" . chars ( ) . skip ( 1 ) ) ;
457
- s. extend ( [ 'a' , 'b' , 'c' ] . iter ( ) ) ;
458
-
459
- let f = HasChars ;
460
- s. extend ( f. chars ( ) ) ;
461
- }
462
-
463
- fn clone_on_copy ( ) {
464
- 42 . clone ( ) ;
465
-
466
- vec ! [ 1 ] . clone ( ) ; // ok, not a Copy type
467
- Some ( vec ! [ 1 ] ) . clone ( ) ; // ok, not a Copy type
468
- ( & 42 ) . clone ( ) ;
469
- }
470
-
471
- fn clone_on_ref_ptr ( ) {
472
- let rc = Rc :: new ( true ) ;
473
- let arc = Arc :: new ( true ) ;
474
-
475
- let rcweak = Rc :: downgrade ( & rc) ;
476
- let arc_weak = Arc :: downgrade ( & arc) ;
477
-
478
- rc. clone ( ) ;
479
- Rc :: clone ( & rc) ;
480
-
481
- arc. clone ( ) ;
482
- Arc :: clone ( & arc) ;
483
-
484
- rcweak. clone ( ) ;
485
- rc:: Weak :: clone ( & rcweak) ;
486
-
487
- arc_weak. clone ( ) ;
488
- sync:: Weak :: clone ( & arc_weak) ;
489
-
490
-
491
- }
492
-
493
- fn clone_on_copy_generic < T : Copy > ( t : T ) {
494
- t. clone ( ) ;
495
-
496
- Some ( t) . clone ( ) ;
497
- }
498
-
499
- fn clone_on_double_ref ( ) {
500
- let x = vec ! [ 1 ] ;
501
- let y = & & x;
502
- let z: & Vec < _ > = y. clone ( ) ;
503
-
504
- println ! ( "{:p} {:p}" , * y, z) ;
505
- }
506
-
507
- fn single_char_pattern ( ) {
508
- let x = "foo" ;
509
- x. split ( "x" ) ;
510
- x. split ( "xx" ) ;
511
- x. split ( 'x' ) ;
512
-
513
- let y = "x" ;
514
- x. split ( y) ;
515
- // Not yet testing for multi-byte characters
516
- // Changing `r.len() == 1` to `r.chars().count() == 1` in `lint_single_char_pattern`
517
- // should have done this but produced an ICE
518
- //
519
- // We may not want to suggest changing these anyway
520
- // See: https://github.com/rust-lang-nursery/rust-clippy/issues/650#issuecomment-184328984
521
- x. split ( "ß" ) ;
522
- x. split ( "ℝ" ) ;
523
- x. split ( "💣" ) ;
524
- // Can't use this lint for unicode code points which don't fit in a char
525
- x. split ( "❤️" ) ;
526
- x. contains ( "x" ) ;
527
- x. starts_with ( "x" ) ;
528
- x. ends_with ( "x" ) ;
529
- x. find ( "x" ) ;
530
- x. rfind ( "x" ) ;
531
- x. rsplit ( "x" ) ;
532
- x. split_terminator ( "x" ) ;
533
- x. rsplit_terminator ( "x" ) ;
534
- x. splitn ( 0 , "x" ) ;
535
- x. rsplitn ( 0 , "x" ) ;
536
- x. matches ( "x" ) ;
537
- x. rmatches ( "x" ) ;
538
- x. match_indices ( "x" ) ;
539
- x. rmatch_indices ( "x" ) ;
540
- x. trim_left_matches ( "x" ) ;
541
- x. trim_right_matches ( "x" ) ;
542
-
543
- let h = HashSet :: < String > :: new ( ) ;
544
- h. contains ( "X" ) ; // should not warn
545
- }
546
-
547
- #[ allow( result_unwrap_used) ]
548
- fn temporary_cstring ( ) {
549
- use std:: ffi:: CString ;
550
-
551
- CString :: new ( "foo" ) . unwrap ( ) . as_ptr ( ) ;
552
- }
553
-
554
- fn iter_clone_collect ( ) {
555
- let v = [ 1 , 2 , 3 , 4 , 5 ] ;
556
- let v2 : Vec < isize > = v. iter ( ) . cloned ( ) . collect ( ) ;
557
- let v3 : HashSet < isize > = v. iter ( ) . cloned ( ) . collect ( ) ;
558
- let v4 : VecDeque < isize > = v. iter ( ) . cloned ( ) . collect ( ) ;
559
- }
560
-
561
- fn chars_cmp_with_unwrap ( ) {
562
- let s = String :: from ( "foo" ) ;
563
- if s. chars ( ) . next ( ) . unwrap ( ) == 'f' { // s.starts_with('f')
564
- // Nothing here
565
- }
566
- if s. chars ( ) . next_back ( ) . unwrap ( ) == 'o' { // s.ends_with('o')
567
- // Nothing here
568
- }
569
- if s. chars ( ) . last ( ) . unwrap ( ) == 'o' { // s.ends_with('o')
570
- // Nothing here
571
- }
572
- if s. chars ( ) . next ( ) . unwrap ( ) != 'f' { // !s.starts_with('f')
573
- // Nothing here
574
- }
575
- if s. chars ( ) . next_back ( ) . unwrap ( ) != 'o' { // !s.ends_with('o')
576
- // Nothing here
577
- }
578
- if s. chars ( ) . last ( ) . unwrap ( ) != 'o' { // !s.ends_with('o')
579
- // Nothing here
580
- }
581
- }
582
-
583
- #[ allow( unnecessary_operation) ]
584
- fn ends_with ( ) {
585
- "" . chars ( ) . last ( ) == Some ( ' ' ) ;
586
- Some ( ' ' ) != "" . chars ( ) . last ( ) ;
587
- "" . chars ( ) . next_back ( ) == Some ( ' ' ) ;
588
- Some ( ' ' ) != "" . chars ( ) . next_back ( ) ;
589
356
}
0 commit comments