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
|
use crate::{make, Lint, Metadata, Report, Rule, Suggestion};
use if_chain::if_chain;
use macros::lint;
use rowan::Direction;
use rnix::{
types::{LetIn, TypedNode},
NodeOrToken, SyntaxElement, SyntaxKind, TextRange
};
#[lint(
name = "collapsible let in",
note = "These let-in expressions are collapsible",
code = 6,
match_with = SyntaxKind::NODE_LET_IN
)]
struct CollapsibleLetIn;
impl Rule for CollapsibleLetIn {
fn validate(&self, node: &SyntaxElement) -> Option<Report> {
if_chain! {
if let NodeOrToken::Node(node) = node;
if let Some(let_in_expr) = LetIn::cast(node.clone());
if let Some(body) = let_in_expr.body();
if LetIn::cast(body.clone()).is_some();
then {
let first_annotation = node.text_range();
let first_message = "This `let in` expression contains a nested `let in` expression";
let second_annotation = body.text_range();
let second_message = "This `let in` expression is nested";
let replacement_at = {
let start = body
.siblings_with_tokens(Direction::Prev)
.find(|elem| elem.kind() == SyntaxKind::TOKEN_IN)?
.text_range()
.start();
let end = body
.descendants_with_tokens()
.find(|elem| elem.kind() == SyntaxKind::TOKEN_LET)?
.text_range()
.end();
TextRange::new(start, end)
};
let replacement = make::empty().node().clone();
Some(
Self::report()
.diagnostic(first_annotation, first_message)
.suggest(second_annotation, second_message, Suggestion::new(replacement_at, replacement))
)
} else {
None
}
}
}
}
|