rubyfast 1.3.2

An ultra-fast Ruby performance linter rewritten in Rust — detects 19 common anti-patterns
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
use crate::ast_helpers::*;
use crate::fix::Fix;
use crate::offense::{Offense, OffenseKind};

/// Scan a method call (CallNode) that does NOT have a block.
pub fn scan_call(call: &ruby_prism::CallNode<'_>) -> Vec<Offense> {
    let mut offenses = Vec::new();

    check_shuffle_first(call, &mut offenses);
    check_reverse_each(call, &mut offenses);
    check_keys_each(call, &mut offenses);
    check_each_with_index(call, &mut offenses);
    check_include_vs_cover(call, &mut offenses);
    check_gsub_vs_tr(call, &mut offenses);
    check_fetch_with_argument(call, &mut offenses);
    check_hash_merge_bang(call, &mut offenses);
    check_map_flatten(call, &mut offenses);
    check_select_first(call, &mut offenses);
    check_select_last(call, &mut offenses);
    check_module_eval_call(call, &mut offenses);

    offenses
}

/// Scan a CallNode that has a BlockNode (method call + block).
pub fn scan_call_with_block(
    call: &ruby_prism::CallNode<'_>,
    block: &ruby_prism::BlockNode<'_>,
) -> Vec<Offense> {
    let mut offenses = Vec::new();

    // Checks that only apply when a block is present
    check_sort_vs_sort_by(call, &mut offenses);
    check_module_eval_call(call, &mut offenses);
    check_block_vs_symbol_to_proc(call, block, &mut offenses);

    // Chain checks on the call inside the block
    check_shuffle_first(call, &mut offenses);
    check_reverse_each(call, &mut offenses);
    check_keys_each(call, &mut offenses);
    check_each_with_index(call, &mut offenses);
    check_include_vs_cover(call, &mut offenses);
    check_gsub_vs_tr(call, &mut offenses);
    // NOTE: check_fetch_with_argument excluded — if fetch already has a block, rule doesn't apply.
    check_hash_merge_bang(call, &mut offenses);

    offenses
}

/// Scan a CallNode whose receiver is another CallNode that has a block.
/// This handles chains like `.select { }.first` where .first's receiver is a call-with-block.
pub fn scan_call_on_block_call(
    outer: &ruby_prism::CallNode<'_>,
    recv_call: &ruby_prism::CallNode<'_>,
) -> Vec<Offense> {
    let mut offenses = Vec::new();

    let outer_name = outer.name().as_slice();
    let recv_name = recv_call.name().as_slice();

    // .select{}.first → .detect{}
    if outer_name == b"first" && recv_name == b"select" && arg_count(outer) == 0 {
        let offense = match (recv_call.message_loc(), outer.call_operator_loc()) {
            (Some(sel_l), Some(dot_l)) => {
                let fix = Fix::two(
                    sel_l.start_offset(),
                    sel_l.end_offset(),
                    "detect",
                    dot_l.start_offset(),
                    outer.location().end_offset(),
                    "",
                );
                Offense::with_fix(
                    OffenseKind::SelectFirstVsDetect,
                    outer.location().start_offset(),
                    fix,
                )
            }
            _ => Offense::new(
                OffenseKind::SelectFirstVsDetect,
                outer.location().start_offset(),
            ),
        };
        offenses.push(offense);
    }

    // .select{}.last (no auto-fix)
    if outer_name == b"last" && recv_name == b"select" && arg_count(outer) == 0 {
        offenses.push(Offense::new(
            OffenseKind::SelectLastVsReverseDetect,
            outer.location().start_offset(),
        ));
    }

    // .map{}.flatten(1) → .flat_map{}
    if outer_name == b"flatten"
        && recv_name == b"map"
        && let Some(arg) = first_call_arg(outer)
        && arg_count(outer) == 1
        && is_int_one(&arg)
    {
        let offense = match (recv_call.message_loc(), outer.call_operator_loc()) {
            (Some(sel_l), Some(dot_l)) => {
                let fix = Fix::two(
                    sel_l.start_offset(),
                    sel_l.end_offset(),
                    "flat_map",
                    dot_l.start_offset(),
                    outer.location().end_offset(),
                    "",
                );
                Offense::with_fix(
                    OffenseKind::MapFlattenVsFlatMap,
                    outer.location().start_offset(),
                    fix,
                )
            }
            _ => Offense::new(
                OffenseKind::MapFlattenVsFlatMap,
                outer.location().start_offset(),
            ),
        };
        offenses.push(offense);
    }

    offenses
}

