targeted-mutation 0.3.0

Targeted mutation testing for Rust: find the wrong implementations your tests let through
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
//! Finds direct comparisons and sort keys that can use another same-type value.

use std::collections::BTreeMap;

use syn::spanned::Spanned;
use syn::{BinOp, Expr, FnArg, Ident, Item, ItemFn, Member, Pat, Stmt, Type};

use super::{Candidate, Edit, Scenario};

const MAX_PARAMETERS: usize = 8;
const MAX_FIELDS: usize = 16;
const MAX_CANDIDATES_PER_FUNCTION: usize = 12;

#[derive(Clone, Copy, PartialEq, Eq)]
enum Integer {
    Signed(Option<u32>),
    Unsigned(Option<u32>),
}

impl Integer {
    fn from_type(ty: &Type) -> Option<Self> {
        let name = type_name(ty)?.to_string();
        Some(match name.as_str() {
            "i8" => Self::Signed(Some(8)),
            "i16" => Self::Signed(Some(16)),
            "i32" => Self::Signed(Some(32)),
            "i64" => Self::Signed(Some(64)),
            "i128" => Self::Signed(Some(128)),
            "isize" => Self::Signed(None),
            "u8" => Self::Unsigned(Some(8)),
            "u16" => Self::Unsigned(Some(16)),
            "u32" => Self::Unsigned(Some(32)),
            "u64" => Self::Unsigned(Some(64)),
            "u128" => Self::Unsigned(Some(128)),
            "usize" => Self::Unsigned(None),
            _ => return None,
        })
    }

    // Use the minimum pointer width so the suggested literal fits on all targets.
    fn contains(self, value: i128) -> bool {
        match self {
            Self::Signed(Some(128)) => true,
            Self::Signed(bits) => {
                let bound = 1_i128 << (bits.unwrap_or(16) - 1);
                (-bound..bound).contains(&value)
            }
            Self::Unsigned(Some(128)) => value >= 0,
            Self::Unsigned(bits) => (0..(1_i128 << bits.unwrap_or(16))).contains(&value),
        }
    }
}

struct Record<'a> {
    name: &'a Ident,
    fields: Vec<(&'a Ident, Integer)>,
}

impl Record<'_> {
    fn literal(&self, value: impl Fn(usize) -> i128) -> String {
        let fields = self
            .fields
            .iter()
            .enumerate()
            .map(|(index, (field, _))| format!("{field}: {}", value(index)))
            .collect::<Vec<_>>()
            .join(", ");
        format!("{} {{ {fields} }}", self.name)
    }
}

enum ParameterType<'a> {
    Integer(Integer),
    Record {
        record: &'a Record<'a>,
        borrowed: bool,
    },
}

struct Parameter<'a> {
    name: &'a Ident,
    ty: ParameterType<'a>,
}

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
struct Value {
    parameter: usize,
    field: Option<usize>,
}

enum Operand {
    Place(Value),
    Literal(i128),
}

pub(super) fn discover(source: &str, file: &syn::File, out: &mut Vec<Candidate>) {
    let records: BTreeMap<_, _> = file
        .items
        .iter()
        .filter_map(|item| {
            let Item::Struct(item) = item else {
                return None;
            };
            let syn::Fields::Named(fields) = &item.fields else {
                return None;
            };
            if !item.generics.params.is_empty() || fields.named.len() > MAX_FIELDS {
                return None;
            }
            let fields = fields
                .named
                .iter()
                .map(|field| Some((field.ident.as_ref()?, Integer::from_type(&field.ty)?)))
                .collect::<Option<Vec<_>>>()?;
            Some((
                &item.ident,
                Record {
                    name: &item.ident,
                    fields,
                },
            ))
        })
        .collect();

    for item in &file.items {
        let Item::Fn(function) = item else { continue };
        if !is_direct_function(function) {
            continue;
        }
        let [Stmt::Expr(expression, semicolon)] = &function.block.stmts[..] else {
            continue;
        };
        let limit = out.len().saturating_add(MAX_CANDIDATES_PER_FUNCTION);
        match unparen(expression) {
            Expr::Binary(comparison) if semicolon.is_none() => {
                if let Some(parameters) = parameters(function, &records) {
                    compare_candidates(source, function, comparison, &parameters, out, limit);
                }
            }
            Expr::MethodCall(call) => sort_candidates(source, function, call, &records, out, limit),
            _ => {}
        }
    }
}

