case-missing-default: underline the case header, not the whole block
The violation span now runs from the statement's first token through the parenthesis closing the subject, so a missing default no longer paints every branch red in the editor. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
5ffafc505e
commit
fda6d20d9f
1 file changed
+29
-4
@@ -5,6 +5,7 @@ use crate::registry::Rule;
|
||||
use crate::violation::Violation;
|
||||
use crate::visit::{walk_all_stmts, walk_items};
|
||||
use vlog_parser::ast::{CaseItem, GenCaseItem, ItemKind, StmtKind};
|
||||
use vlog_parser::span::TextRange;
|
||||
|
||||
/// See [`Rule::doc`].
|
||||
pub struct CaseMissingDefault;
|
||||
@@ -34,12 +35,12 @@ impl Rule for CaseMissingDefault {
|
||||
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
||||
let mut out = Vec::new();
|
||||
walk_all_stmts(ctx.file(), &mut |stmt| {
|
||||
if let StmtKind::Case { kw, items, .. } = &stmt.kind {
|
||||
if let StmtKind::Case { kw, expr, items, .. } = &stmt.kind {
|
||||
if !items.iter().any(CaseItem::is_default) {
|
||||
out.push(Violation::new(
|
||||
NAME,
|
||||
format!("`{kw}` statement has no `default` branch"),
|
||||
stmt.span,
|
||||
header_span(ctx.source(), stmt.span.start, expr.span.end),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -50,7 +51,7 @@ impl Rule for CaseMissingDefault {
|
||||
out.push(Violation::new(
|
||||
NAME,
|
||||
"generate `case` has no `default` branch",
|
||||
item.span,
|
||||
header_span(ctx.source(), item.span.start, case.expr.span.end),
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -59,6 +60,18 @@ impl Rule for CaseMissingDefault {
|
||||
}
|
||||
}
|
||||
|
||||
/// The `case (subject)` header alone: from the statement's first token through
|
||||
/// the parenthesis that closes the subject. Underlining the whole block would
|
||||
/// paint every branch red for a problem that lives on the header line.
|
||||
fn header_span(source: &str, start: u32, expr_end: u32) -> TextRange {
|
||||
let close = source
|
||||
.get(expr_end as usize..)
|
||||
.and_then(|rest| rest.find(')'))
|
||||
.map(|i| expr_end + i as u32 + 1)
|
||||
.unwrap_or(expr_end);
|
||||
TextRange::new(start, close)
|
||||
}
|
||||
|
||||
/// A case item with no labels is the `default` one — the AST spells it that
|
||||
/// way for both statement and generate cases.
|
||||
trait IsDefault {
|
||||
@@ -91,10 +104,21 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn flags_a_case_without_default() {
|
||||
let found = violations(&wrap(" case (s)\n 0: x = 1;\n endcase\n"));
|
||||
let src = wrap(" case (s)\n 0: x = 1;\n endcase\n");
|
||||
let found = violations(&src);
|
||||
assert_eq!(found.len(), 1);
|
||||
assert!(found[0].message.contains("`case`"), "{}", found[0].message);
|
||||
assert!(found[0].fix.is_none(), "adding a default is a design decision");
|
||||
let underlined = &src[found[0].span.as_range()];
|
||||
assert_eq!(underlined, "case (s)", "the header, not the whole block");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_qualifier_stays_inside_the_underline() {
|
||||
let src = wrap(" unique case (s)\n 0: x = 1;\n endcase\n");
|
||||
let found = violations(&src);
|
||||
assert_eq!(found.len(), 1);
|
||||
assert_eq!(&src[found[0].span.as_range()], "unique case (s)");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -128,5 +152,6 @@ mod tests {
|
||||
let found = violations(src);
|
||||
assert_eq!(found.len(), 1, "{found:?}");
|
||||
assert!(found[0].message.contains("generate"), "{}", found[0].message);
|
||||
assert_eq!(&src[found[0].span.as_range()], "case (WIDTH)", "header only");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user