Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-mutants"
version = "27.0.0-pre"
version = "27.0.0"
edition = "2024"
authors = ["Martin Pool"]
license = "MIT"
Expand Down
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# cargo-mutants changelog

## Unreleased
## 27.0.0

Released 2026-03-07.

- Changed: Command line values for `--file`, `--exclude`, `--examine-re`, and `--exclude-re` are now combined with, rather than replacing, values given in the configuration file, consistently with every other option that takes a list. (Use `--config=OTHER` or `--no-config` to avoid using values in the configuration.) Thanks to @sandersaares for pointing this out.

Expand Down
80 changes: 79 additions & 1 deletion src/fnvalue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ fn type_replacements(type_: &Type, error_exprs: &[Expr]) -> impl Iterator<Item =
]
} else if path_is_nonzero_unsigned(path) {
vec![quote! { 1.try_into().unwrap() }]
} else if let Some(inner_type) = match_first_type_arg(path, "NonZero") {
// NonZero<T> generic form (stabilized in Rust 1.79)
if let Type::Path(syn::TypePath {
path: inner_path, ..
}) = inner_type
{
if path_is_unsigned(inner_path) {
vec![quote! { 1.try_into().unwrap() }]
} else if path_is_signed(inner_path) {
vec![
quote! { 1.try_into().unwrap() },
quote! { (-1).try_into().unwrap() },
]
} else {
// Unknown T, assume it could be signed
vec![
quote! { 1.try_into().unwrap() },
quote! { (-1).try_into().unwrap() },
]
}
} else {
// T is not a simple path, assume it could be signed
vec![
quote! { 1.try_into().unwrap() },
quote! { (-1).try_into().unwrap() },
]
}
Comment on lines +72 to +85
Copy link

Copilot AI Mar 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The NonZero<T> branch repeats the same two-value vec![1.try_into()..., (-1).try_into()...] construction in multiple places. Consider extracting this into a small local variable/helper to reduce duplication and make future tweaks (e.g., additional candidate values) less error-prone.

Copilot uses AI. Check for mistakes.
} else if path_is_float(path) {
vec![quote! { 0.0 }, quote! { 1.0 }, quote! { -1.0 }]
} else if path_ends_with(path, "Result") {
Expand Down Expand Up @@ -393,7 +420,6 @@ fn path_is_nonzero_signed(path: &Path) -> bool {
}

fn path_is_nonzero_unsigned(path: &Path) -> bool {
// TODO: Also NonZero<usize> etc.
if let Some(l) = path.segments.last().map(|p| p.ident.to_string()) {
matches!(
l.as_str(),
Expand Down Expand Up @@ -494,6 +520,58 @@ mod test {
);
}

#[test]
fn nonzero_generic_unsigned_replacements() {
check_replacements(
&parse_quote! { -> NonZero<u32> },
&[],
&["1.try_into().unwrap()"],
);

check_replacements(
&parse_quote! { -> NonZero<usize> },
&[],
&["1.try_into().unwrap()"],
);

check_replacements(
&parse_quote! { -> std::num::NonZero<u8> },
&[],
&["1.try_into().unwrap()"],
);
}

#[test]
fn nonzero_generic_signed_replacements() {
check_replacements(
&parse_quote! { -> NonZero<i32> },
&[],
&["1.try_into().unwrap()", "(-1).try_into().unwrap()"],
);

check_replacements(
&parse_quote! { -> NonZero<isize> },
&[],
&["1.try_into().unwrap()", "(-1).try_into().unwrap()"],
);

check_replacements(
&parse_quote! { -> std::num::NonZero<i64> },
&[],
&["1.try_into().unwrap()", "(-1).try_into().unwrap()"],
);
}

#[test]
fn nonzero_generic_unknown_type_replacements() {
// When T is not a recognized integer type, assume it could be signed.
check_replacements(
&parse_quote! { -> NonZero<T> },
&[],
&["1.try_into().unwrap()", "(-1).try_into().unwrap()"],
);
}

#[test]
fn unit_replacement() {
check_replacements(&parse_quote! { -> () }, &[], &["()"]);
Expand Down