TODO section 33. Every rule states a Severity (error: would likely fail
to compile or elaborate, or silently mis-synthesize; warning: a probable
bug; style), printed in lucisv check text and JSON, mapped to
ERROR/WARNING/INFORMATION in the editor, grouped in --rules-doc, and
accepted by --select/--ignore and [lint] as a class word. VS Code gains
lucisv.lint.severity.{error,warning,style} and one tri-state
lucisv.lint.rules.<rule> per rule (default/on/off - a client sends
every key, so a boolean cannot say unset), generated by
vlog_linter::vscode with a drift test, and lint::enabled_rules is the
one documented ladder: defaults -> select -> class switches -> per-rule
-> ignore. Nineteen new rules (17 -> 36), one file each with true
positives and near-misses: Error - mismatched-labels,
duplicate-declaration (Semantics::duplicates(), additive),
undeclared-identifier (with a project; silent for macro text, included
fragments, hierarchical tails, any name another file mentions),
unknown-port, unknown-parameter; Warning - forbid-defparam,
invalid-system-task-function, truncated-numeric-literal,
undersized-binary-literal (safe fix), suspicious-semicolon,
packed-dimensions-range-ordering, module-port, module-parameter,
explicit-function-task-parameter-type, plusarg-assignment,
instance-shadowing, forbid-negative-array-dim, void-cast,
incomplete-assignment. Every false-positive class found on opentitan
closed with a named test; 3 383 tests / 48 binaries; corpus 814/820
and byte-identical (formatter untouched); vsix smoke OK.
docs/features/linter-expansion-1/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
163 lines
5.5 KiB
Rust
163 lines
5.5 KiB
Rust
//! `case-missing-default` — every case statement needs a `default` branch.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
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;
|
|
|
|
const NAME: &str = "case-missing-default";
|
|
|
|
impl Rule for CaseMissingDefault {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"give every case statement a default branch"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Warning
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A case statement with no `default` infers a latch in combinational \
|
|
logic and leaves the unlisted patterns unspecified in simulation — the \
|
|
classic source of a synthesis/simulation mismatch. Verible's style \
|
|
guide asks for an explicit `default`, even one that only assigns X or \
|
|
repeats the reset value, so the intent is written down rather than \
|
|
inferred. Fixing this is a design decision, so no automatic fix is \
|
|
offered. `unique`/`priority` case statements are flagged too: the \
|
|
qualifier is a promise to the tools, not a branch."
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
walk_all_stmts(ctx.file(), &mut |stmt| {
|
|
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"),
|
|
header_span(ctx.source(), stmt.span.start, expr.span.end),
|
|
));
|
|
}
|
|
}
|
|
});
|
|
walk_items(ctx.file(), &mut |item| {
|
|
if let ItemKind::GenCase(case) = &item.kind {
|
|
if !case.items.iter().any(GenCaseItem::is_default) {
|
|
out.push(Violation::new(
|
|
NAME,
|
|
"generate `case` has no `default` branch",
|
|
header_span(ctx.source(), item.span.start, case.expr.span.end),
|
|
));
|
|
}
|
|
}
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
/// 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 {
|
|
fn is_default(&self) -> bool;
|
|
}
|
|
|
|
impl IsDefault for CaseItem {
|
|
fn is_default(&self) -> bool {
|
|
self.labels.is_empty()
|
|
}
|
|
}
|
|
|
|
impl IsDefault for GenCaseItem {
|
|
fn is_default(&self) -> bool {
|
|
self.labels.is_empty()
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
CaseMissingDefault.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
fn wrap(body: &str) -> String {
|
|
format!("module m;\n always_comb begin\n{body} end\nendmodule\n")
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_case_without_default() {
|
|
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]
|
|
fn a_default_branch_satisfies_it() {
|
|
let src = wrap(" case (s)\n 0: x = 1;\n default: x = 0;\n endcase\n");
|
|
assert!(violations(&src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn casex_and_casez_count_too() {
|
|
let found = violations(&wrap(" casez (s)\n 2'b1?: x = 1;\n endcase\n"));
|
|
assert_eq!(found.len(), 1);
|
|
assert!(found[0].message.contains("`casez`"), "{}", found[0].message);
|
|
}
|
|
|
|
#[test]
|
|
fn a_qualifier_is_not_a_branch() {
|
|
let src = wrap(" unique case (s)\n 0: x = 1;\n endcase\n");
|
|
assert_eq!(violations(&src).len(), 1);
|
|
}
|
|
|
|
#[test]
|
|
fn generate_case_is_covered() {
|
|
let src = "module m;\n\
|
|
generate\n\
|
|
case (WIDTH)\n\
|
|
1: begin : g\n wire a;\n end\n\
|
|
endcase\n\
|
|
endgenerate\n\
|
|
endmodule\n";
|
|
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");
|
|
}
|
|
}
|