Skip to content

Commit 2934cb8

Browse files
committed
chore: Reduce implementation of NoDotNamesCheck trait by blanket
implementations
1 parent d4d0f53 commit 2934cb8

2 files changed

Lines changed: 34 additions & 43 deletions

File tree

rusty_linter/src/post_linter/dots_linter.rs

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,62 @@ trait NoDotNamesCheck<T, E> {
1515
fn ensure_no_dots(&self, x: &T) -> Result<(), E>;
1616
}
1717

18-
impl NoDotNamesCheck<FunctionImplementation, LintErrorPos> for DotsLinter {
19-
fn ensure_no_dots(&self, x: &FunctionImplementation) -> Result<(), LintErrorPos> {
20-
self.ensure_no_dots(&x.name)?;
21-
self.ensure_no_dots(&x.params)
22-
}
23-
}
18+
// Blanket for Positioned
2419

25-
impl NoDotNamesCheck<SubImplementation, LintErrorPos> for DotsLinter {
26-
fn ensure_no_dots(&self, x: &SubImplementation) -> Result<(), LintErrorPos> {
27-
self.ensure_no_dots(&x.name)?;
28-
self.ensure_no_dots(&x.params)
20+
impl<T, E> NoDotNamesCheck<Positioned<T>, Positioned<E>> for DotsLinter
21+
where
22+
DotsLinter: NoDotNamesCheck<T, E>,
23+
{
24+
fn ensure_no_dots(&self, element: &Positioned<T>) -> Result<(), Positioned<E>> {
25+
let Positioned { element, pos } = element;
26+
self.ensure_no_dots(element).map_err(|e| e.at_pos(*pos))
2927
}
3028
}
3129

32-
impl NoDotNamesCheck<Vec<Positioned<Parameter>>, LintErrorPos> for DotsLinter {
33-
fn ensure_no_dots(&self, x: &Vec<Positioned<Parameter>>) -> Result<(), LintErrorPos> {
30+
// Blanket for Vec
31+
32+
impl<T, E> NoDotNamesCheck<Vec<T>, E> for DotsLinter
33+
where
34+
DotsLinter: NoDotNamesCheck<T, E>,
35+
{
36+
fn ensure_no_dots(&self, x: &Vec<T>) -> Result<(), E> {
3437
x.iter().try_for_each(|x| self.ensure_no_dots(x))
3538
}
3639
}
3740

38-
impl NoDotNamesCheck<Positioned<Parameter>, LintErrorPos> for DotsLinter {
39-
fn ensure_no_dots(&self, x: &Positioned<Parameter>) -> Result<(), LintErrorPos> {
40-
let Positioned { element, pos } = x;
41-
self.ensure_no_dots(element).map_err(|e| e.at(pos))
41+
// FunctionImplementation and SubImplementation
42+
43+
impl<T> NoDotNamesCheck<SubprogramImplementation<T>, LintErrorPos> for DotsLinter
44+
where
45+
DotsLinter:
46+
NoDotNamesCheck<Positioned<T>, LintErrorPos> + NoDotNamesCheck<Parameters, LintErrorPos>,
47+
{
48+
fn ensure_no_dots(&self, x: &SubprogramImplementation<T>) -> Result<(), LintErrorPos> {
49+
self.ensure_no_dots(&x.name)?;
50+
self.ensure_no_dots(&x.params)
4251
}
4352
}
4453

54+
// TODO the next 4 can be merged into one with a trait like AsRef<BareName>
55+
4556
impl NoDotNamesCheck<Parameter, LintError> for DotsLinter {
4657
fn ensure_no_dots(&self, x: &Parameter) -> Result<(), LintError> {
4758
self.ensure_no_dots(&x.bare_name)
4859
}
4960
}
5061

51-
impl NoDotNamesCheck<DimVarPos, LintErrorPos> for DotsLinter {
52-
fn ensure_no_dots(&self, x: &DimVarPos) -> Result<(), LintErrorPos> {
53-
let Positioned { element, pos } = x;
54-
self.ensure_no_dots(element).map_err(|e| e.at(pos))
55-
}
56-
}
57-
5862
impl NoDotNamesCheck<DimVar, LintError> for DotsLinter {
5963
fn ensure_no_dots(&self, x: &DimVar) -> Result<(), LintError> {
6064
self.ensure_no_dots(&x.bare_name)
6165
}
6266
}
6367

64-
impl NoDotNamesCheck<NamePos, LintErrorPos> for DotsLinter {
65-
fn ensure_no_dots(&self, name_pos: &NamePos) -> Result<(), LintErrorPos> {
66-
let name = &name_pos.element;
67-
self.ensure_no_dots(name).map_err(|e| e.at(name_pos))
68-
}
69-
}
70-
7168
impl NoDotNamesCheck<Name, LintError> for DotsLinter {
7269
fn ensure_no_dots(&self, name: &Name) -> Result<(), LintError> {
7370
self.ensure_no_dots(name.bare_name())
7471
}
7572
}
7673

77-
impl NoDotNamesCheck<BareNamePos, LintErrorPos> for DotsLinter {
78-
fn ensure_no_dots(&self, x: &BareNamePos) -> Result<(), LintErrorPos> {
79-
let Positioned { element, pos } = x;
80-
self.ensure_no_dots(element).map_err(|e| e.at(pos))
81-
}
82-
}
83-
8474
impl NoDotNamesCheck<BareName, LintError> for DotsLinter {
8575
fn ensure_no_dots(&self, x: &BareName) -> Result<(), LintError> {
8676
match x.prefix('.') {
@@ -96,12 +86,6 @@ impl NoDotNamesCheck<BareName, LintError> for DotsLinter {
9686
}
9787
}
9888

99-
impl NoDotNamesCheck<Expressions, LintErrorPos> for DotsLinter {
100-
fn ensure_no_dots(&self, x: &Expressions) -> Result<(), LintErrorPos> {
101-
x.iter().try_for_each(|x| self.ensure_no_dots(x))
102-
}
103-
}
104-
10589
impl NoDotNamesCheck<Positioned<&Expression>, LintErrorPos> for DotsLinter {
10690
fn ensure_no_dots(&self, x: &Positioned<&Expression>) -> Result<(), LintErrorPos> {
10791
let Positioned { element, pos } = x;

rusty_linter/src/post_linter/post_linter.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,34 @@ fn apply_linters(
2828
let mut linter = for_next_counter_match_linter::ForNextCounterMatch::visitor();
2929
linter.visit(result)?;
3030

31+
// TODO migrate to Visitor
3132
let mut linter = dots_linter::DotsLinter::default();
3233
linter.visit_program(result)?;
3334

35+
// TODO migrate to Visitor
3436
let mut linter = built_in_linter::BuiltInLinter::new();
3537
linter.visit_program(result)?;
3638

3739
let mut linter = print_linter::PrintLinter::visitor();
3840
linter.visit(result)?;
3941

42+
// TODO migrate to Visitor
4043
let mut linter = user_defined_function_linter::UserDefinedFunctionLinter { linter_context };
4144
linter.visit_program(result)?;
4245

46+
// TODO migrate to Visitor
4347
let mut linter = user_defined_sub_linter::UserDefinedSubLinter { linter_context };
4448
linter.visit_program(result)?;
4549

50+
// TODO migrate to Visitor
4651
let mut linter = select_case_linter::SelectCaseLinter {};
4752
linter.visit_program(result)?;
4853

54+
// TODO migrate to Visitor
4955
let mut linter = condition_type_linter::ConditionTypeLinter {};
5056
linter.visit_program(result)?;
5157

58+
// TODO migrate to Visitor
5259
let mut linter = label_linter::LabelLinter::default();
5360
linter.visit_program(result)?;
5461
Ok(())

0 commit comments

Comments
 (0)