Skip to content

Commit 1e4f5ae

Browse files
committed
Refactor mutant name computation into constructor
1 parent ae324ef commit 1e4f5ae

File tree

2 files changed

+52
-30
lines changed

2 files changed

+52
-30
lines changed

src/mutant.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ pub struct Function {
109109
}
110110

111111
impl Mutant {
112+
/// Construct a mutant discovered while walking source code.
113+
///
114+
/// This initializes all fields and precomputes the human-readable `name`
115+
/// (including file path, line/column, and change description) for use in
116+
/// CLI output, filtering, and JSON.
117+
pub(crate) fn new_discovered(
118+
source_file: SourceFile,
119+
function: Option<Arc<Function>>,
120+
span: Span,
121+
short_replaced: Option<String>,
122+
replacement: String,
123+
genre: Genre,
124+
target: Option<MutationTarget>,
125+
) -> Self {
126+
let mut mutant = Mutant {
127+
name: String::new(),
128+
source_file,
129+
function,
130+
span,
131+
short_replaced,
132+
replacement,
133+
genre,
134+
target,
135+
};
136+
mutant.name = mutant.name(true);
137+
mutant
138+
}
139+
112140
/// Return text of the whole file with the mutation applied.
113141
pub fn mutated_code(&self) -> String {
114142
self.span.replace(

src/visit.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ pub fn walk_tree(
7575
files.append(&mut package_files);
7676
}
7777

78-
for mutant in &mut mutants {
79-
mutant.name = mutant.name(true);
80-
}
81-
8278
progress.finish();
8379
Ok(Discovered { mutants, files })
8480
}
@@ -322,16 +318,16 @@ impl DiscoveryVisitor<'_> {
322318

323319
/// Record that we generated some mutants.
324320
fn collect_mutant(&mut self, span: Span, replacement: &TokenStream, genre: Genre) {
325-
self.mutants.push(Mutant {
326-
name: String::new(),
327-
source_file: self.source_file.clone(),
328-
function: self.fn_stack.last().cloned(),
321+
let mutant = Mutant::new_discovered(
322+
self.source_file.clone(),
323+
self.fn_stack.last().cloned(),
329324
span,
330-
short_replaced: None,
331-
replacement: replacement.to_pretty_string(),
325+
None,
326+
replacement.to_pretty_string(),
332327
genre,
333-
target: None,
334-
});
328+
None,
329+
);
330+
self.mutants.push(mutant);
335331
}
336332

337333
fn collect_fn_mutants(&mut self, sig: &Signature, block: &Block) {
@@ -661,16 +657,15 @@ impl<'ast> Visit<'ast> for DiscoveryVisitor<'_> {
661657
continue;
662658
}
663659
let short_replaced = Some(arm.pat.to_pretty_string());
664-
let mutant = Mutant {
665-
name: String::new(),
666-
source_file: self.source_file.clone(),
667-
function: self.fn_stack.last().cloned(),
668-
span: arm.span().into(),
660+
let mutant = Mutant::new_discovered(
661+
self.source_file.clone(),
662+
self.fn_stack.last().cloned(),
663+
arm.span().into(),
669664
short_replaced,
670-
replacement: String::new(),
671-
genre: Genre::MatchArm,
672-
target: None,
673-
};
665+
String::new(),
666+
Genre::MatchArm,
667+
None,
668+
);
674669
self.mutants.push(mutant);
675670
}
676671
} else {
@@ -728,19 +723,18 @@ impl<'ast> Visit<'ast> for DiscoveryVisitor<'_> {
728723
// No comma, just the field
729724
field.span().into()
730725
};
731-
let mutant = Mutant {
732-
name: String::new(),
733-
source_file: self.source_file.clone(),
734-
function: self.fn_stack.last().cloned(),
726+
let mutant = Mutant::new_discovered(
727+
self.source_file.clone(),
728+
self.fn_stack.last().cloned(),
735729
span,
736-
short_replaced: None,
737-
replacement: String::new(),
738-
genre: Genre::StructField,
739-
target: Some(MutationTarget::StructLiteralField {
730+
None,
731+
String::new(),
732+
Genre::StructField,
733+
Some(MutationTarget::StructLiteralField {
740734
field_name: field_name_str,
741735
struct_name: struct_name.clone(),
742736
}),
743-
};
737+
);
744738
self.mutants.push(mutant);
745739
}
746740
}

0 commit comments

Comments
 (0)