aboutsummaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/lib.rs69
-rw-r--r--lib/src/lints.rs7
-rw-r--r--lib/src/lints/bool_comparison.rs8
3 files changed, 57 insertions, 27 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index 537f4b3..c06f02d 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -1,10 +1,11 @@
1pub mod lints; 1mod lints;
2pub use lints::LINTS;
2 3
3use rnix::{SyntaxElement, SyntaxKind, TextRange}; 4use rnix::{SyntaxElement, SyntaxKind, TextRange};
4use std::ops::Deref; 5use std::default::Default;
5 6
6pub trait Rule { 7pub trait Rule {
7 fn validate(&self, node: &SyntaxElement) -> Option<Diagnostic>; 8 fn validate(&self, node: &SyntaxElement) -> Option<Report>;
8} 9}
9 10
10#[derive(Debug)] 11#[derive(Debug)]
@@ -19,29 +20,55 @@ impl Diagnostic {
19 } 20 }
20} 21}
21 22
23#[derive(Debug, Default)]
24pub struct Report {
25 pub diagnostics: Vec<Diagnostic>,
26}
27
28impl Report {
29 pub fn new() -> Self {
30 Self::default()
31 }
32 pub fn diagnostic(mut self, at: TextRange, message: String) -> Self {
33 self.diagnostics.push(Diagnostic::new(at, message));
34 self
35 }
36}
37
22pub trait Metadata { 38pub trait Metadata {
23 fn name(&self) -> &str; 39 fn name(&self) -> &str;
24 fn note(&self) -> &str; 40 fn note(&self) -> &str;
25 fn match_with(&self, with: &SyntaxKind) -> bool; 41 fn match_with(&self, with: &SyntaxKind) -> bool;
42 fn match_kind(&self) -> SyntaxKind;
26} 43}
27 44
28pub trait Lint: Metadata + Rule + Send + Sync {} 45pub trait Lint: Metadata + Rule + Send + Sync {}
29 46
30// #[macro_export] 47#[macro_export]
31// macro_rules! lint_map { 48macro_rules! lint_map {
32// ($($s:ident),*,) => { 49 ($($s:ident),*,) => {
33// lint_map($($s),*); 50 lint_map!($($s),*);
34// } 51 };
35// ($($s:ident),*) => { 52 ($($s:ident),*) => {
36// use ::std::collections::HashMap; 53 use ::std::collections::HashMap;
37// use rnix::SyntaxKind; 54 use ::rnix::SyntaxKind;
38// $( 55 $(
39// mod $s; 56 mod $s;
40// )* 57 )*
41// ::lazy_static::lazy_static! { 58 ::lazy_static::lazy_static! {
42// pub static ref RULES: HashMap<SyntaxKind, &'static Box<dyn $crate::Lint>> = { 59 pub static ref LINTS: HashMap<SyntaxKind, Vec<&'static Box<dyn $crate::Lint>>> = {
43// vec![$(&*$s::LINT,)*] 60 let mut map = HashMap::new();
44// } 61 $(
45// } 62 {
46// } 63 let temp_lint = &*$s::LINT;
47// } 64 let temp_match = temp_lint.match_kind();
65 map.entry(temp_match)
66 .and_modify(|v: &mut Vec<_>| v.push(temp_lint))
67 .or_insert_with(|| vec![temp_lint]);
68 }
69 )*
70 map
71 };
72 }
73 }
74}
diff --git a/lib/src/lints.rs b/lib/src/lints.rs
index b0df71b..15d9063 100644
--- a/lib/src/lints.rs
+++ b/lib/src/lints.rs
@@ -1,2 +1,5 @@
1pub mod bool_comparison; 1use crate::lint_map;
2pub mod with_list; 2
3lint_map! {
4 bool_comparison,
5}
diff --git a/lib/src/lints/bool_comparison.rs b/lib/src/lints/bool_comparison.rs
index 4476b31..918126f 100644
--- a/lib/src/lints/bool_comparison.rs
+++ b/lib/src/lints/bool_comparison.rs
@@ -1,4 +1,4 @@
1use crate::{Diagnostic, Lint, Metadata, Rule}; 1use crate::{Lint, Metadata, Report, Rule};
2 2
3use if_chain::if_chain; 3use if_chain::if_chain;
4use macros::lint; 4use macros::lint;
@@ -10,12 +10,12 @@ use rnix::{
10#[lint( 10#[lint(
11 name = "bool_comparison", 11 name = "bool_comparison",
12 note = "Unnecessary comparison with boolean", 12 note = "Unnecessary comparison with boolean",
13 match_with = "SyntaxKind::NODE_BIN_OP" 13 match_with = SyntaxKind::NODE_BIN_OP
14)] 14)]
15struct BoolComparison; 15struct BoolComparison;
16 16
17impl Rule for BoolComparison { 17impl Rule for BoolComparison {
18 fn validate(&self, node: &SyntaxElement) -> Option<Diagnostic> { 18 fn validate(&self, node: &SyntaxElement) -> Option<Report> {
19 if_chain! { 19 if_chain! {
20 if let NodeOrToken::Node(bin_op_node) = node; 20 if let NodeOrToken::Node(bin_op_node) = node;
21 if let Some(bin_expr) = BinOp::cast(bin_op_node.clone()); 21 if let Some(bin_expr) = BinOp::cast(bin_op_node.clone());
@@ -37,7 +37,7 @@ impl Rule for BoolComparison {
37 non_bool_side, 37 non_bool_side,
38 bool_side 38 bool_side
39 ); 39 );
40 dbg!(Some(Diagnostic::new(at, message))) 40 Some(Report::new().diagnostic(at, message))
41 } else { 41 } else {
42 None 42 None
43 } 43 }