Lints and suggestions for the Nix programming language
1use crate::{Metadata, Report, Rule, Suggestion};
2
3use macros::lint;
4use rnix::{NodeOrToken, SyntaxElement, SyntaxKind, ast::Pattern};
5use rowan::ast::AstNode as _;
6
7/// ## What it does
8/// Checks for binds of the form `inputs @ { ... }` in function
9/// arguments.
10///
11/// ## Why is this bad?
12/// The variadic pattern here is redundant, as it does not capture
13/// anything.
14///
15/// ## Example
16///
17/// ```nix
18/// inputs @ { ... }: inputs.nixpkgs
19/// ```
20///
21/// Remove the pattern altogether:
22///
23/// ```nix
24/// inputs: inputs.nixpkgs
25/// ```
26#[lint(
27 name = "redundant_pattern_bind",
28 note = "Found redundant pattern bind in function argument",
29 code = 11,
30 match_with = SyntaxKind::NODE_PATTERN
31)]
32struct RedundantPatternBind;
33
34impl Rule for RedundantPatternBind {
35 fn validate(&self, node: &SyntaxElement) -> Option<Report> {
36 let NodeOrToken::Node(node) = node else {
37 return None;
38 };
39
40 let pattern = Pattern::cast(node.clone())?;
41
42 // no patterns within `{ }`
43 if pattern.pat_entries().count() != 0 {
44 return None;
45 }
46
47 // pattern is just ellipsis
48 pattern.ellipsis_token()?;
49
50 // pattern is bound
51 let pat_bind = pattern.pat_bind()?;
52 let ident = pat_bind.ident()?;
53 let at = node.text_range();
54 let message = format!("This pattern bind is redundant, use `{ident}` instead");
55 let replacement = ident.syntax().clone();
56
57 Some(
58 self.report()
59 .suggest(at, message, Suggestion::with_replacement(at, replacement)),
60 )
61 }
62}