From c5c4b55c2b3a3fb824c3fd64e33bb30f1b011b71 Mon Sep 17 00:00:00 2001
From: Akshay <nerdy@peppe.rs>
Date: Sat, 2 Oct 2021 20:53:19 +0530
Subject: new lint: legacy_let_syntax

---
 lib/src/lints/legacy_let_syntax.rs | 55 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 55 insertions(+)
 create mode 100644 lib/src/lints/legacy_let_syntax.rs

(limited to 'lib/src/lints')

diff --git a/lib/src/lints/legacy_let_syntax.rs b/lib/src/lints/legacy_let_syntax.rs
new file mode 100644
index 0000000..c5588e2
--- /dev/null
+++ b/lib/src/lints/legacy_let_syntax.rs
@@ -0,0 +1,55 @@
+use crate::{make, Lint, Metadata, Report, Rule, Suggestion};
+
+use if_chain::if_chain;
+use macros::lint;
+use rnix::{
+    types::{EntryHolder, Ident, Key, LegacyLet, TokenWrapper, TypedNode},
+    NodeOrToken, SyntaxElement, SyntaxKind,
+};
+
+#[lint(
+    name = "legacy let syntax",
+    note = "Using undocumented `let` syntax",
+    code = 5,
+    match_with = SyntaxKind::NODE_LEGACY_LET
+)]
+struct ManualInherit;
+
+impl Rule for ManualInherit {
+    fn validate(&self, node: &SyntaxElement) -> Option<Report> {
+        if_chain! {
+            if let NodeOrToken::Node(node) = node;
+            if let Some(legacy_let) = LegacyLet::cast(node.clone());
+
+            if legacy_let
+                .entries()
+                .find(|kv| matches!(kv.key(), Some(k) if key_is_ident(&k, "body")))
+                .is_some();
+
+            then {
+                let inherits = legacy_let.inherits();
+                let entries = legacy_let.entries();
+                let attrset = make::attrset(inherits, entries, true);
+                let parenthesized = make::parenthesize(&attrset.node());
+                let selected = make::select(parenthesized.node(), &make::ident("body").node());
+
+                let at = node.text_range();
+                let message = "Prefer `rec` over undocumented `let` syntax";
+                let replacement = selected.node().clone();
+
+                Some(Self::report().suggest(at, message, Suggestion::new(at, replacement)))
+            } else {
+                None
+            }
+        }
+    }
+}
+
+fn key_is_ident(key_path: &Key, ident: &str) -> bool {
+    if let Some(key_node) = key_path.path().next() {
+        if let Some(key) = Ident::cast(key_node) {
+            return key.as_str() == ident;
+        }
+    }
+    false
+}
-- 
cgit v1.2.3