#[allow(dead_code)]
pub(super) mod templates {
use super::{FixTemplate, Placeholder};
#[must_use]
pub(crate) fn to_string_conversion() -> FixTemplate {
FixTemplate::new("$expr.to_string()", "Convert &str to String")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.9)
}
#[must_use]
pub(crate) fn as_str_conversion() -> FixTemplate {
FixTemplate::new("$expr.as_str()", "Convert String to &str")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.85)
}
#[must_use]
pub(crate) fn clone_value() -> FixTemplate {
FixTemplate::new("$expr.clone()", "Clone the value to avoid move")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0382")
.with_confidence(0.8)
}
#[must_use]
pub(crate) fn add_reference() -> FixTemplate {
FixTemplate::new("&$expr", "Add reference")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn add_mut_reference() -> FixTemplate {
FixTemplate::new("&mut $expr", "Add mutable reference")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn dereference() -> FixTemplate {
FixTemplate::new("*$expr", "Dereference")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.6)
}
#[must_use]
pub(crate) fn into_conversion() -> FixTemplate {
FixTemplate::new("$expr.into()", "Use Into trait for conversion")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.75)
}
#[must_use]
pub(crate) fn vec_new() -> FixTemplate {
FixTemplate::new("Vec::new()", "Create empty Vec")
.with_code("E0308")
.with_confidence(0.8)
}
#[must_use]
pub(crate) fn string_new() -> FixTemplate {
FixTemplate::new("String::new()", "Create empty String")
.with_code("E0308")
.with_confidence(0.8)
}
#[must_use]
pub(crate) fn unwrap_or_default() -> FixTemplate {
FixTemplate::new(
"$expr.unwrap_or_default()",
"Unwrap Option with default value",
)
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0308")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn borrow_instead_of_move() -> FixTemplate {
FixTemplate::new("&$expr", "Borrow instead of moving the value")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0382")
.with_confidence(0.85)
}
#[must_use]
pub(crate) fn rc_wrap() -> FixTemplate {
FixTemplate::new("Rc::new($expr)", "Wrap in Rc for shared ownership")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0382")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn arc_wrap() -> FixTemplate {
FixTemplate::new(
"Arc::new($expr)",
"Wrap in Arc for thread-safe shared ownership",
)
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0382")
.with_confidence(0.65)
}
#[must_use]
pub(crate) fn derive_debug() -> FixTemplate {
FixTemplate::new("#[derive(Debug)]", "Add Debug derive to type")
.with_code("E0277")
.with_confidence(0.9)
}
#[must_use]
pub(crate) fn derive_clone_trait() -> FixTemplate {
FixTemplate::new("#[derive(Clone)]", "Add Clone derive to type")
.with_code("E0277")
.with_confidence(0.85)
}
#[must_use]
pub(crate) fn impl_display() -> FixTemplate {
FixTemplate::new(
"impl std::fmt::Display for $type { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, \"{}\", self.$field) } }",
"Implement Display trait"
)
.with_placeholder(Placeholder::type_name("type"))
.with_placeholder(Placeholder::identifier("field"))
.with_code("E0277")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn impl_from() -> FixTemplate {
FixTemplate::new(
"impl From<$source> for $target { fn from(value: $source) -> Self { Self($value) } }",
"Implement From trait for type conversion",
)
.with_placeholder(Placeholder::type_name("source"))
.with_placeholder(Placeholder::type_name("target"))
.with_placeholder(Placeholder::expression("value"))
.with_code("E0277")
.with_confidence(0.65)
}
#[must_use]
pub(crate) fn return_owned() -> FixTemplate {
FixTemplate::new("$expr", "Return owned value instead of reference")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0515")
.with_confidence(0.8)
}
#[must_use]
pub(crate) fn return_cloned() -> FixTemplate {
FixTemplate::new("$expr.clone()", "Return a clone instead of reference")
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0515")
.with_confidence(0.75)
}
#[must_use]
pub(crate) fn use_cow() -> FixTemplate {
FixTemplate::new(
"Cow::Owned($expr)",
"Use Cow for efficient owned/borrowed return",
)
.with_placeholder(Placeholder::expression("expr"))
.with_code("E0515")
.with_confidence(0.7)
}
#[must_use]
pub(crate) fn all_templates() -> Vec<FixTemplate> {
vec![
to_string_conversion(),
as_str_conversion(),
add_reference(),
add_mut_reference(),
dereference(),
into_conversion(),
vec_new(),
string_new(),
unwrap_or_default(),
clone_value(),
borrow_instead_of_move(),
rc_wrap(),
arc_wrap(),
derive_debug(),
derive_clone_trait(),
impl_display(),
impl_from(),
return_owned(),
return_cloned(),
use_cow(),
]
}
}
#[cfg(test)]
mod tests;