Skip to content

Commit 581e2ad

Browse files
committed
Wrap LocalDMLError in DMLStyleError to hold local Lint module info
1 parent 5ad1864 commit 581e2ad

File tree

8 files changed

+143
-92
lines changed

8 files changed

+143
-92
lines changed

src/analysis/parsing/expression.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ use crate::analysis::structure::expressions::DMLString;
1717
use crate::analysis::{DeclarationSpan, LocalDMLError};
1818

1919

20-
use crate::lint::{rules::{spacing::{NspFunparArgs,
20+
use crate::lint::{DMLStyleError,
21+
rules::{spacing::{NspFunparArgs,
2122
NspInparenArgs,
2223
NspUnaryArgs,
2324
SpPunctArgs},
@@ -37,7 +38,7 @@ impl TreeElement for UnaryExpressionContent {
3738
fn subs(&self) -> TreeElements<'_> {
3839
create_subs!(&self.operation, &self.expr)
3940
}
40-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
41+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
4142
rules.nsp_unary.check(acc, NspUnaryArgs::from_unary_expr(self));
4243
}
4344
}
@@ -71,7 +72,7 @@ impl TreeElement for PostUnaryExpressionContent {
7172
fn subs(&self) -> TreeElements<'_> {
7273
create_subs!(&self.expr, &self.operation)
7374
}
74-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
75+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
7576
rules.nsp_unary.check(acc, NspUnaryArgs::from_postunary_expr(self));
7677
}
7778
}
@@ -209,7 +210,7 @@ impl TreeElement for FunctionCallContent {
209210
noderef, ReferenceKind::Callable));
210211
}
211212
}
212-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
213+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
213214
rules.nsp_funpar.check(acc, NspFunparArgs::from_function_call(self));
214215
rules.nsp_inparen.check(acc, NspInparenArgs::from_function_call(self));
215216
rules.sp_punct.check(acc, SpPunctArgs::from_function_call(self));
@@ -420,7 +421,7 @@ impl TreeElement for IndexContent {
420421
fn subs(&self) -> TreeElements<'_> {
421422
create_subs!(&self.array, &self.lbracket, &self.index, &self.rbracket)
422423
}
423-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
424+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
424425
rules.nsp_inparen.check(acc, NspInparenArgs::from_index(self));
425426
}
426427
}

src/analysis/parsing/statement.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::analysis::parsing::misc::{Initializer, InitializerContent, CDecl,
2121
ident_filter, objident_filter};
2222
use crate::analysis::parsing::structure::{parse_vardecl, VarDecl};
2323
use crate::analysis::LocalDMLError;
24-
use crate::lint::{rules::{CurrentRules,
24+
use crate::lint::{DMLStyleError,
25+
rules::{CurrentRules,
2526
indentation::{IN3Args, IN9Args},
2627
spacing::{NspInparenArgs,
2728
SpBracesArgs,
@@ -141,7 +142,7 @@ impl TreeElement for CompoundContent {
141142
fn subs(&self) -> TreeElements<'_> {
142143
create_subs!(&self.lbrace, &self.statements, &self.rbrace)
143144
}
144-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, aux: &mut AuxParams) {
145+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: &mut AuxParams) {
145146
rules.sp_brace.check(acc, SpBracesArgs::from_compound(self));
146147
rules.in3.check(acc, IN3Args::from_compound_content(self, &mut aux.depth));
147148
}
@@ -195,7 +196,7 @@ impl TreeElement for VariableDeclContent {
195196
fn post_parse_sanity(&self, _file: &TextFile) -> Vec<LocalDMLError> {
196197
self.decls.ensure_named()
197198
}
198-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
199+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
199200
rules.sp_punct.check(acc, SpPunctArgs::from_variable_decl(self));
200201
}
201202
}
@@ -427,7 +428,7 @@ impl TreeElement for IfContent {
427428
&self.truebranch,
428429
&self.elsebranch)
429430
}
430-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
431+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
431432
rules.nsp_inparen.check(acc, NspInparenArgs::from_if(self));
432433
}
433434
}
@@ -1003,7 +1004,7 @@ impl TreeElement for SwitchCase {
10031004
Self::Default(default, colon) => create_subs!(default, colon),
10041005
}
10051006
}
1006-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, aux: &mut AuxParams) {
1007+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: &mut AuxParams) {
10071008
rules.in9.check(acc, IN9Args::from_switch_case(self, &mut aux.depth));
10081009
}
10091010
}
@@ -1715,7 +1716,7 @@ impl TreeElement for ExpressionStmtContent {
17151716
fn subs(&self) -> TreeElements<'_> {
17161717
create_subs!(&self.expression, &self.semi)
17171718
}
1718-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
1719+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
17191720
rules.sp_punct.check(acc, SpPunctArgs::from_expression_stmt(self));
17201721
}
17211722
}

