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