aboutsummaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/lib.rs6
-rw-r--r--lib/src/lints.rs1
-rw-r--r--lib/src/lints/empty_let_in.rs42
3 files changed, 46 insertions, 3 deletions
diff --git a/lib/src/lib.rs b/lib/src/lib.rs
index 93792d4..ab4a14a 100644
--- a/lib/src/lib.rs
+++ b/lib/src/lib.rs
@@ -32,7 +32,7 @@ impl Report {
32 self 32 self
33 } 33 }
34 /// Add a diagnostic with a fix to this report 34 /// Add a diagnostic with a fix to this report
35 pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self { 35 pub fn suggest<S: AsRef<str>>(mut self, at: TextRange, message: S, suggestion: Suggestion) -> Self {
36 self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); 36 self.diagnostics.push(Diagnostic::suggest(at, message, suggestion));
37 self 37 self
38 } 38 }
@@ -54,8 +54,8 @@ impl Diagnostic {
54 Self { at, message, suggestion: None } 54 Self { at, message, suggestion: None }
55 } 55 }
56 /// Construct a diagnostic with a fix. 56 /// Construct a diagnostic with a fix.
57 pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self { 57 pub fn suggest<S: AsRef<str>>(at: TextRange, message: S, suggestion: Suggestion) -> Self {
58 Self { at, message, suggestion: Some(suggestion) } 58 Self { at, message: message.as_ref().into(), suggestion: Some(suggestion) }
59 } 59 }
60} 60}
61 61
diff --git a/lib/src/lints.rs b/lib/src/lints.rs
index 15d9063..a5fe0da 100644
--- a/lib/src/lints.rs
+++ b/lib/src/lints.rs
@@ -2,4 +2,5 @@ use crate::lint_map;
2 2
3lint_map! { 3lint_map! {
4 bool_comparison, 4 bool_comparison,
5 empty_let_in,
5} 6}
diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs
new file mode 100644
index 0000000..4b074e7
--- /dev/null
+++ b/lib/src/lints/empty_let_in.rs
@@ -0,0 +1,42 @@
1use crate::{Lint, Metadata, Report, Rule, Suggestion};
2
3use if_chain::if_chain;
4use macros::lint;
5use rnix::{
6 types::{LetIn, TypedNode,
7 EntryHolder},
8 NodeOrToken, SyntaxElement, SyntaxKind,
9};
10
11#[lint(
12 name = "empty let-in",
13 note = "Useless let-in expression",
14 code = 2,
15 match_with = SyntaxKind::NODE_LET_IN
16)]
17struct EmptyLetIn;
18
19impl Rule for EmptyLetIn {
20 fn validate(&self, node: &SyntaxElement) -> Option<Report> {
21 if_chain! {
22 if let NodeOrToken::Node(let_in_node) = node;
23 if let Some(let_in_expr) = LetIn::cast(let_in_node.clone());
24 let entries = let_in_expr.entries();
25 let inherits = let_in_expr.inherits();
26
27 if entries.count() == 0;
28 if inherits.count() == 0;
29
30 if let Some(body) = let_in_expr.body();
31 then {
32 let at = node.text_range();
33 let replacement = body;
34 let message = "This let-in expression has no entries";
35 Some(Self::report().suggest(at, message, Suggestion::new(at, replacement)))
36 } else {
37 None
38 }
39 }
40 }
41}
42