svlint/opt/rustwide/workdir/src/textrules/
style_semicolon.rs1use crate::config::ConfigOption;
2use crate::linter::{TextRule, TextRuleEvent, TextRuleResult};
3use regex::Regex;
4
5#[derive(Default)]
6pub struct StyleSemicolon {
7 re: Option<Regex>,
8}
9
10impl TextRule for StyleSemicolon {
11 fn check(
12 &mut self,
13 event: TextRuleEvent,
14 _option: &ConfigOption,
15 ) -> TextRuleResult {
16 let line: &str = match event {
17 TextRuleEvent::StartOfFile => {
18 return TextRuleResult::Pass;
19 }
20 TextRuleEvent::Line(x) => x,
21 };
22
23 if self.re.is_none() {
24 self.re = Some(Regex::new("([ ]+);").unwrap());
25 }
26 let re = self.re.as_ref().unwrap();
27
28 if let Some(caps) = re.captures(line) {
29 if let Some(m) = caps.get(1) {
30 TextRuleResult::Fail {
31 offset: 0,
32 len: m.as_str().chars().count(),
33 }
34 } else {
35 TextRuleResult::Pass
36 }
37 } else {
38 TextRuleResult::Pass
39 }
40 }
41
42 fn name(&self) -> String {
43 String::from("style_semicolon")
44 }
45
46 fn hint(&self, _option: &ConfigOption) -> String {
47 String::from(format!("Remove whitespace preceeding semicolon."))
48 }
49
50 fn reason(&self) -> String {
51 String::from("Whitespace before a semicolon may obfuscate the statement.")
52 }
53}