fn is_direct_function(function: &ItemFn) -> bool {
    let signature = &function.sig;
    signature.generics.params.is_empty()
        && signature.asyncness.is_none()
        && signature.unsafety.is_none()
        && signature.constness.is_none()
        && signature.abi.is_none()
        && function.attrs.iter().all(|attr| {
            ["doc", "inline", "must_use"]
                .iter()
                .any(|name| attr.path().is_ident(name))
        })
}

fn type_name(ty: &Type) -> Option<&Ident> {
    let Type::Path(path) = ty else { return None };
    if path.qself.is_some() {
        return None;
    }
    path.path.get_ident()
}

fn binding(pattern: &Pat) -> Option<&Ident> {
    let Pat::Ident(binding) = pattern else {
        return None;
    };
    (binding.by_ref.is_none() && binding.subpat.is_none()).then_some(&binding.ident)
}

fn unparen(mut expression: &Expr) -> &Expr {
    loop {
        expression = match expression {
            Expr::Paren(paren) => &paren.expr,
            Expr::Group(group) => &group.expr,
            _ => return expression,
        };
    }
}

fn path_name(expression: &Expr) -> Option<&Ident> {
    let Expr::Path(path) = unparen(expression) else {
        return None;
    };
    if path.qself.is_some() {
        return None;
    }
    path.path.get_ident()
}

fn parameters<'a>(
    function: &'a ItemFn,
    records: &'a BTreeMap<&Ident, Record<'a>>,
) -> Option<Vec<Parameter<'a>>> {
    if function.sig.inputs.len() > MAX_PARAMETERS {
        return None;
    }
    function
        .sig
        .inputs
        .iter()
        .map(|arg| {
            let FnArg::Typed(arg) = arg else { return None };
            let name = binding(&arg.pat)?;
            let ty = if let Some(integer) = Integer::from_type(&arg.ty) {
                ParameterType::Integer(integer)
            } else {
                let (ty, borrowed) = match arg.ty.as_ref() {
                    Type::Reference(reference) if reference.mutability.is_none() => {
                        (reference.elem.as_ref(), true)
                    }
                    ty => (ty, false),
                };
                ParameterType::Record {
                    record: records.get(type_name(ty)?)?,
                    borrowed,
                }
            };
            Some(Parameter { name, ty })
        })
        .collect()
}

fn named_field(expression: &Expr) -> Option<(&Ident, &Ident)> {
    let Expr::Field(field) = unparen(expression) else {
        return None;
    };
    let Member::Named(name) = &field.member else {
        return None;
    };
    Some((path_name(&field.base)?, name))
}

fn place<'a>(expression: &'a Expr, params: &[Parameter<'_>]) -> Option<(Value, &'a Ident)> {
    if let Some(name) = path_name(expression) {
        let parameter = params.iter().position(|parameter| {
            parameter.name == name && matches!(parameter.ty, ParameterType::Integer(_))
        })?;
        return Some((
            Value {
                parameter,
                field: None,
            },
            name,
        ));
    }
    let (base, field_name) = named_field(expression)?;
    let parameter = params.iter().position(|parameter| parameter.name == base)?;
    let ParameterType::Record { record, .. } = params[parameter].ty else {
        return None;
    };
    let field = record
        .fields
        .iter()
        .position(|(name, _)| *name == field_name)?;
    Some((
        Value {
            parameter,
            field: Some(field),
        },
        field_name,
    ))
}

fn integer_literal(expression: &Expr) -> Option<i128> {
    let (expression, negative) = match unparen(expression) {
        Expr::Unary(unary) if matches!(unary.op, syn::UnOp::Neg(_)) => (unparen(&unary.expr), true),
        expression => (expression, false),
    };
    let Expr::Lit(literal) = expression else {
        return None;
    };
    let syn::Lit::Int(integer) = &literal.lit else {
        return None;
    };
    let value = integer.base10_parse::<u128>().ok()?;
    if negative && value == 1_u128 << 127 {
        return Some(i128::MIN);
    }
    let value = i128::try_from(value).ok()?;
    Some(if negative { -value } else { value })
}

