Skip to content

Commit

Permalink
fix: Avoid reserved function names during sampling
Browse files Browse the repository at this point in the history
  • Loading branch information
uncomputable committed Dec 12, 2024
1 parent ac84719 commit 03c1425
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,37 @@ impl FunctionName {
}

wrapped_string!(FunctionName, "function name");
impl_arbitrary_lowercase_alpha!(FunctionName);

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for FunctionName {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
const RESERVED_NAMES: [&str; 11] = [
"unwrap_left",
"unwrap_right",
"for_while",
"is_none",
"unwrap",
"assert",
"panic",
"match",
"into",
"fold",
"dbg",
];

let len = u.int_in_range(1..=10)?;
let mut string = String::with_capacity(len);
for _ in 0..len {
let offset = u.int_in_range(0..=25)?;
string.push((b'a' + offset) as char)
}
if RESERVED_NAMES.contains(&string.as_str()) {
string.push('_');
}

Ok(Self::from_str_unchecked(string.as_str()))
}
}

/// The identifier of a variable.
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
Expand Down

0 comments on commit 03c1425

Please sign in to comment.