rapx/analysis/opt/data_collection/suboptimal/
slice_contains.rs1use annotate_snippets::{Level, Renderer, Snippet};
2
3use once_cell::sync::OnceCell;
4
5use crate::{
6 analysis::{core::dataflow::graph::*, opt::OptCheck, utils::def_path::DefPath},
7 utils::log::{relative_pos_range, span_to_filename, span_to_line_number, span_to_source_code},
8};
9use rustc_hir::{intravisit, Expr, ExprKind};
10use rustc_middle::ty::{TyCtxt, TypeckResults};
11use rustc_span::Span;
12
13struct DefPaths {
14 slice_contains: DefPath,
15}
16
17static DEFPATHS: OnceCell<DefPaths> = OnceCell::new();
18
19impl DefPaths {
20 pub fn new(tcx: &TyCtxt<'_>) -> Self {
21 Self {
22 slice_contains: DefPath::new("slice::contains", tcx),
23 }
24 }
25}
26
27struct ContainsFinder<'tcx> {
28 typeck_results: &'tcx TypeckResults<'tcx>,
29 record: Vec<Span>,
30}
31
32impl<'tcx> intravisit::Visitor<'tcx> for ContainsFinder<'tcx> {
33 fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
34 if let ExprKind::MethodCall(.., span) = ex.kind {
35 let def_id = self
36 .typeck_results
37 .type_dependent_def_id(ex.hir_id)
38 .unwrap();
39 let target_def_id = (&DEFPATHS.get().unwrap()).slice_contains.last_def_id();
40 if def_id == target_def_id {
41 self.record.push(span);
42 }
43 }
44 intravisit::walk_expr(self, ex);
45 }
46}
47
48pub struct SliceContainsCheck {
49 record: Vec<Span>,
50}
51
52impl OptCheck for SliceContainsCheck {
53 fn new() -> Self {
54 Self { record: Vec::new() }
55 }
56
57 fn check(&mut self, graph: &Graph, tcx: &TyCtxt) {
58 let _ = &DEFPATHS.get_or_init(|| DefPaths::new(tcx));
59 let def_id = graph.def_id;
60 let body = tcx.hir_body_owned_by(def_id.as_local().unwrap());
61 let typeck_results = tcx.typeck(def_id.as_local().unwrap());
62 let mut contains_finder = ContainsFinder {
63 typeck_results,
64 record: Vec::new(),
65 };
66 intravisit::walk_body(&mut contains_finder, body);
67 self.record = contains_finder.record;
68 }
69
70 fn report(&self, graph: &Graph) {
71 for contains_span in self.record.iter() {
72 report_slice_contains_bug(graph, *contains_span);
73 }
74 }
75
76 fn cnt(&self) -> usize {
77 self.record.len()
78 }
79}
80
81fn report_slice_contains_bug(graph: &Graph, contains_span: Span) {
82 let code_source = span_to_source_code(graph.span);
83 let filename = span_to_filename(graph.span);
84 let snippet = Snippet::source(&code_source)
85 .line_start(span_to_line_number(graph.span))
86 .origin(&filename)
87 .fold(true)
88 .annotation(
89 Level::Error
90 .span(relative_pos_range(graph.span, contains_span))
91 .label("Slice contains happens here."),
92 );
93 let message = Level::Warning
94 .title("Improper data collection detected")
95 .snippet(snippet)
96 .footer(Level::Help.title("Use Set instead of Slice."));
97 let renderer = Renderer::styled();
98 println!("{}", renderer.render(message));
99}