From a60429891fe9eb5290f95a52dd5e56f62d25d344 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 2 Oct 2021 10:50:47 +0530 Subject: new lint: empty-let-in --- lib/src/lib.rs | 6 +++--- lib/src/lints.rs | 1 + lib/src/lints/empty_let_in.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 lib/src/lints/empty_let_in.rs (limited to 'lib/src') 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 { self } /// Add a diagnostic with a fix to this report - pub fn suggest(mut self, at: TextRange, message: String, suggestion: Suggestion) -> Self { + pub fn suggest>(mut self, at: TextRange, message: S, suggestion: Suggestion) -> Self { self.diagnostics.push(Diagnostic::suggest(at, message, suggestion)); self } @@ -54,8 +54,8 @@ impl Diagnostic { Self { at, message, suggestion: None } } /// Construct a diagnostic with a fix. - pub fn suggest(at: TextRange, message: String, suggestion: Suggestion) -> Self { - Self { at, message, suggestion: Some(suggestion) } + pub fn suggest>(at: TextRange, message: S, suggestion: Suggestion) -> Self { + Self { at, message: message.as_ref().into(), suggestion: Some(suggestion) } } } 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; lint_map! { bool_comparison, + empty_let_in, } 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 @@ +use crate::{Lint, Metadata, Report, Rule, Suggestion}; + +use if_chain::if_chain; +use macros::lint; +use rnix::{ + types::{LetIn, TypedNode, + EntryHolder}, + NodeOrToken, SyntaxElement, SyntaxKind, +}; + +#[lint( + name = "empty let-in", + note = "Useless let-in expression", + code = 2, + match_with = SyntaxKind::NODE_LET_IN +)] +struct EmptyLetIn; + +impl Rule for EmptyLetIn { + fn validate(&self, node: &SyntaxElement) -> Option { + if_chain! { + if let NodeOrToken::Node(let_in_node) = node; + if let Some(let_in_expr) = LetIn::cast(let_in_node.clone()); + let entries = let_in_expr.entries(); + let inherits = let_in_expr.inherits(); + + if entries.count() == 0; + if inherits.count() == 0; + + if let Some(body) = let_in_expr.body(); + then { + let at = node.text_range(); + let replacement = body; + let message = "This let-in expression has no entries"; + Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) + } else { + None + } + } + } +} + -- cgit v1.2.3