src/analysis/parsing/structure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::lint::rules::spacing::{SpBracesArgs,
2121
NspFunparArgs,
2222
SpPunctArgs};
2323
use crate::lint::rules::indentation::{IN3Args};
24-
use crate::lint::{rules::CurrentRules, AuxParams};
24+
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
2525
use crate::analysis::reference::{Reference, ReferenceKind};
2626
use crate::analysis::FileSpec;
2727
use crate::span::Range;
@@ -235,7 +235,7 @@ impl TreeElement for MethodContent {
235235
}
236236
errors
237237
}
238-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
238+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
239239
rules.nsp_funpar.check(acc, NspFunparArgs::from_method(self));
240240
rules.nsp_inparen.check(acc, NspInparenArgs::from_method(self));
241241
rules.sp_punct.check(acc, SpPunctArgs::from_method(self));
@@ -704,7 +704,7 @@ impl TreeElement for ObjectStatementsContent {
704704
create_subs!(left, vect, right)
705705
}
706706
}
707-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, aux: &mut AuxParams) {
707+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: &mut AuxParams) {
708708
rules.sp_brace.check(acc, SpBracesArgs::from_obj_stmts(self));
709709
rules.in3.check(acc, IN3Args::from_obj_stmts_content(self, &mut aux.depth));
710710
}

src/analysis/parsing/tree.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::analysis::reference::{Reference, NodeRef, MaybeIsNodeRef,
77
use crate::analysis::FileSpec;
88
use crate::analysis::structure::expressions::DMLString;
99

10-
use crate::lint::{rules::CurrentRules, AuxParams};
10+
use crate::lint::{rules::CurrentRules, AuxParams, DMLStyleError};
1111
use crate::span::{Range, Span, ZeroIndexed, Position, FilePosition};
1212
use crate::vfs::{Vfs as GenVfs, TextFile};
1313

@@ -91,13 +91,13 @@ pub trait TreeElement {
9191
self.default_references(accumulator, file);
9292
}
9393

94-
fn style_check(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, mut aux: AuxParams) {
94+
fn style_check(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, mut aux: AuxParams) {
9595
self.evaluate_rules(acc, rules, &mut aux);
9696
for sub in self.subs() {
9797
sub.style_check(acc, rules, aux);
9898
}
9999
}
100-
fn evaluate_rules(&self, _acc: &mut Vec<LocalDMLError>, _rules: &CurrentRules, _aux: &mut AuxParams) {} // default NOOP
100+
fn evaluate_rules(&self, _acc: &mut Vec<DMLStyleError>, _rules: &CurrentRules, _aux: &mut AuxParams) {} // default NOOP
101101
}
102102

103103
impl <T: ?Sized + TreeElement> ReferenceContainer for T {

src/analysis/parsing/types.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use crate::lint::{rules::{indentation::IN3Args,
44
spacing::SpBracesArgs,
55
CurrentRules},
6-
AuxParams};
6+
AuxParams,
7+
DMLStyleError};
78
use crate::span::Range;
89
use crate::analysis::parsing::lexer::TokenKind;
910
use crate::analysis::parsing::parser::{doesnt_understand_tokens,
@@ -52,7 +53,7 @@ impl TreeElement for StructTypeContent {
5253
}
5354
errors
5455
}
55-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, aux: &mut AuxParams) {
56+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: &mut AuxParams) {
5657
rules.in3.check(acc, IN3Args::from_struct_type_content(self, &mut aux.depth));
5758
rules.sp_brace.check(acc, SpBracesArgs::from_struct_type_content(self));
5859
}
@@ -131,7 +132,7 @@ impl TreeElement for LayoutContent {
131132
}
132133
errors
133134
}
134-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, aux: &mut AuxParams) {
135+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, aux: &mut AuxParams) {
135136
rules.in3.check(acc, IN3Args::from_layout_content(self, &mut aux.depth));
136137
rules.sp_brace.check(acc, SpBracesArgs::from_layout_content(self));
137138
}
@@ -304,7 +305,7 @@ impl TreeElement for BitfieldsContent {
304305
}
305306
errors
306307
}
307-
fn evaluate_rules(&self, acc: &mut Vec<LocalDMLError>, rules: &CurrentRules, _aux: &mut AuxParams) {
308+
fn evaluate_rules(&self, acc: &mut Vec<DMLStyleError>, rules: &CurrentRules, _aux: &mut AuxParams) {
308309
rules.sp_brace.check(acc, SpBracesArgs::from_bitfields_content(self));
309310
}
310311
}