// --- Individual offense checks ---

/// `.shuffle.first` → `.sample`
fn check_shuffle_first(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"first"
        || !receiver_is_call_with_name(&call.receiver(), b"shuffle")
    {
        return;
    }
    let offense = match receiver_as_call(&call.receiver()).and_then(|rs| rs.call_operator_loc()) {
        Some(dot_l) => {
            let fix = Fix::single(
                dot_l.start_offset(),
                call.location().end_offset(),
                ".sample",
            );
            Offense::with_fix(
                OffenseKind::ShuffleFirstVsSample,
                call.location().start_offset(),
                fix,
            )
        }
        None => Offense::new(
            OffenseKind::ShuffleFirstVsSample,
            call.location().start_offset(),
        ),
    };
    offenses.push(offense);
}

/// `.reverse.each` → `.reverse_each`
fn check_reverse_each(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"each"
        || !receiver_is_call_with_name(&call.receiver(), b"reverse")
    {
        return;
    }
    let offense = match (
        receiver_as_call(&call.receiver()).and_then(|rs| rs.call_operator_loc()),
        call.message_loc(),
    ) {
        (Some(dot_l), Some(sel_l)) => {
            let fix = Fix::single(dot_l.start_offset(), sel_l.end_offset(), ".reverse_each");
            Offense::with_fix(
                OffenseKind::ReverseEachVsReverseEach,
                call.location().start_offset(),
                fix,
            )
        }
        _ => Offense::new(
            OffenseKind::ReverseEachVsReverseEach,
            call.location().start_offset(),
        ),
    };
    offenses.push(offense);
}

/// `.keys.each` → `.each_key` (keys must have 0 args)
fn check_keys_each(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"each" {
        return;
    }
    if let Some(recv_call) = receiver_as_call(&call.receiver())
        && recv_call.name().as_slice() == b"keys"
        && arg_count(&recv_call) == 0
    {
        let offense = match (recv_call.call_operator_loc(), call.message_loc()) {
            (Some(dot_l), Some(sel_l)) => {
                let fix = Fix::single(dot_l.start_offset(), sel_l.end_offset(), ".each_key");
                Offense::with_fix(
                    OffenseKind::KeysEachVsEachKey,
                    call.location().start_offset(),
                    fix,
                )
            }
            _ => Offense::new(
                OffenseKind::KeysEachVsEachKey,
                call.location().start_offset(),
            ),
        };
        offenses.push(offense);
    }
}

/// `.select{}.first` → `.detect{}` (when receiver is a plain call with block_pass, not block)
fn check_select_first(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"first" || arg_count(call) != 0 {
        return;
    }
    if let Some(recv_call) = receiver_as_call(&call.receiver())
        && recv_call.name().as_slice() == b"select"
        && has_block_pass(&recv_call)
    {
        let offense = match (recv_call.message_loc(), call.call_operator_loc()) {
            (Some(sel_l), Some(dot_l)) => {
                let fix = Fix::two(
                    sel_l.start_offset(),
                    sel_l.end_offset(),
                    "detect",
                    dot_l.start_offset(),
                    call.location().end_offset(),
                    "",
                );
                Offense::with_fix(
                    OffenseKind::SelectFirstVsDetect,
                    call.location().start_offset(),
                    fix,
                )
            }
            _ => Offense::new(
                OffenseKind::SelectFirstVsDetect,
                call.location().start_offset(),
            ),
        };
        offenses.push(offense);
    }
}

/// `.select{}.last` → `.reverse.detect{}` (when receiver is a plain call with block_pass)
fn check_select_last(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"last" || arg_count(call) != 0 {
        return;
    }
    if let Some(recv_call) = receiver_as_call(&call.receiver())
        && recv_call.name().as_slice() == b"select"
        && has_block_pass(&recv_call)
    {
        offenses.push(Offense::new(
            OffenseKind::SelectLastVsReverseDetect,
            call.location().start_offset(),
        ));
    }
}

