forked from trailofbits/dylint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
93 lines (80 loc) · 2.35 KB
/
lib.rs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#![feature(iter_collect_into)]
#![feature(let_chains)]
#![feature(lint_reasons)]
#![feature(non_exhaustive_omitted_patterns_lint)]
#![feature(once_cell_try)]
#![feature(rustc_private)]
#![warn(unused_extern_crates)]
extern crate rustc_ast;
extern crate rustc_errors;
extern crate rustc_hash;
extern crate rustc_hir;
extern crate rustc_lint_defs;
extern crate rustc_middle;
extern crate rustc_span;
extern crate rustc_target;
use marker_adapter::{Adapter, LintCrateInfo};
use rustc_lint::{LateContext, LateLintPass};
use serde::Deserialize;
use std::path::PathBuf;
mod context;
#[allow(dead_code)]
mod conversion;
#[allow(dead_code)]
mod lint_pass;
dylint_linting::impl_late_lint! {
/// ### What it does
/// Runs Marker lints from a Dylint library.
///
/// ### Configuration
/// - `lint_crates`: A list of [`marker_adapter::LintCrateInfo`]. Each is a struct containing
/// two fields, `name` and `path`, which are documented as follows:
/// - `name`: The name of the lint crate
/// - `path`: The absolute path of the compiled dynamic library, which can be loaded as a lint
/// crate
///
/// [`marker_adapter::LintCrateInfo`]: https://docs.rs/marker_adapter/latest/marker_adapter/struct.LintCrateInfo.html
pub MARKER,
Warn,
"Marker lints run from a Dylint library",
Marker::new()
}
#[derive(Clone, Deserialize)]
struct DeserializableLintCrateInfo {
pub name: String,
pub path: PathBuf,
}
impl From<DeserializableLintCrateInfo> for LintCrateInfo {
fn from(value: DeserializableLintCrateInfo) -> Self {
let DeserializableLintCrateInfo { name, path } = value;
Self { name, path }
}
}
#[derive(Default, Deserialize)]
struct Config {
lint_crates: Vec<DeserializableLintCrateInfo>,
}
struct Marker {
config: Config,
}
impl Marker {
pub fn new() -> Self {
Self {
config: dylint_linting::config_or_default(env!("CARGO_PKG_NAME")),
}
}
fn lint_crates(&self) -> Vec<LintCrateInfo> {
self.config
.lint_crates
.clone()
.into_iter()
.map(Into::into)
.collect()
}
}
impl<'tcx> LateLintPass<'tcx> for Marker {
fn check_crate(&mut self, cx: &LateContext<'tcx>) {
let adapter = Adapter::new(&self.lint_crates()).unwrap();
lint_pass::process_crate(cx, &adapter);
}
}