Skip to content

Commit dfd44cc

Browse files
committed
Use enum RuleType to as identifier for Rules instead of name()
1 parent 581e2ad commit dfd44cc

File tree

4 files changed

+69
-20
lines changed

4 files changed

+69
-20
lines changed

src/lint/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::path::{Path, PathBuf};
44
use log::{debug, error, trace};
55
use serde::{Deserialize, Serialize};
66
use rules::indentation::IN2Rule;
7-
use rules::{Rule, instantiate_rules, CurrentRules};
7+
use rules::{Rule, instantiate_rules, CurrentRules, RuleType};
88
use rules::{spacing::{SpBraceOptions, SpPunctOptions, NspFunparOptions,
99
NspInparenOptions, NspUnaryOptions, NspTrailingOptions},
1010
indentation::{LongLineOptions, IN1Options, IN3Options,
@@ -93,7 +93,7 @@ impl Default for LintCfg {
9393

9494
pub struct DMLStyleError {
9595
pub error: LocalDMLError,
96-
pub rule_name: String,
96+
pub rule_type: RuleType,
9797
}
9898

9999
#[derive(Debug, Clone)]
@@ -155,15 +155,15 @@ pub fn begin_style_check(ast: TopAst, file: String, rules: &CurrentRules) -> Res
155155
fn post_process_linting_errors(errors: &mut Vec<DMLStyleError>) {
156156
// Collect in2 ranges
157157
let in2_ranges: Vec<_> = errors.iter()
158-
.filter(|style_err| style_err.rule_name == IN2Rule::name())
158+
.filter(|style_err| style_err.rule_type == RuleType::IN2)
159159
.map(|style_err| style_err.error.range)
160160
.collect();
161161

162162
// Remove linting errors that are in in2 rows
163163
errors.retain(|style_err| {
164164
!in2_ranges.iter().any(|range|
165165
(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())
166+
&& style_err.rule_type != RuleType::IN2)
167167
});
168168
}
169169

src/lint/rules/indentation.rs

Lines changed: 21 additions & 6 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, DMLStyleError};
11+
use crate::lint::{LintCfg, DMLStyleError, RuleType};
1212

1313
pub const MAX_LENGTH_DEFAULT: u32 = 80;
1414
pub const INDENTATION_LEVEL_DEFAULT: u32 = 4;
@@ -68,7 +68,7 @@ impl LongLinesRule {
6868
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, self.max_length, len),
6969
description: msg,
7070
},
71-
rule_name: Self::name().to_string(),
71+
rule_type: Self::get_rule_type(),
7272
};
7373
acc.push(dmlerror);
7474
}
@@ -81,6 +81,9 @@ impl Rule for LongLinesRule {
8181
fn description() -> &'static str {
8282
"Line length is above the threshold"
8383
}
84+
fn get_rule_type() -> RuleType {
85+
RuleType::LongLines
86+
}
8487
}
8588

8689
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
@@ -108,7 +111,7 @@ impl IN2Rule {
108111
range: Range::<ZeroIndexed>::from_u32(rowu32, rowu32, colu32, colu32 + 1),
109112
description: msg,
110113
},
111-
rule_name: Self::name().to_string(),
114+
rule_type: Self::get_rule_type(),
112115
};
113116
acc.push(dmlerror);
114117
}
@@ -121,6 +124,9 @@ impl Rule for IN2Rule {
121124
fn description() -> &'static str {
122125
"Tab characters (ASCII 9) should never be used to indent lines."
123126
}
127+
fn get_rule_type() -> RuleType {
128+
RuleType::IN2
129+
}
124130
}
125131

126132
pub struct IN3Rule {
@@ -208,7 +214,7 @@ impl IN3Rule {
208214
range: member_range,
209215
description: Self::description().to_string(),
210216
},
211-
rule_name: Self::name().to_string(),
217+
rule_type: Self::get_rule_type(),
212218
};
213219
acc.push(dmlerror);
214220
}
@@ -229,6 +235,9 @@ impl Rule for IN3Rule {
229235
"Previous line contains an openning brace and current line is not one\
230236
level of indentation ahead of past line"
231237
}
238+
fn get_rule_type() -> RuleType {
239+
RuleType::IN3
240+
}
232241
}
233242

234243
// IN6: Continuation Line
@@ -293,7 +302,7 @@ impl IN6Rule {
293302
),
294303
description: msg,
295304
},
296-
rule_name: Self::name().to_string(),
305+
rule_type: Self::get_rule_type(),
297306
};
298307
acc.push(dmlerror);
299308
}
@@ -312,6 +321,9 @@ impl Rule for IN6Rule {
312321
fn description() -> &'static str {
313322
"Continuation line not indented correctly."
314323
}
324+
fn get_rule_type() -> RuleType {
325+
RuleType::IN6
326+
}
315327
}
316328

317329
pub struct IN9Rule {
@@ -380,7 +392,7 @@ impl IN9Rule {
380392
range: args.case_range,
381393
description: Self::description().to_string(),
382394
},
383-
rule_name: Self::name().to_string(),
395+
rule_type: Self::get_rule_type(),
384396
};
385397
acc.push(dmlerror);
386398
}
@@ -401,4 +413,7 @@ impl Rule for IN9Rule {
401413
"Case labels are indented one level less than surrounding lines, \
402414
so that they are on the same level as the switch statement"
403415
}
416+
fn get_rule_type() -> RuleType {
417+
RuleType::IN9
418+
}
404419
}

