<commit_before>use ::front::ast;

pub fn check_target(functions: &ast::Functions, fun: &ast::Node<ast::Function>) {
    fun.node.body.visit_expr(&mut |expr| {
        match expr.node {
            ast::Expression::Call { ref function, .. } => assert!(functions.contains_key(function)),
            _ => (),
        };
        true
    });
}<commit_msg>Front: Add CallsUnknownFunction error<commit_after>use std::error::Error;
use std::fmt;
use std::result;

use ::front::ast;

pub type Result<'a> = result::Result<(), CallsUnknownFunction<'a>>;

pub fn check_target<'a>(functions: &'a ast::Functions,
                        fun: &'a ast::Node<ast::Function>)
                        -> Result<'a> {
    let mut res = Ok(());

    fun.node.body.visit_expr(&mut |expr| {
        match expr.node {
            ast::Expression::Call { ref function, .. } if !functions.contains_key(function) => {
                res = Err(CallsUnknownFunction { call: expr });
                false
            }
            _ => true,
        }
    });

    res
}

#[derive(PartialEq, Debug)]
pub struct CallsUnknownFunction<'a> {
    call: &'a ast::Node<ast::Expression>,
}

impl<'a> fmt::Display for CallsUnknownFunction<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self.call.node {
            ast::Expression::Call { ref function, .. } => {
                write!(f,
                       "{}.{} CallsUnknownFunction: `{}`",
                       self.call.pos.line,
                       self.call.pos.col,
                       function)
            }
            _ => panic!("expression not an Expression::Call"),
        }
    }
}

impl<'a> Error for CallsUnknownFunction<'a> {
    fn description(&self) -> &str {
        "call to unknown function"
    }
}
{'fraction_non_alphanumeric': 0.160964666292765, 'fraction_numerical': 0.0, 'mean_word_length': 2.023728813559322, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1339096', 'n_tokens_mistral': 528, 'n_tokens_neox': 527, 'n_words': 105}