/// `.map{}.flatten(1)` → `.flat_map{}` (when receiver is a plain call with block_pass, not full block)
fn check_map_flatten(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"flatten" {
        return;
    }
    if arg_count(call) != 1 {
        return;
    }
    if !first_call_arg(call).is_some_and(|a| is_int_one(&a)) {
        return;
    }
    // Only match when receiver is map WITHOUT a full block (block_pass is ok).
    // Full block cases are handled by scan_call_on_block_call.
    if let Some(recv_call) = receiver_as_call(&call.receiver())
        && recv_call.name().as_slice() == b"map"
        && !has_full_block(&recv_call)
    {
        offenses.push(Offense::new(
            OffenseKind::MapFlattenVsFlatMap,
            call.location().start_offset(),
        ));
    }
}

/// `.each_with_index` → while loop
fn check_each_with_index(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() == b"each_with_index" {
        offenses.push(Offense::new(
            OffenseKind::EachWithIndexVsWhile,
            call.location().start_offset(),
        ));
    }
}

/// `(1..10).include?` → `.cover?`
fn check_include_vs_cover(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"include?" || !receiver_is_range(&call.receiver()) {
        return;
    }
    let offense = match call.message_loc() {
        Some(sel_l) => {
            let fix = Fix::single(sel_l.start_offset(), sel_l.end_offset(), "cover?");
            Offense::with_fix(
                OffenseKind::IncludeVsCoverOnRange,
                call.location().start_offset(),
                fix,
            )
        }
        None => Offense::new(
            OffenseKind::IncludeVsCoverOnRange,
            call.location().start_offset(),
        ),
    };
    offenses.push(offense);
}

/// `.gsub("x", "y")` → `.tr("x", "y")` when both args are single-char strings
fn check_gsub_vs_tr(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"gsub" {
        return;
    }
    let Some((first, second)) = call_args_pair(call) else {
        return;
    };
    if is_single_char_string(&first) && is_single_char_string(&second) {
        let offense = match call.message_loc() {
            Some(sel_l) => {
                let fix = Fix::single(sel_l.start_offset(), sel_l.end_offset(), "tr");
                Offense::with_fix(OffenseKind::GsubVsTr, call.location().start_offset(), fix)
            }
            None => Offense::new(OffenseKind::GsubVsTr, call.location().start_offset()),
        };
        offenses.push(offense);
    }
}

/// `.sort { |a, b| ... }` → `.sort_by` (only fires when sort has a block)
fn check_sort_vs_sort_by(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() == b"sort" {
        offenses.push(Offense::new(
            OffenseKind::SortVsSortBy,
            call.location().start_offset(),
        ));
    }
}

/// `.fetch(k, v)` → `.fetch(k) { v }`
fn check_fetch_with_argument(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() == b"fetch" && arg_count(call) == 2 && !has_block_pass(call) {
        offenses.push(Offense::new(
            OffenseKind::FetchWithArgumentVsBlock,
            call.location().start_offset(),
        ));
    }
}

/// `.merge!({k: v})` → `h[k] = v` (single pair hash argument)
fn check_hash_merge_bang(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"merge!" {
        return;
    }
    if arg_count(call) != 1 {
        return;
    }
    if first_arg_is_single_pair_hash(call) {
        offenses.push(Offense::new(
            OffenseKind::HashMergeBangVsHashBrackets,
            call.location().start_offset(),
        ));
    }
}

/// `.module_eval("def ...")` → `define_method`
fn check_module_eval_call(call: &ruby_prism::CallNode<'_>, offenses: &mut Vec<Offense>) {
    if call.name().as_slice() != b"module_eval" {
        return;
    }
    if let Some(first_arg) = first_call_arg(call)
        && str_contains_def(&first_arg)
    {
        offenses.push(Offense::new(
            OffenseKind::ModuleEval,
            call.location().start_offset(),
        ));
    }
}

