rem_repairer/
repair_rustfix.rs

1use crate::common::{repair_iteration, RepairResult, RepairSystem};
2
3use rem_utils::compile_file;
4use std::collections::HashSet;
5use std::fs;
6
7#[derive(Debug, Clone)]
8pub struct Repairer {}
9
10impl RepairSystem for Repairer {
11    fn name(&self) -> &str {
12        "_rustfix_repairer"
13    }
14
15    fn repair_project(
16        &self,
17        _src_path: &str,
18        _manifest_path: &str,
19        _fn_name: &str,
20    ) -> RepairResult {
21        RepairResult {
22            success: false,
23            repair_count: 0,
24            has_non_elidible_lifetime: false,
25            has_struct_lt: false,
26        }
27    }
28
29    fn repair_file(&self, file_name: &str, new_file_name: &str) -> RepairResult {
30        fs::copy(file_name, &new_file_name).unwrap();
31        let args = vec!["--error-format=json"];
32
33        let mut compile_cmd = compile_file(&new_file_name, &args);
34
35        let process_errors = |stderr: &str| {
36            let suggestions = rustfix::get_suggestions_from_json(
37                stderr,
38                &HashSet::new(),
39                rustfix::Filter::Everything,
40            )
41            .expect("rustfix failed to run on error json");
42
43            if suggestions.len() == 0 {
44                return false;
45            }
46
47            let code: String = fs::read_to_string(&new_file_name).unwrap().parse().unwrap();
48            let fixed = rustfix::apply_suggestions(&code, &suggestions)
49                .expect("could not apply suggestions");
50            fs::write(new_file_name.to_string(), fixed).unwrap();
51            true
52        };
53
54        repair_iteration(&mut compile_cmd, &process_errors, true, None)
55    }
56
57    fn repair_function(&self, file_name: &str, new_file_name: &str, _: &str) -> RepairResult {
58        self.repair_file(file_name, new_file_name)
59    }
60
61    fn clone_box(&self) -> Box<dyn RepairSystem> {
62        Box::new((*self).clone())
63    }
64}