Skip to main content

unused_functions

Function unused_functions 

Source
pub fn unused_functions(cpg: &CodePropertyGraph) -> Vec<&Node>
Expand description

Returns all functions that are never the target of a direct call.

A function is considered unused if no Edge::Call edge points to its node. This is a simple direct‑call analysis; indirect calls through function pointers or references are not detected.

§Examples

use icb_graph::graph::{CodePropertyGraph, Node, Edge};
use icb_graph::query::unused_functions;
use icb_common::NodeKind;

let mut cpg = CodePropertyGraph::new();
let foo = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("foo".into()),
    usr: Some("foo".into()),
    start_line: 1,
    end_line: 1,
});
let bar = cpg.graph.add_node(Node {
    kind: NodeKind::Function,
    name: Some("bar".into()),
    usr: Some("bar".into()),
    start_line: 2,
    end_line: 2,
});
// foo calls bar
cpg.graph.add_edge(foo, bar, Edge::Call);

let unused = unused_functions(&cpg);
// foo is never called directly => unused
assert!(unused.iter().any(|n| n.name.as_deref() == Some("foo")));
assert!(!unused.iter().any(|n| n.name.as_deref() == Some("bar")));