Skip to main content

sqrust_rules/layout/
trailing_whitespace.rs

1use sqrust_core::{Diagnostic, FileContext, Rule};
2
3pub struct TrailingWhitespace;
4
5impl Rule for TrailingWhitespace {
6    fn name(&self) -> &'static str {
7        "Layout/TrailingWhitespace"
8    }
9
10    fn check(&self, ctx: &FileContext) -> Vec<Diagnostic> {
11        let mut diags = Vec::new();
12        for (line_num, line) in ctx.lines() {
13            let trimmed = line.trim_end();
14            if trimmed.len() < line.len() {
15                diags.push(Diagnostic {
16                    rule: self.name(),
17                    message: "Trailing whitespace".to_string(),
18                    line: line_num,
19                    col: trimmed.len() + 1,
20                });
21            }
22        }
23        diags
24    }
25
26    fn fix(&self, ctx: &FileContext) -> Option<String> {
27        let lines: Vec<&str> = ctx.source.lines().map(|l| l.trim_end()).collect();
28        let mut fixed = lines.join("\n");
29        if ctx.source.ends_with('\n') {
30            fixed.push('\n');
31        }
32        Some(fixed)
33    }
34}