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