aboutsummaryrefslogtreecommitdiff
path: root/lib/src/lints/eta_reduction.rs
diff options
context:
space:
mode:
Diffstat (limited to 'lib/src/lints/eta_reduction.rs')
-rw-r--r--lib/src/lints/eta_reduction.rs30
1 files changed, 28 insertions, 2 deletions
diff --git a/lib/src/lints/eta_reduction.rs b/lib/src/lints/eta_reduction.rs
index 79a5101..3a483d0 100644
--- a/lib/src/lints/eta_reduction.rs
+++ b/lib/src/lints/eta_reduction.rs
@@ -1,4 +1,4 @@
1use crate::{Lint, Metadata, Report, Rule, Suggestion}; 1use crate::{Metadata, Report, Rule, Suggestion};
2 2
3use if_chain::if_chain; 3use if_chain::if_chain;
4use macros::lint; 4use macros::lint;
@@ -7,6 +7,32 @@ use rnix::{
7 NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, 7 NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode,
8}; 8};
9 9
10/// ## What it does
11/// Checks for eta-reducible functions, i.e.: converts lambda
12/// expressions into free standing functions where applicable.
13///
14/// ## Why is this bad?
15/// Oftentimes, eta-reduction results in code that is more natural
16/// to read.
17///
18/// ## Example
19///
20/// ```
21/// let
22/// double = i: 2 * i;
23/// in
24/// map (x: double x) [ 1 2 3 ]
25/// ```
26///
27/// The lambda passed to the `map` function is eta-reducible, and the
28/// result reads more naturally:
29///
30/// ```
31/// let
32/// double = i: 2 * i;
33/// in
34/// map double [ 1 2 3 ]
35/// ```
10#[lint( 36#[lint(
11 name = "eta reduction", 37 name = "eta reduction",
12 note = "This function expression is eta reducible", 38 note = "This function expression is eta reducible",
@@ -43,7 +69,7 @@ impl Rule for EtaReduction {
43 "Found eta-reduction: `{}`", 69 "Found eta-reduction: `{}`",
44 replacement.text().to_string() 70 replacement.text().to_string()
45 ); 71 );
46 Some(Self::report().suggest(at, message, Suggestion::new(at, replacement))) 72 Some(self.report().suggest(at, message, Suggestion::new(at, replacement)))
47 } else { 73 } else {
48 None 74 None
49 } 75 }