src/lint/mod.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ impl Default for LintCfg {
9191
}
9292
}
9393

94+
pub struct DMLStyleError {
95+
pub error: LocalDMLError,
96+
pub rule_name: String,
97+
}
98+
9499
#[derive(Debug, Clone)]
95100
pub struct LinterAnalysis {
96101
pub path: CanonPath,
@@ -128,7 +133,7 @@ impl LinterAnalysis {
128133
}
129134

130135
pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Result<Vec<LocalDMLError>, Error> {
131-
let mut linting_errors: Vec<LocalDMLError> = vec![];
136+
let mut linting_errors: Vec<DMLStyleError> = vec![];
132137
ast.style_check(&mut linting_errors, rules, AuxParams { depth: 0 });
133138

134139
// Per line checks
@@ -144,21 +149,21 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
144149

145150
post_process_linting_errors(&mut linting_errors);
146151

147-
Ok(linting_errors)
152+
Ok(linting_errors.into_iter().map(|e| e.error).collect())
148153
}
149154

150-
fn post_process_linting_errors(errors: &mut Vec<LocalDMLError>) {
155+
fn post_process_linting_errors(errors: &mut Vec<DMLStyleError>) {
151156
// Collect in2 ranges
152157
let in2_ranges: Vec<_> = errors.iter()
153-
.filter(|error| error.description == IN2Rule::description())
154-
.map(|error| error.range)
158+
.filter(|style_err| style_err.rule_name == IN2Rule::name())
159+
.map(|style_err| style_err.error.range)
155160
.collect();
156161

157162
// Remove linting errors that are in in2 rows
158-
errors.retain(|error| {
163+
errors.retain(|style_err| {
159164
!in2_ranges.iter().any(|range|
160-
(range.row_start == error.range.row_start || range.row_end == error.range.row_end)
161-
&& error.description != IN2Rule::description())
165+
(range.row_start == style_err.error.range.row_start || range.row_end == style_err.error.range.row_end)
166+
&& style_err.rule_name != IN2Rule::name())
162167
});
163168
}
164169

src/lint/rules/indentation.rs

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::analysis::LocalDMLError;
88
use crate::analysis::parsing::tree::{ZeroRange, Content, TreeElement};
99
use serde::{Deserialize, Serialize};
1010
use super::Rule;
11-
use crate::lint::LintCfg;
11+
use crate::lint::{LintCfg, DMLStyleError};
1212

1313
pub const MAX_LENGTH_DEFAULT: u32 = 80;
1414
pub const INDENTATION_LEVEL_DEFAULT: u32 = 4;
@@ -56,16 +56,19 @@ impl LongLinesRule {
5656
},
5757
}
5858
}
59-
pub fn check(&self, acc: &mut Vec<LocalDMLError>, row: usize, line: &str) {
59+
pub fn check(&self, acc: &mut Vec<DMLStyleError>, row: usize, line: &str) {
6060
if !self.enabled { return; }
6161
let len = line.len().try_into().unwrap();
6262
if len > self.max_length {
6363
let rowu32 = row.try_into().unwrap();
6464
let msg = LongLinesRule::description().to_owned()
6565
+ format!(" of {} characters", self.max_length).as_str();
66-
let dmlerror = LocalDMLError {
67-
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, self.max_length, len),
68-
description: msg,
66+
let dmlerror = DMLStyleError {
67+
error: LocalDMLError {
68+
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, self.max_length, len),
69+
description: msg,
70+
},
71+
rule_name: Self::name().to_string(),
6972
};
7073
acc.push(dmlerror);
7174
}
@@ -93,16 +96,19 @@ pub struct IN2Rule {
9396
}
9497

9598
impl IN2Rule {
96-
pub fn check(&self, acc: &mut Vec<LocalDMLError>, row: usize, line: &str) {
99+
pub fn check(&self, acc: &mut Vec<DMLStyleError>, row: usize, line: &str) {
97100
if !self.enabled { return; }
98101
let rowu32 = row.try_into().unwrap();
99102

100103
for (col, _) in line.match_indices('\t') {
101104
let colu32 = col.try_into().unwrap();
102105
let msg = IN2Rule::description().to_owned();
103-
let dmlerror = LocalDMLError {
104-
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, colu32, colu32 + 1),
105-
description: msg,
106+
let dmlerror = DMLStyleError {
107+
error: LocalDMLError {
108+
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, colu32, colu32 + 1),
109+
description: msg,
110+
},
111+
rule_name: Self::name().to_string(),
106112
};
107113
acc.push(dmlerror);
108114
}
@@ -187,7 +193,7 @@ impl IN3Rule {
187193
}
188194
}
189195
}
190-
pub fn check<'a>(&self, acc: &mut Vec<LocalDMLError>,
196+
pub fn check<'a>(&self, acc: &mut Vec<DMLStyleError>,
191197
args: Option<IN3Args<'a>>)
192198
{
193199
if !self.enabled { return; }
@@ -197,9 +203,12 @@ impl IN3Rule {
197203
*args.expected_depth += 1;
198204
for member_range in args.members_ranges {
199205
if self.indentation_is_not_aligned(member_range, *args.expected_depth) {
200-
let dmlerror = LocalDMLError {
201-
range: member_range,
202-
description: Self::description().to_string(),
206+
let dmlerror = DMLStyleError {
207+
error: LocalDMLError {
208+
range: member_range,
209+
description: Self::description().to_string(),
210+
},
211+
rule_name: Self::name().to_string(),
203212
};
204213
acc.push(dmlerror);
205214
}
@@ -248,7 +257,7 @@ impl IN6Rule {
248257
}
249258
}
250259

251-
pub fn check(&self, acc: &mut Vec<LocalDMLError>, lines: &[&str]) {
260+
pub fn check(&self, acc: &mut Vec<DMLStyleError>, lines: &[&str]) {
252261
if !self.enabled {
253262
return;
254263
}
@@ -274,14 +283,17 @@ impl IN6Rule {
274283
let actual_indent = next_line.chars().take_while(|c| c.is_whitespace()).count();
275284
if actual_indent != expected_indent {
276285
let msg = IN6Rule::description().to_owned();
277-
let dmlerror = LocalDMLError {
278-
range: Range::new(
279-
Row::new_zero_indexed((i + 1) as u32),
280-
Row::new_zero_indexed((i + 1) as u32),
281-
Column::new_zero_indexed(0),
282-
Column::new_zero_indexed(next_line.len() as u32)
283-
),
284-
description: msg,
286+
let dmlerror = DMLStyleError {
287+
error: LocalDMLError {
288+
range: Range::new(
289+
Row::new_zero_indexed((i + 1) as u32),
290+
Row::new_zero_indexed((i + 1) as u32),
291+
Column::new_zero_indexed(0),
292+
Column::new_zero_indexed(next_line.len() as u32)
293+
),
294+
description: msg,
295+
},
296+
rule_name: Self::name().to_string(),
285297
};
286298
acc.push(dmlerror);
287299
}
@@ -357,15 +369,18 @@ impl IN9Rule {
357369
}
358370
}
359371
}
360-
pub fn check<'a>(&self, acc: &mut Vec<LocalDMLError>,
372+
pub fn check<'a>(&self, acc: &mut Vec<DMLStyleError>,
361373
args: Option<IN9Args<'a>>)
362374
{
363375
if !self.enabled { return; }
364376
let Some(args) = args else { return; };
365377
if self.indentation_is_not_aligned(args.case_range, *args.expected_depth) {
366-
let dmlerror = LocalDMLError {
367-
range: args.case_range,
368-
description: Self::description().to_string(),
378+
let dmlerror = DMLStyleError {
379+
error: LocalDMLError {
380+
range: args.case_range,
381+
description: Self::description().to_string(),
382+
},
383+
rule_name: Self::name().to_string(),
369384
};
370385
acc.push(dmlerror);
371386
}

0 commit comments

Comments
 (0)