src/lint/rules/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,20 @@ pub fn instantiate_rules(cfg: &LintCfg) -> CurrentRules {
4444
pub trait Rule {
4545
fn name() -> &'static str;
4646
fn description() -> &'static str;
47+
fn get_rule_type() -> RuleType;
48+
}
49+
50+
#[derive(PartialEq)]
51+
pub enum RuleType {
52+
SpBraces,
53+
SpPunct,
54+
NspFunpar,
55+
NspInparen,
56+
NspUnary,
57+
NspTrailing,
58+
LongLines,
59+
IN2,
60+
IN3,
61+
IN6,
62+
IN9
4763
}

src/lint/rules/spacing.rs

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::convert::TryInto;
33
use serde::{Deserialize, Serialize};
44
use crate::analysis::parsing::types::{BitfieldsContent, LayoutContent,
55
StructTypeContent};
6-
use crate::lint::{rules::Rule,
6+
use crate::lint::{rules::{Rule, RuleType},
77
DMLStyleError};
88
use crate::analysis::LocalDMLError;
99
use crate::analysis::parsing::tree::{TreeElement, ZeroRange};
@@ -109,7 +109,7 @@ impl SpBracesRule {
109109
range: location.lbrace,
110110
description: Self::description().to_string(),
111111
},
112-
rule_name: Self::name().to_string(),
112+
rule_type: Self::get_rule_type(),
113113
};
114114
acc.push(dmlerror);
115115
}
@@ -120,7 +120,7 @@ impl SpBracesRule {
120120
range: location.rbrace,
121121
description: Self::description().to_string(),
122122
},
123-
rule_name: Self::name().to_string(),
123+
rule_type: Self::get_rule_type(),
124124
};
125125
acc.push(dmlerror);
126126
}
@@ -135,6 +135,9 @@ impl Rule for SpBracesRule {
135135
fn description() -> &'static str {
136136
"Missing space around brace"
137137
}
138+
fn get_rule_type() -> RuleType {
139+
RuleType::SpBraces
140+
}
138141
}
139142

140143
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
@@ -256,7 +259,7 @@ impl SpPunctRule {
256259
range: error_range,
257260
description: Self::description().to_string(),
258261
},
259-
rule_name: Self::name().to_string(),
262+
rule_type: Self::get_rule_type(),
260263
};
261264
acc.push(dmlerror);
262265
}
@@ -274,7 +277,7 @@ impl SpPunctRule {
274277
range: error_range,
275278
description: Self::description().to_string(),
276279
},
277-
rule_name: Self::name().to_string(),
280+
rule_type: Self::get_rule_type(),
278281
};
279282
acc.push(dmlerror);
280283
}
@@ -290,6 +293,9 @@ impl Rule for SpPunctRule {
290293
fn description() -> &'static str {
291294
"Missing space after punctuation mark"
292295
}
296+
fn get_rule_type() -> RuleType {
297+
RuleType::SpPunct
298+
}
293299
}
294300
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
295301
pub struct NspFunparOptions {}
@@ -331,7 +337,7 @@ impl NspFunparRule {
331337
range: gap,
332338
description: Self::description().to_string(),
333339
},
334-
rule_name: Self::name().to_string(),
340+
rule_type: Self::get_rule_type(),
335341
};
336342
acc.push(dmlerror);
337343
}
@@ -344,6 +350,9 @@ impl Rule for NspFunparRule {
344350
fn description() -> &'static str {
345351
"There should be no space between a method/function name and its opening parenthesis."
346352
}
353+
fn get_rule_type() -> RuleType {
354+
RuleType::NspFunpar
355+
}
347356
}
348357

349358

@@ -428,7 +437,7 @@ impl NspInparenRule {
428437
range: gap,
429438
description: Self::description().to_string(),
430439
},
431-
rule_name: Self::name().to_string(),
440+
rule_type: Self::get_rule_type(),
432441
};
433442
acc.push(dmlerror);
434443
}
@@ -442,7 +451,7 @@ impl NspInparenRule {
442451
range: gap,
443452
description: Self::description().to_string(),
444453
},
445-
rule_name: Self::name().to_string(),
454+
rule_type: Self::get_rule_type(),
446455
};
447456
acc.push(dmlerror);
448457
}
@@ -456,6 +465,9 @@ impl Rule for NspInparenRule {
456465
fn description() -> &'static str {
457466
"There should be no space after opening or before closing () / []"
458467
}
468+
fn get_rule_type() -> RuleType {
469+
RuleType::NspInparen
470+
}
459471
}
460472

461473
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
@@ -497,7 +509,7 @@ impl NspUnaryRule {
497509
range: gap,
498510
description: Self::description().to_string(),
499511
},
500-
rule_name: Self::name().to_string(),
512+
rule_type: Self::get_rule_type(),
501513
};
502514
acc.push(dmlerror);
503515
}
@@ -510,6 +522,9 @@ impl Rule for NspUnaryRule {
510522
fn description() -> &'static str {
511523
"There should be no space between unary operator and its operand"
512524
}
525+
fn get_rule_type() -> RuleType {
526+
RuleType::NspUnary
527+
}
513528
}
514529

515530
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
@@ -533,7 +548,7 @@ impl NspTrailingRule {
533548
len),
534549
description: Self::description().to_string(),
535550
},
536-
rule_name: Self::name().to_string(),
551+
rule_type: Self::get_rule_type(),
537552
};
538553
acc.push(dmlerror);
539554
}
@@ -546,4 +561,7 @@ impl Rule for NspTrailingRule {
546561
fn description() -> &'static str {
547562
"Found trailing whitespace on row"
548563
}
564+
fn get_rule_type() -> RuleType {
565+
RuleType::NspTrailing
566+
}
549567
}

0 commit comments

Comments
 (0)