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
explain_lint! {
L0001: RedundantNestingLinter
}
use crate::linter::LintRule;
use crate::common::Span;
use crate::diagnostics::Diagnostic;
use crate::grammar::visit::StmtVisitor;
use crate::grammar::*;
pub struct RedundantNestingLinter<'a> {
source: &'a str,
diagnostics: Vec<Diagnostic>,
}
impl<'a> RedundantNestingLinter<'a> {
pub fn new(source: &'a str) -> Self {
Self {
source,
diagnostics: vec![],
}
}
}
impl<'a> RedundantNestingLinter<'a> {
fn visit_nesting(&mut self, mut expr: &'a RcExpr, span: Span) {
let mut nestings = 1;
while let Expr::Parend(inner) | Expr::Bracketed(inner) = expr.as_ref() {
expr = inner;
nestings += 1;
}
if nestings > 1 {
let opener = &self.source[span.lo..span.lo + 1];
let closer = &self.source[span.hi - 1..span.hi];
let inner_expr = expr.span.over(self.source);
self.diagnostics.push(
Diagnostic::span_warn(span, "Redundant nesting", Self::CODE, None).with_help(
format!(
r#"consider reducing this expression to "{}{}{}""#,
opener, inner_expr, closer
),
),
)
}
visit::descend_expr(self, expr);
}
}
impl<'a> visit::StmtVisitor<'a> for RedundantNestingLinter<'a> {
fn visit_parend(&mut self, expr: &'a RcExpr, span: Span) {
self.visit_nesting(expr, span);
}
fn visit_bracketed(&mut self, expr: &'a RcExpr, span: Span) {
self.visit_nesting(expr, span);
}
}
impl<'a> LintRule<'a, StmtList> for RedundantNestingLinter<'a> {
fn lint(stmt_list: &StmtList, source: &'a str) -> Vec<Diagnostic> {
let mut linter = Self::new(&source);
linter.visit_stmt_list(stmt_list);
linter.diagnostics
}
}