Something went wrong. Try again.
Lints and suggestions for the Nix programming language forked from oppi.li/statix
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768use crate::{Metadata, Report, Rule, Suggestion};
use macros::lint;use rnix::{ NodeOrToken, SyntaxElement, SyntaxKind, ast::{HasEntry as _, LetIn},};use rowan::ast::AstNode as _;
/// ## What it does/// Checks for `let-in` expressions which create no new bindings.////// ## Why is this bad?/// `let-in` expressions that create no new bindings are useless./// These are probably remnants from debugging or editing expressions.////// ## Example////// ```nix/// let in pkgs.statix/// ```////// Preserve only the body of the `let-in` expression:////// ```nix/// pkgs.statix/// ```#[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<Report> { if let NodeOrToken::Node(node) = node && let Some(let_in_expr) = LetIn::cast(node.clone()) && let entries = let_in_expr.entries() && let inherits = let_in_expr.inherits() && entries.count() == 0 && inherits.count() == 0 && let Some(body) = let_in_expr.body() { // ensure that the let-in-expr does not have comments let has_comments = node .children_with_tokens() .any(|el| el.kind() == SyntaxKind::TOKEN_COMMENT);
let at = node.text_range(); let replacement = body.syntax(); let message = "This let-in expression has no entries"; Some(if has_comments { self.report().diagnostic(at, message) } else { self.report().suggest( at, message, Suggestion::with_replacement(at, replacement.clone()), ) }) } else { None } }}