rapx/analysis/opt/data_collection/initialization/
local_set.rs1use annotate_snippets::{Level, Renderer, Snippet};
2
3use once_cell::sync::OnceCell;
4
5use rustc_hir::def_id::DefId;
6use rustc_middle::mir::Local;
7use rustc_middle::ty::TyCtxt;
8use rustc_span::Span;
9
10use crate::analysis::core::dataflow::graph::{Graph, NodeOp};
11use crate::analysis::opt::OptCheck;
12use crate::analysis::utils::def_path::DefPath;
13
14use crate::utils::log::{
15 relative_pos_range, span_to_filename, span_to_line_number, span_to_source_code,
16};
17
18struct DefPaths {
19 hashset_new: DefPath,
20 hashset_with_capacity: DefPath,
21 hashmap_new: DefPath,
22 hashmap_with_capacity: DefPath,
23 btreeset_new: DefPath,
24 btreemap_new: DefPath,
25}
26
27static DEFPATHS: OnceCell<DefPaths> = OnceCell::new();
28
29impl DefPaths {
30 pub fn new(tcx: &TyCtxt<'_>) -> Self {
31 Self {
32 hashset_new: DefPath::new("std::collections::HashSet::new", tcx),
33 hashset_with_capacity: DefPath::new("std::collections::HashSet::with_capacity", tcx),
34 hashmap_new: DefPath::new("std::collections::HashMap::new", tcx),
35 hashmap_with_capacity: DefPath::new("std::collections::HashMap::with_capacity", tcx),
36 btreeset_new: DefPath::new("std::collections::BTreeSet::new", tcx),
37 btreemap_new: DefPath::new("std::collections::BTreeMap::new", tcx),
38 }
39 }
40
41 fn has_id(&self, id: DefId) -> bool {
42 id == self.hashset_new.last_def_id()
43 || id == self.hashmap_new.last_def_id()
44 || id == self.btreemap_new.last_def_id()
45 || id == self.btreeset_new.last_def_id()
46 || id == self.hashmap_with_capacity.last_def_id()
47 || id == self.hashset_with_capacity.last_def_id()
48 }
49}
50
51pub struct LocalSetCheck {
52 record: Vec<Span>,
53}
54
55impl OptCheck for LocalSetCheck {
56 fn new() -> Self {
57 Self { record: Vec::new() }
58 }
59
60 fn check(&mut self, graph: &Graph, tcx: &TyCtxt) {
61 let def_paths = &DEFPATHS.get_or_init(|| DefPaths::new(tcx));
62 for (node_idx, node) in graph.nodes.iter_enumerated() {
63 for op in node.ops.iter() {
64 if let NodeOp::Call(def_id) = op {
65 if def_paths.has_id(*def_id)
66 && !graph.is_connected(Local::from_usize(0), node_idx)
67 {
68 self.record.push(node.span);
69 }
70 }
71 }
72 }
73 }
74
75 fn report(&self, graph: &Graph) {
76 for span in self.record.iter() {
77 report_local_set(graph, *span);
78 }
79 }
80
81 fn cnt(&self) -> usize {
82 self.record.len()
83 }
84}
85
86fn report_local_set(graph: &Graph, span: Span) {
87 let code_source = span_to_source_code(graph.span);
88 let filename = span_to_filename(span);
89 let snippet = Snippet::source(&code_source)
90 .line_start(span_to_line_number(graph.span))
91 .origin(&filename)
92 .fold(true)
93 .annotation(
94 Level::Error
95 .span(relative_pos_range(graph.span, span))
96 .label("Initialization happens here"),
97 );
98 let message = Level::Warning
99 .title("Unnecessary data collection initialization detected")
100 .snippet(snippet)
101 .footer(
102 Level::Help.title("Move it into parameter list and use hash table to save allocation."),
103 );
104 let renderer = Renderer::styled();
105 println!("{}", renderer.render(message));
106}