File size: 1,204 Bytes
17f328f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#![feature(rustc_private)]

extern crate rustc_driver;
extern crate rustc_interface;

use std::env;
use std::ffi::OsString;
use std::path::Path;

fn main() {
    let mut callbacks = Callbacks;
    let args = rustc_args(env::args_os().skip(1).collect());
    rustc_driver::run_compiler(&args, &mut callbacks);
}

struct Callbacks;

impl rustc_driver::Callbacks for Callbacks {
    fn config(&mut self, config: &mut rustc_interface::Config) {
        let previous = config.register_lints.take();
        config.register_lints = Some(Box::new(move |sess, lint_store| {
            if let Some(previous) = &previous {
                previous(sess, lint_store);
            }
            argument_comment_lint::register_lints(sess, lint_store);
        }));
    }
}

fn rustc_args(args: Vec<OsString>) -> Vec<String> {
    let mut rustc_args: Vec<String> = args
        .into_iter()
        .map(|arg| arg.to_string_lossy().into_owned())
        .collect();
    if rustc_args.first().is_none_or(|arg| !is_rustc(arg)) {
        rustc_args.insert(0, "rustc".to_string());
    }
    rustc_args
}

fn is_rustc(arg: &str) -> bool {
    Path::new(arg).file_stem().and_then(|stem| stem.to_str()) == Some("rustc")
}