diff options
author | Akshay <[email protected]> | 2021-09-21 12:11:24 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2021-09-21 12:11:24 +0100 |
commit | 40867728c023a9f95f346671b51be68002cc7552 (patch) | |
tree | e856327b0d42d9a4740c460a5efb633f8a73f3d9 /lib/src/lib.rs | |
parent | c91c2ae1398e4f567ee5e4c50a1da35a9b65919b (diff) |
add suggestion to bool_comparison
Diffstat (limited to 'lib/src/lib.rs')
-rw-r--r-- | lib/src/lib.rs | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs index c06f02d..0f92d0d 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs | |||
@@ -1,8 +1,10 @@ | |||
1 | mod lints; | 1 | mod lints; |
2 | mod make; | ||
3 | |||
2 | pub use lints::LINTS; | 4 | pub use lints::LINTS; |
3 | 5 | ||
4 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; | 6 | use rnix::{SyntaxElement, SyntaxKind, TextRange}; |
5 | use std::default::Default; | 7 | use std::{default::Default, convert::Into}; |
6 | 8 | ||
7 | pub trait Rule { | 9 | pub trait Rule { |
8 | fn validate(&self, node: &SyntaxElement) -> Option<Report>; | 10 | fn validate(&self, node: &SyntaxElement) -> Option<Report>; |
@@ -12,32 +14,60 @@ pub trait Rule { | |||
12 | pub struct Diagnostic { | 14 | pub struct Diagnostic { |
13 | pub at: TextRange, | 15 | pub at: TextRange, |
14 | pub message: String, | 16 | pub message: String, |
17 | pub suggestion: Option<Suggestion>, | ||
15 | } | 18 | } |
16 | 19 | ||
17 | impl Diagnostic { | 20 | impl Diagnostic { |
18 | pub fn new(at: TextRange, message: String) -> Self { | 21 | pub fn new(at: TextRange, message: String) -> Self { |
19 | Self { at, message } | 22 | Self { at, message, suggestion: None } |
23 | } | ||
24 | pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self { | ||
25 | Self { at, message, suggestion: Some(suggestion) } | ||
26 | } | ||
27 | } | ||
28 | |||
29 | #[derive(Debug)] | ||
30 | pub struct Suggestion { | ||
31 | pub at: TextRange, | ||
32 | pub fix: SyntaxElement, | ||
33 | } | ||
34 | |||
35 | impl Suggestion { | ||
36 | pub fn new<E: Into<SyntaxElement>>(at: TextRange, fix: E) -> Self { | ||
37 | Self { | ||
38 | at, | ||
39 | fix: fix.into() | ||
40 | } | ||
20 | } | 41 | } |
21 | } | 42 | } |
22 | 43 | ||
23 | #[derive(Debug, Default)] | 44 | #[derive(Debug, Default)] |
24 | pub struct Report { | 45 | pub struct Report { |
25 | pub diagnostics: Vec<Diagnostic>, | 46 | pub diagnostics: Vec<Diagnostic>, |
47 | pub note: &'static str | ||
26 | } | 48 | } |
27 | 49 | ||
28 | impl Report { | 50 | impl Report { |
29 | pub fn new() -> Self { | 51 | pub fn new(note: &'static str) -> Self { |
30 | Self::default() | 52 | Self { |
53 | note, | ||
54 | ..Default::default() | ||
55 | } | ||
31 | } | 56 | } |
32 | pub fn diagnostic(mut self, at: TextRange, message: String) -> Self { | 57 | pub fn diagnostic(mut self, at: TextRange, message: String) -> Self { |
33 | self.diagnostics.push(Diagnostic::new(at, message)); | 58 | self.diagnostics.push(Diagnostic::new(at, message)); |
34 | self | 59 | self |
35 | } | 60 | } |
61 | pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self { | ||
62 | self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); | ||
63 | self | ||
64 | } | ||
65 | |||
36 | } | 66 | } |
37 | 67 | ||
38 | pub trait Metadata { | 68 | pub trait Metadata { |
39 | fn name(&self) -> &str; | 69 | fn name() -> &'static str where Self: Sized; |
40 | fn note(&self) -> &str; | 70 | fn note() -> &'static str where Self: Sized; |
41 | fn match_with(&self, with: &SyntaxKind) -> bool; | 71 | fn match_with(&self, with: &SyntaxKind) -> bool; |
42 | fn match_kind(&self) -> SyntaxKind; | 72 | fn match_kind(&self) -> SyntaxKind; |
43 | } | 73 | } |