Replace match_method_chain() with method_chain_args()

This commit is contained in:
Devon Hollowood
2015-12-27 14:15:09 -08:00
parent f1aac931bd
commit 29b53d600f
2 changed files with 44 additions and 73 deletions

View File

@@ -8,10 +8,13 @@ use rustc::middle::ty;
use std::borrow::Cow;
use syntax::ast::Lit_::*;
use syntax::ast;
use syntax::ptr::P;
use rustc::session::Session;
use std::str::FromStr;
pub type MethodArgs = HirVec<P<Expr>>;
// module DefPaths for certain structs/enums we check for
pub const OPTION_PATH: [&'static str; 3] = ["core", "option", "Option"];
pub const RESULT_PATH: [&'static str; 3] = ["core", "result", "Result"];
@@ -164,24 +167,28 @@ pub fn match_path_ast(path: &ast::Path, segments: &[&str]) -> bool {
|(a, b)| a.identifier.name.as_str() == *b)
}
/// match an Expr against a chain of methods. For example, if `expr` represents the `.baz()` in
/// `foo.bar().baz()`, `matched_method_chain(expr, &["bar", "baz"])` will return true.
pub fn match_method_chain(expr: &Expr, methods: &[&str]) -> bool {
let mut current = &expr.node ;
/// match an Expr against a chain of methods, and return the matched Exprs. For example, if `expr`
/// represents the `.baz()` in `foo.bar().baz()`, `matched_method_chain(expr, &["bar", "baz"])`
/// will return a Vec containing the Exprs for `.bar()` and `.baz()`
pub fn method_chain_args<'a>(expr: &'a Expr, methods: &[&str]) -> Option<Vec<&'a MethodArgs>> {
let mut current = expr;
let mut matched = Vec::with_capacity(methods.len());
for method_name in methods.iter().rev() { // method chains are stored last -> first
if let ExprMethodCall(ref name, _, ref args) = *current {
if let ExprMethodCall(ref name, _, ref args) = current.node {
if name.node.as_str() == *method_name {
current = &args[0].node
matched.push(args); // build up `matched` backwards
current = &args[0] // go to parent expression
}
else {
return false;
return None;
}
}
else {
return false;
return None;
}
}
true
matched.reverse(); // reverse `matched`, so that it is in the same order as `methods`
Some(matched)
}