#![cfg_attr(test, allow(clippy::expect_used, clippy::unwrap_used))]
use crate::path::SimplePath;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Expr {
Call { callee: SimplePath },
Path(SimplePath),
Literal(String),
}
#[must_use]
pub fn def_id_of_expr_callee(expr: &Expr) -> Option<&SimplePath> {
match expr {
Expr::Call { callee } => Some(callee),
_ => None,
}
}
#[must_use]
pub fn is_path_to<'a, I>(path: &SimplePath, candidate: I) -> bool
where
I: IntoIterator<Item = &'a str>,
{
path.matches(candidate)
}
#[must_use]
pub fn recv_is_option_or_result(path: &SimplePath) -> bool {
matches!(path.last(), Some("Option" | "Result"))
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[rstest]
fn callee_extraction() {
let expr = Expr::Call {
callee: SimplePath::from("std::mem::drop"),
};
assert!(def_id_of_expr_callee(&expr).is_some());
}
#[rstest]
fn recognizes_option_like_receivers() {
let option_path = SimplePath::from("std::option::Option");
let result_path = SimplePath::from("Result");
let custom_path = SimplePath::from("crate::Thing");
assert!(recv_is_option_or_result(&option_path));
assert!(recv_is_option_or_result(&result_path));
assert!(!recv_is_option_or_result(&custom_path));
}
}