From 33490aa211fdd2b2292c396002123035e0b1fe54 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 4 Jun 2022 16:20:19 +0530 Subject: do not raise empty-let when comments are present --- bin/tests/data/empty_let_in.nix | 16 +++++++++++++--- bin/tests/snapshots/main__empty_let_in.snap | 10 +++++----- lib/src/lints/empty_let_in.rs | 8 ++++++++ 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/bin/tests/data/empty_let_in.nix b/bin/tests/data/empty_let_in.nix index 3ecb6e4..54fee2a 100644 --- a/bin/tests/data/empty_let_in.nix +++ b/bin/tests/data/empty_let_in.nix @@ -1,3 +1,13 @@ -let -in - null +[ + ( + let + in + null + ) + ( + let + # don't match this, we have a comment + in + null + ) +] diff --git a/bin/tests/snapshots/main__empty_let_in.snap b/bin/tests/snapshots/main__empty_let_in.snap index 426692f..df0911d 100644 --- a/bin/tests/snapshots/main__empty_let_in.snap +++ b/bin/tests/snapshots/main__empty_let_in.snap @@ -4,11 +4,11 @@ expression: "&out" --- [W02] Warning: Useless let-in expression - ╭─[data/empty_let_in.nix:1:1] + ╭─[data/empty_let_in.nix:3:5] │ - 1 │ ╭─▶ let - 3 │ ├─▶ null - · │ - · ╰──────────── This let-in expression has no entries + 3 │ ╭─▶ let + 5 │ ├─▶ null + · │ + · ╰────────────── This let-in expression has no entries ───╯ diff --git a/lib/src/lints/empty_let_in.rs b/lib/src/lints/empty_let_in.rs index e42f658..390a1e1 100644 --- a/lib/src/lints/empty_let_in.rs +++ b/lib/src/lints/empty_let_in.rs @@ -1,3 +1,5 @@ +use std::ops::Not; + use crate::{session::SessionInfo, Metadata, Report, Rule, Suggestion}; use if_chain::if_chain; @@ -45,6 +47,12 @@ impl Rule for EmptyLetIn { if inherits.count() == 0; if let Some(body) = let_in_expr.body(); + + // ensure that the let-in-expr does not have comments + if node + .children_with_tokens() + .any(|el| el.kind() == SyntaxKind::TOKEN_COMMENT) + .not(); then { let at = node.text_range(); let replacement = body; -- cgit v1.2.3