fn operand(expression: &Expr, params: &[Parameter<'_>]) -> Option<Operand> {
    if let Some((value, _)) = place(expression, params) {
        return Some(Operand::Place(value));
    }
    integer_literal(expression).map(Operand::Literal)
}

fn integer_type(value: Value, params: &[Parameter<'_>]) -> Integer {
    match &params[value.parameter].ty {
        ParameterType::Integer(integer) => *integer,
        ParameterType::Record { record, .. } => record.fields[value.field.unwrap()].1,
    }
}

fn alternatives(value: Value, params: &[Parameter<'_>]) -> Vec<Value> {
    let integer = integer_type(value, params);
    match &params[value.parameter].ty {
        ParameterType::Integer(_) => params
            .iter()
            .enumerate()
            .filter_map(|(parameter, arg)| {
                matches!(arg.ty, ParameterType::Integer(other) if integer == other).then_some(
                    Value {
                        parameter,
                        field: None,
                    },
                )
            })
            .collect(),
        ParameterType::Record { record, .. } => record
            .fields
            .iter()
            .enumerate()
            .filter_map(|(field, (_, other))| {
                (*other == integer).then_some(Value {
                    parameter: value.parameter,
                    field: Some(field),
                })
            })
            .collect(),
    }
}

fn place_name(value: Value, params: &[Parameter<'_>]) -> String {
    let parameter = &params[value.parameter];
    match &parameter.ty {
        ParameterType::Integer(_) => parameter.name.to_string(),
        ParameterType::Record { record, .. } => {
            format!(
                "{}.{}",
                parameter.name,
                record.fields[value.field.unwrap()].0
            )
        }
    }
}

fn alternative_name(value: Value, params: &[Parameter<'_>]) -> String {
    let parameter = &params[value.parameter];
    match &parameter.ty {
        ParameterType::Integer(_) => parameter.name.to_string(),
        ParameterType::Record { record, .. } => record.fields[value.field.unwrap()].0.to_string(),
    }
}

fn compare(op: &BinOp, lhs: i128, rhs: i128) -> Option<bool> {
    Some(match op {
        BinOp::Eq(_) => lhs == rhs,
        BinOp::Ne(_) => lhs != rhs,
        BinOp::Lt(_) => lhs < rhs,
        BinOp::Le(_) => lhs <= rhs,
        BinOp::Gt(_) => lhs > rhs,
        BinOp::Ge(_) => lhs >= rhs,
        _ => return None,
    })
}

fn across(pivot: i128, primitive: Integer, op: &BinOp, change_lhs: bool) -> Option<(i128, i128)> {
    let values: Vec<_> = [pivot.checked_sub(1), pivot.checked_add(1), Some(pivot)]
        .into_iter()
        .flatten()
        .filter(|&value| primitive.contains(value))
        .collect();
    let evaluate = |value| {
        if change_lhs {
            compare(op, value, pivot)
        } else {
            compare(op, pivot, value)
        }
    };
    for (index, &first) in values.iter().enumerate() {
        for &second in &values[index + 1..] {
            if evaluate(first)? != evaluate(second)? {
                return Some(
                    if matches!(op, BinOp::Eq(_) | BinOp::Ne(_)) && second == pivot {
                        (second, first)
                    } else {
                        (first, second)
                    },
                );
            }
        }
    }
    None
}

fn compare_candidates(
    source: &str,
    function: &ItemFn,
    expression: &syn::ExprBinary,
    params: &[Parameter<'_>],
    out: &mut Vec<Candidate>,
    limit: usize,
) {
    for (change_lhs, changed, other) in [
        (true, expression.left.as_ref(), expression.right.as_ref()),
        (false, expression.right.as_ref(), expression.left.as_ref()),
    ] {
        let Some((chosen, token)) = place(changed, params) else {
            continue;
        };
        let Some(opposite) = operand(other, params) else {
            continue;
        };
        let integer = integer_type(chosen, params);
        let pivot = match opposite {
            Operand::Literal(value) if integer.contains(value) => value,
            Operand::Place(value) if value != chosen && integer_type(value, params) == integer => 1,
            _ => continue,
        };
        let Some((original, alternative)) = across(pivot, integer, &expression.op, change_lhs)
        else {
            continue;
        };
        for new in alternatives(chosen, params) {
            if out.len() == limit {
                return;
            }
            if new == chosen || matches!(opposite, Operand::Place(value) if new == value) {
                continue;
            }
            let mut values = BTreeMap::from([(chosen, original), (new, alternative)]);
            if let Operand::Place(value) = opposite {
                values.insert(value, pivot);
            }
            let args = params
                .iter()
                .enumerate()
                .map(|(parameter, input)| {
                    let value = |field| {
                        values
                            .get(&Value { parameter, field })
                            .copied()
                            .unwrap_or(0)
                    };
                    match &input.ty {
                        ParameterType::Integer(_) => value(None).to_string(),
                        ParameterType::Record { record, borrowed } => {
                            let literal = record.literal(|index| value(Some(index)));
                            if *borrowed {
                                format!("&{literal}")
                            } else {
                                literal
                            }
                        }
                    }
                })
                .collect::<Vec<_>>()
                .join(", ");
            push_candidate(
                source,
                function,
                token,
                expression.span(),
                alternative_name(new, params),
                format!(
                    "replace `{}` with `{}`",
                    place_name(chosen, params),
                    place_name(new, params)
                ),
                Scenario {
                    expression: format!("{}({args})", function.sig.ident),
                    // Arguments of this tool's making: together they may be a case
                    // the function is never given.
                    guidance: Some("Use these arguments if the API accepts them together.".into()),
                    ..Scenario::default()
                },
                out,
            );
        }
    }
}

fn sort_candidates(
    source: &str,
    function: &ItemFn,
    expression: &syn::ExprMethodCall,
    records: &BTreeMap<&Ident, Record<'_>>,
    out: &mut Vec<Candidate>,
    limit: usize,
) {
    if expression.method != "sort_by_key" && expression.method != "sort_unstable_by_key" {
        return;
    }
    let mut args = function.sig.inputs.iter();
    let (Some(FnArg::Typed(arg)), None) = (args.next(), args.next()) else {
        return;
    };
    let Type::Reference(reference) = arg.ty.as_ref() else {
        return;
    };
    let Type::Slice(slice) = reference.elem.as_ref() else {
        return;
    };
    let Some(record) = type_name(&slice.elem).and_then(|name| records.get(name)) else {
        return;
    };
    let mut calls = expression.args.iter();
    let (Some(Expr::Closure(key)), None) = (calls.next(), calls.next()) else {
        return;
    };
    let mut inputs = key.inputs.iter();
    let (Some(param), None) = (inputs.next(), inputs.next()) else {
        return;
    };
    let Some((base, chosen)) = named_field(&key.body) else {
        return;
    };
    if reference.mutability.is_none()
        || expression.turbofish.is_some()
        || path_name(&expression.receiver).is_none()
        || path_name(&expression.receiver) != binding(&arg.pat)
        || Some(base) != binding(param)
    {
        return;
    }
    let Some((index, (_, integer))) = record
        .fields
        .iter()
        .enumerate()
        .find(|(_, (name, _))| *name == chosen)
    else {
        return;
    };
    let function_name = &function.sig.ident;
    let rows = if function_name == "rows" {
        "tmt_rows"
    } else {
        "rows"
    };
    for (alternative, (field, ty)) in record.fields.iter().enumerate() {
        if out.len() == limit {
            break;
        }
        if alternative == index || ty != integer {
            continue;
        }
        let first = record.literal(|i| {
            if i == index {
                10
            } else if i == alternative {
                40
            } else {
                0
            }
        });
        let second = record.literal(|i| {
            if i == index {
                20
            } else if i == alternative {
                30
            } else {
                0
            }
        });
        let setup = format!("let mut {rows} = [{first}, {second}]; {function_name}(&mut {rows});");
        push_candidate(
            source,
            function,
            chosen,
            expression.span(),
            field.to_string(),
            format!("replace sort key `{chosen}` with `{field}`"),
            Scenario {
                setup: setup.clone(),
                expression: format!("[{rows}[0].{chosen}, {rows}[1].{chosen}]"),
                assertion_prefix: setup,
                guidance: Some("Use these rows if the API accepts such values.".into()),
                ..Scenario::default()
            },
            out,
        );
    }
}

#[allow(clippy::too_many_arguments)]
fn push_candidate(
    source: &str,
    function: &ItemFn,
    token: &Ident,
    expression: proc_macro2::Span,
    replacement: String,
    description: String,
    scenario: Scenario,
    out: &mut Vec<Candidate>,
) {
    let span = token.span().byte_range();
    if source.get(span.clone()) != Some(token.to_string().as_str()) {
        return;
    }
    out.push(Candidate {
        edits: vec![Edit { span, replacement }],
        scope: expression.start().line..=expression.end().line,
        signature_lines: vec![function.sig.span().start().line],
        description,
        existing: false,
        scenarios: vec![scenario],
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    fn candidates(source: &str) -> Vec<Candidate> {
        let mut candidates = Vec::new();
        discover(source, &syn::parse_file(source).unwrap(), &mut candidates);
        candidates
    }

    #[test]
    fn arguments_straddle_the_other_operand() {
        let source =
            "fn expiry(created: u64, updated: u64, expires: u64) -> bool { created < expires }";
        let all = candidates(source);
        let candidate = all
            .iter()
            .find(|entry| entry.description == "replace `created` with `updated`")
            .unwrap();
        assert_eq!(&source[candidate.edits[0].span.clone()], "created");
        assert_eq!(candidate.edits[0].replacement, "updated");
        assert_eq!(candidate.scenarios[0].expression, "expiry(0, 2, 1)");
    }

    #[test]
    fn fields_can_straddle_an_argument_or_a_fixed_boundary() {
        let source = r#"
struct Entry { created_at: u64, updated_at: u64 }
fn field_expiry(row: &Entry, expires: u64) -> bool { row.created_at < expires }
fn mature(row: Entry) -> bool { row.created_at >= 5000 }
"#;
        let all = candidates(source);
        let created_to_updated: Vec<_> = all
            .iter()
            .filter(|entry| entry.description == "replace `row.created_at` with `row.updated_at`")
            .collect();
        assert_eq!(created_to_updated.len(), 2);
        assert_eq!(
            &source[created_to_updated[0].edits[0].span.clone()],
            "created_at"
        );
        assert_eq!(
            created_to_updated[0].scenarios[0].expression,
            "field_expiry(&Entry { created_at: 0, updated_at: 2 }, 1)"
        );
        assert_eq!(
            created_to_updated[1].scenarios[0].expression,
            "mature(Entry { created_at: 4999, updated_at: 5001 })"
        );
    }

    #[test]
    fn equality_and_primitive_type_boundaries_are_reachable() {
        let source = r#"
fn unsigned(a: u8, b: u8) -> bool { a == 0 }
fn signed(a: i8, b: i8) -> bool { -128 > a }
fn reversed(a: i16, b: i16) -> bool { -128 >= a }
"#;
        let all = candidates(source);
        let witnesses: Vec<_> = all
            .iter()
            .map(|entry| entry.scenarios[0].expression.as_str())
            .collect();
        assert_eq!(witnesses, ["unsigned(0, 1)", "reversed(-129, -127)"]);
    }

    #[test]
    fn sort_key_alternatives_reverse_two_rows() {
        let source = r#"
struct Entry { created_at: u64, updated_at: u64, id: u8 }
fn sort_entries(rows: &mut [Entry]) { rows.sort_by_key(|row| row.created_at); }
"#;
        let all = candidates(source);
        assert_eq!(all.len(), 1);
        let candidate = &all[0];
        assert_eq!(
            candidate.description,
            "replace sort key `created_at` with `updated_at`"
        );
        assert_eq!(&source[candidate.edits[0].span.clone()], "created_at");
        let observation = &candidate.scenarios[0];
        assert!(
            observation
                .setup
                .contains("Entry { created_at: 10, updated_at: 40, id: 0 }")
        );
        assert!(
            observation
                .setup
                .contains("Entry { created_at: 20, updated_at: 30, id: 0 }")
        );
        assert!(observation.setup.contains("sort_entries(&mut rows);"));
        assert_eq!(observation.assertion_prefix, observation.setup);
        assert_eq!(
            observation.expression,
            "[rows[0].created_at, rows[1].created_at]"
        );
    }

    #[test]
    fn requires_compatible_types_and_direct_syntax() {
        let source = r#"
struct Entry { created_at: u64, updated_at: u32 }
fn mixed(a: u64, alternate: i64, limit: u64) -> bool { a < limit }
fn unrelated(row: &Entry, limit: u64) -> bool { row.created_at < limit }
fn computed(a: u64, alternate: u64, limit: u64) -> bool { a + 1 < limit }
fn hidden(a: u64, alternate: u64, limit: u64) -> bool { let a = limit; a < limit }
fn wrong_key(rows: &mut [Entry]) { rows.sort_by_key(|row| row.created_at + 1); }
"#;
        assert!(candidates(source).is_empty());
    }
}