/// `.map { |x| x.foo }` → `.map(&:foo)`
fn check_block_vs_symbol_to_proc(
    call: &ruby_prism::CallNode<'_>,
    block: &ruby_prism::BlockNode<'_>,
    offenses: &mut Vec<Offense>,
) {
    // Outer method call must have 0 arguments
    if arg_count(call) != 0 {
        return;
    }

    // Block must have exactly 1 argument
    let arg_names = block_arg_names(&block.parameters());
    if arg_names.len() != 1 {
        return;
    }
    let block_arg_name = &arg_names[0];

    // Block body must be a single CallNode
    let body = match block.body() {
        Some(node) => node,
        None => return,
    };

    // If body is a StatementsNode with a single statement, unwrap it
    let inner_node = if let Some(stmts) = body.as_statements_node() {
        let body_nodes: Vec<_> = stmts.body().iter().collect();
        if body_nodes.len() != 1 {
            return;
        }
        body_nodes.into_iter().next().unwrap()
    } else {
        body
    };

    let inner_call = match inner_node.as_call_node() {
        Some(c) => c,
        None => return,
    };

    // Inner call must have 0 arguments and no block
    if arg_count(&inner_call) != 0 || inner_call.block().is_some() {
        return;
    }

    // Inner call must have a receiver
    let receiver = match inner_call.receiver() {
        Some(r) => r,
        None => return,
    };

    // Receiver must not be a primitive
    if is_primitive(&receiver) {
        return;
    }

    // Receiver must be a LocalVariableReadNode matching the block argument name
    if let Some(lv) = receiver.as_local_variable_read_node()
        && String::from_utf8_lossy(lv.name().as_slice()) == *block_arg_name
    {
        offenses.push(Offense::new(
            OffenseKind::BlockVsSymbolToProc,
            call.location().start_offset(),
        ));
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast_helpers::test_helpers::leak_parse;
    use crate::ast_visitor::for_each_direct_child;
    use ruby_prism::Node;

    fn parse_and_collect(source: &[u8]) -> Vec<Offense> {
        let result = leak_parse(source);
        let mut offenses = Vec::new();
        let root = result.node();
        walk_for_offenses(&root, &mut offenses);
        offenses
    }

    /// Walk AST matching real analyzer behavior.
    fn walk_for_offenses<'pr>(node: &Node<'pr>, offenses: &mut Vec<Offense>) {
        match node {
            Node::CallNode { .. } => {
                let call = node.as_call_node().unwrap();

                // Check receiver-is-block-call chains
                if let Some(recv) = call.receiver() {
                    if let Some(recv_call) = recv.as_call_node() {
                        if let Some(Node::BlockNode { .. }) = recv_call.block() {
                            offenses.extend(scan_call_on_block_call(&call, &recv_call));
                        }
                    }
                }

                match call.block() {
                    Some(Node::BlockNode { .. }) => {
                        let block = call.block().unwrap().as_block_node().unwrap();
                        offenses.extend(scan_call_with_block(&call, &block));
                        // Walk receiver and arguments
                        if let Some(recv) = call.receiver() {
                            walk_for_offenses(&recv, offenses);
                        }
                        if let Some(args) = call.arguments() {
                            for arg in args.arguments().iter() {
                                walk_for_offenses(&arg, offenses);
                            }
                        }
                        // Walk block body
                        if let Some(body) = block.body() {
                            walk_for_offenses(&body, offenses);
                        }
                    }
                    _ => {
                        offenses.extend(scan_call(&call));
                        for_each_direct_child(node, &mut |child| {
                            walk_for_offenses(child, offenses);
                        });
                    }
                }
            }
            _ => {
                for_each_direct_child(node, &mut |child| {
                    walk_for_offenses(child, offenses);
                });
            }
        }
    }

    #[test]
    fn shuffle_first() {
        let o = parse_and_collect(b"[].shuffle.first");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::ShuffleFirstVsSample)
        );
    }

    #[test]
    fn reverse_each() {
        let o = parse_and_collect(b"arr.reverse.each { |x| x }");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::ReverseEachVsReverseEach)
        );
    }

    #[test]
    fn keys_each() {
        let o = parse_and_collect(b"h.keys.each { |k| k }");
        assert!(o.iter().any(|x| x.kind == OffenseKind::KeysEachVsEachKey));
    }

    #[test]
    fn keys_with_arg_each_no_fire() {
        let o = parse_and_collect(b"redis.keys('queue:*').each { |q| q }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::KeysEachVsEachKey));
    }

    #[test]
    fn gsub_single_chars() {
        let o = parse_and_collect(b"s.gsub('r', 'k')");
        assert!(o.iter().any(|x| x.kind == OffenseKind::GsubVsTr));
    }

    #[test]
    fn gsub_multi_char_no_fire() {
        let o = parse_and_collect(b"s.gsub('pet', 'fat')");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::GsubVsTr));
    }

    #[test]
    fn fetch_two_args() {
        let o = parse_and_collect(b"h.fetch(:key, [])");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::FetchWithArgumentVsBlock)
        );
    }

    #[test]
    fn fetch_with_block_no_fire() {
        let o = parse_and_collect(b"Rails.cache.fetch('key', expires_in: 1.hour) { compute }");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::FetchWithArgumentVsBlock)
        );
    }

    #[test]
    fn merge_bang_single_pair() {
        let o = parse_and_collect(b"h.merge!(item: 1)");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::HashMergeBangVsHashBrackets)
        );
    }

    #[test]
    fn merge_bang_explicit_hash() {
        let o = parse_and_collect(b"h.merge!({item: 1})");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::HashMergeBangVsHashBrackets)
        );
    }

    #[test]
    fn merge_bang_two_pairs_no_fire() {
        let o = parse_and_collect(b"h.merge!(a: 1, b: 2)");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::HashMergeBangVsHashBrackets)
        );
    }

    #[test]
    fn each_with_index() {
        let o = parse_and_collect(b"arr.each_with_index { |x, i| x }");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::EachWithIndexVsWhile)
        );
    }

    #[test]
    fn include_on_range() {
        let o = parse_and_collect(b"(1..10).include?(5)");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::IncludeVsCoverOnRange)
        );
    }

    #[test]
    fn sort_with_block() {
        let o = parse_and_collect(b"arr.sort { |a, b| a <=> b }");
        assert!(o.iter().any(|x| x.kind == OffenseKind::SortVsSortBy));
    }

    #[test]
    fn select_first_with_block() {
        let o = parse_and_collect(b"arr.select { |x| x > 1 }.first");
        assert!(o.iter().any(|x| x.kind == OffenseKind::SelectFirstVsDetect));
    }

    #[test]
    fn select_last_with_block() {
        let o = parse_and_collect(b"arr.select { |x| x > 1 }.last");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::SelectLastVsReverseDetect)
        );
    }

    #[test]
    fn map_flatten_one() {
        let o = parse_and_collect(b"arr.map { |e| [e, e] }.flatten(1)");
        assert!(o.iter().any(|x| x.kind == OffenseKind::MapFlattenVsFlatMap));
    }

    #[test]
    fn map_flatten_no_arg_no_fire() {
        let o = parse_and_collect(b"arr.map { |e| [e, e] }.flatten");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::MapFlattenVsFlatMap));
    }

    #[test]
    fn block_vs_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| x.to_s }");
        assert!(o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn block_with_args_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| x.to_s(16) }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn lambda_no_symbol_to_proc() {
        let o = parse_and_collect(b"->(x) { x.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn first_not_on_shuffle_no_fire() {
        let o = parse_and_collect(b"arr.first");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::ShuffleFirstVsSample)
        );
    }

    #[test]
    fn reverse_not_each_no_fire() {
        let o = parse_and_collect(b"arr.reverse.map { |x| x }");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::ReverseEachVsReverseEach)
        );
    }

    #[test]
    fn select_first_with_block_pass() {
        let o = parse_and_collect(b"arr.select(&:odd?).first");
        assert!(o.iter().any(|x| x.kind == OffenseKind::SelectFirstVsDetect));
    }

    #[test]
    fn select_last_with_block_pass() {
        let o = parse_and_collect(b"arr.select(&:odd?).last");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::SelectLastVsReverseDetect)
        );
    }

    #[test]
    fn map_flatten_with_arg_2_no_fire() {
        let o = parse_and_collect(b"arr.map { |e| [e] }.flatten(2)");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::MapFlattenVsFlatMap));
    }

    #[test]
    fn select_first_with_args_no_fire() {
        let o = parse_and_collect(b"arr.select { |x| x > 1 }.first(3)");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::SelectFirstVsDetect));
    }

    #[test]
    fn select_last_with_args_no_fire() {
        let o = parse_and_collect(b"arr.select { |x| x > 1 }.last(3)");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::SelectLastVsReverseDetect)
        );
    }

    #[test]
    fn module_eval_with_def_string() {
        let o = parse_and_collect(b"klass.module_eval(\"def foo; end\")");
        assert!(o.iter().any(|x| x.kind == OffenseKind::ModuleEval));
    }

    #[test]
    fn module_eval_without_def_no_fire() {
        let o = parse_and_collect(b"klass.module_eval(\"puts 1\")");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::ModuleEval));
    }

    #[test]
    fn module_eval_non_string_no_fire() {
        let o = parse_and_collect(b"klass.module_eval(some_var)");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::ModuleEval));
    }

    #[test]
    fn module_eval_with_block() {
        let o = parse_and_collect(b"klass.module_eval { define_method(:foo) {} }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::ModuleEval));
    }

    #[test]
    fn block_multiple_args_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.each_with_object([]) { |x, acc| x.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn block_no_body_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn block_receiver_not_lvar_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| @y.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn block_receiver_is_primitive_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| 42.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn hash_merge_bang_no_args_no_fire() {
        let o = parse_and_collect(b"h.merge!");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::HashMergeBangVsHashBrackets)
        );
    }

    #[test]
    fn gsub_one_arg_no_fire() {
        let o = parse_and_collect(b"s.gsub('x')");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::GsubVsTr));
    }

    #[test]
    fn fetch_one_arg_no_fire() {
        let o = parse_and_collect(b"h.fetch(:key)");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::FetchWithArgumentVsBlock)
        );
    }

    #[test]
    fn include_not_on_range_no_fire() {
        let o = parse_and_collect(b"[1,2,3].include?(5)");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::IncludeVsCoverOnRange)
        );
    }

    #[test]
    fn include_on_exclusive_range() {
        let o = parse_and_collect(b"(1...10).include?(5)");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::IncludeVsCoverOnRange)
        );
    }

    #[test]
    fn include_on_parenthesized_range() {
        let o = parse_and_collect(b"(1..10).include?(5)");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::IncludeVsCoverOnRange)
        );
    }

    #[test]
    fn sort_without_block_no_fire() {
        let o = parse_and_collect(b"arr.sort");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::SortVsSortBy));
    }

    #[test]
    fn block_wrong_lvar_name_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.map { |x| y.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn block_with_args_on_outer_no_symbol_to_proc() {
        let o = parse_and_collect(b"arr.inject(0) { |x| x.to_s }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::BlockVsSymbolToProc));
    }

    #[test]
    fn module_eval_with_heredoc_containing_def() {
        let o = parse_and_collect(b"klass.module_eval(<<~RUBY)\n  def foo\n    42\n  end\nRUBY\n");
        assert!(o.iter().any(|x| x.kind == OffenseKind::ModuleEval));
    }

    #[test]
    fn keys_each_with_keys_having_args_no_fire() {
        let o = parse_and_collect(b"h.keys(\"x\").each { |k| k }");
        assert!(!o.iter().any(|x| x.kind == OffenseKind::KeysEachVsEachKey));
    }

    #[test]
    fn each_with_index_without_block_still_fires() {
        let o = parse_and_collect(b"arr.each_with_index");
        assert!(
            o.iter()
                .any(|x| x.kind == OffenseKind::EachWithIndexVsWhile)
        );
    }

    #[test]
    fn fetch_with_block_pass_no_fire() {
        let o = parse_and_collect(b"h.fetch(:key, &block)");
        assert!(
            !o.iter()
                .any(|x| x.kind == OffenseKind::FetchWithArgumentVsBlock)
        );
    }
}