Skip to main content

sqrust_rules/layout/
trailing_newline.rs

1use sqrust_core::{Diagnostic, FileContext, Rule};
2
3pub struct TrailingNewline;
4
5impl Rule for TrailingNewline {
6    fn name(&self) -> &'static str {
7        "Layout/TrailingNewline"
8    }
9
10    fn check(&self, ctx: &FileContext) -> Vec<Diagnostic> {
11        // Empty file — no violation
12        if ctx.source.is_empty() {
13            return Vec::new();
14        }
15        // File already ends with a newline — no violation
16        if ctx.source.ends_with('\n') {
17            return Vec::new();
18        }
19        // Missing trailing newline — emit one diagnostic
20        let line_count = ctx.source.lines().count();
21        let last_line_len = ctx.source.lines().last().map(|l| l.len()).unwrap_or(0);
22        vec![Diagnostic {
23            rule: self.name(),
24            message: "File must end with a newline".to_string(),
25            line: line_count,
26            col: last_line_len + 1,
27        }]
28    }
29}