1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
use crate::{Metadata, Report, Rule, Suggestion};
use if_chain::if_chain;
use macros::lint;
use rnix::{
types::{Apply, Ident, Lambda, TokenWrapper, TypedNode},
NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode,
};
/// ## What it does
/// Checks for eta-reducible functions, i.e.: converts lambda
/// expressions into free standing functions where applicable.
///
/// ## Why is this bad?
/// Oftentimes, eta-reduction results in code that is more natural
/// to read.
///
/// ## Example
///
/// ```nix
/// let
/// double = i: 2 * i;
/// in
/// map (x: double x) [ 1 2 3 ]
/// ```
///
/// The lambda passed to the `map` function is eta-reducible, and the
/// result reads more naturally:
///
/// ```nix
/// let
/// double = i: 2 * i;
/// in
/// map double [ 1 2 3 ]
/// ```
#[lint(
name = "eta_reduction",
note = "This function expression is eta reducible",
code = 7,
match_with = SyntaxKind::NODE_LAMBDA
)]
struct EtaReduction;
impl Rule for EtaReduction {
fn validate(&self, node: &SyntaxElement) -> Option<Report> {
if_chain! {
if let NodeOrToken::Node(node) = node;
if let Some(lambda_expr) = Lambda::cast(node.clone());
if let Some(arg_node) = lambda_expr.arg();
if let Some(arg) = Ident::cast(arg_node);
if let Some(body_node) = lambda_expr.body();
if let Some(body) = Apply::cast(body_node);
if let Some(value_node) = body.value();
if let Some(value) = Ident::cast(value_node);
if arg.as_str() == value.as_str() ;
if let Some(lambda_node) = body.lambda();
if !mentions_ident(&arg, &lambda_node);
then {
let at = node.text_range();
let replacement = body.lambda()?;
let message =
format!(
"Found eta-reduction: `{}`",
replacement.text()
);
Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
} else {
None
}
}
}
}
fn mentions_ident(ident: &Ident, node: &SyntaxNode) -> bool {
if let Some(node_ident) = Ident::cast(node.clone()) {
node_ident.as_str() == ident.as_str()
} else {
node.children().any(|child| mentions_ident(ident, &child))
}
}
|