Owner report: 'for loop inside generate shows expected item (found ;) at endgenerate;'. Lead diagnosis before dispatch: every generate-for spelling and three systemverilog.io examples already parsed; the failing token was the lone ';' - IEEE 1800-2017 A.1.4's null item, legal in module, generate, package, interface, program and class scope and at file scope, rejected by lucisv in all of them. ItemKind::Empty, one branch at the top of parse_item_kind, the three exhaustive arms topo 3.3 predicts and no others. The formatter keeps the ';' where it was written - glued to the previous sibling's line (endgenerate; end; endmodule; ;;) when no newline separates them and the ';' carries no docs of its own, else on a line of its own - decided from src spans in item_seq rather than a parser flag. verify.rs untouched, eight attack pairs. New empty-item lint (slang's -Wempty-member) with a safe fix whose deletion range structurally cannot swallow a comment. Corpus byte-identical at 813/820 against the pre-change binary; opentitan sweep identical file-for-file; empty-item fires zero times there. The LSP now publishes a warning with a quick fix where it used to publish a parse error. docs/features/null-items/README.md; TODO.md section 14. Co-Authored-By: Claude Fable 5 <[email protected]>
190 lines
6.7 KiB
Rust
190 lines
6.7 KiB
Rust
//! `empty-item` — no stray `;` where an item belongs.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::registry::Rule;
|
|
use crate::violation::{Applicability, Fix, Violation};
|
|
use crate::visit::walk_items;
|
|
use vlog_parser::ast::ItemKind;
|
|
use vlog_parser::span::TextRange;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct EmptyItem;
|
|
|
|
const NAME: &str = "empty-item";
|
|
|
|
impl Rule for EmptyItem {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"drop a `;` that stands where an item belongs"
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A lone `;` in a module, generate, package, interface, program or class \
|
|
body is a *null item* (IEEE 1800-2017 A.1.4). It is legal and it means \
|
|
nothing, so it is almost always a typo — the habit of ending a block \
|
|
with a semicolon, carried over from C or from a statement context: \
|
|
`endgenerate;`, `end;`, `endmodule;`. slang reports the same thing as \
|
|
`-Wempty-member` (\"an unnecessary semicolon is located in a \
|
|
non-procedural scope\"). The fix deletes the `;`, and the line with it \
|
|
when the line held nothing else; a comment written on that line is \
|
|
kept. The procedural null *statement* (`begin ; end`) is a different \
|
|
construct and is not reported here."
|
|
}
|
|
|
|
fn fix(&self) -> Option<Applicability> {
|
|
Some(Applicability::Safe)
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let src = ctx.source();
|
|
let mut out = Vec::new();
|
|
walk_items(ctx.file(), &mut |item| {
|
|
if !matches!(item.kind, ItemKind::Empty) {
|
|
return;
|
|
}
|
|
let mut violation =
|
|
Violation::new(NAME, "unnecessary `;`; a null item does nothing", item.span);
|
|
if let Some(range) = deletion(src, item.span) {
|
|
violation = violation.with_fix(Fix::safe(vec![(range, String::new())]));
|
|
}
|
|
out.push(violation);
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
/// The bytes to delete for the null item at `semi`: the `;`, the horizontal
|
|
/// whitespace before it on its line, and — when nothing but whitespace and
|
|
/// other null items shared that line — the line's newline too.
|
|
///
|
|
/// `None` when the range would cover anything but `;` and whitespace, which
|
|
/// cannot happen given how it is built and is checked anyway: a fix that
|
|
/// swallowed a comment would break a hard invariant, so the guard is cheaper
|
|
/// than the risk.
|
|
fn deletion(src: &str, semi: TextRange) -> Option<TextRange> {
|
|
let bytes = src.as_bytes();
|
|
let mut start = semi.start as usize;
|
|
while start > 0 && matches!(bytes[start - 1], b' ' | b'\t') {
|
|
start -= 1;
|
|
}
|
|
let mut end = semi.end as usize;
|
|
// The rest of the line: only whitespace means the line can go, but only if
|
|
// what stood before this `;` on it was whitespace and null items — a `;`
|
|
// written after real code leaves that code, and its line, alone.
|
|
let mut after = end;
|
|
while matches!(bytes.get(after), Some(b' ' | b'\t' | b'\r')) {
|
|
after += 1;
|
|
}
|
|
let line_is_ours = src[line_start(src, start)..start]
|
|
.bytes()
|
|
.all(|b| matches!(b, b' ' | b'\t' | b';' | b'\r'));
|
|
if line_is_ours {
|
|
match bytes.get(after) {
|
|
Some(b'\n') => end = after + 1,
|
|
// A last line with no newline of its own.
|
|
None => end = after,
|
|
_ => {}
|
|
}
|
|
}
|
|
let range = TextRange::new(start as u32, end as u32);
|
|
src.get(range.as_range())?
|
|
.bytes()
|
|
.all(|b| matches!(b, b';' | b' ' | b'\t' | b'\r' | b'\n'))
|
|
.then_some(range)
|
|
}
|
|
|
|
/// Byte offset of the start of the line `at` sits on.
|
|
fn line_start(src: &str, at: usize) -> usize {
|
|
src[..at].rfind('\n').map_or(0, |i| i + 1)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::violation::apply_fixes;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
EmptyItem.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
fn fix(src: &str) -> String {
|
|
apply_fixes(src, &violations(src), false).output
|
|
}
|
|
|
|
#[test]
|
|
fn the_owners_shape_is_reported_and_the_semicolon_alone_is_removed() {
|
|
let src = "module m;\n generate\n endgenerate;\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1);
|
|
assert_eq!(found[0].rule, NAME);
|
|
assert_eq!(&src[found[0].span.as_range()], ";");
|
|
assert_eq!(fix(src), "module m;\n generate\n endgenerate\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_null_item_on_its_own_line_takes_the_line_with_it() {
|
|
let src = "module m;\n wire a;\n ;\nendmodule\n";
|
|
assert_eq!(fix(src), "module m;\n wire a;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_on_the_line_survives_the_fix() {
|
|
let src = "module m;\n generate\n endgenerate; // note\nendmodule\n";
|
|
assert_eq!(fix(src), "module m;\n generate\n endgenerate // note\nendmodule\n");
|
|
|
|
let src = "module m;\n wire a;\n ; // why\nendmodule\n";
|
|
let fixed = fix(src);
|
|
assert!(fixed.contains("// why"), "the comment was dropped:\n{fixed}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_leading_comment_is_left_where_it_was() {
|
|
let src = "module m;\n // why\n ;\nendmodule\n";
|
|
assert_eq!(fix(src), "module m;\n // why\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn a_run_of_null_items_is_one_finding_each_and_the_line_goes() {
|
|
let src = "module m;\n wire a;\n ;;;\nendmodule\n";
|
|
assert_eq!(violations(src).len(), 3);
|
|
assert_eq!(fix(src), "module m;\n wire a;\nendmodule\n");
|
|
}
|
|
|
|
#[test]
|
|
fn every_item_scope_is_reached() {
|
|
let sources = [
|
|
"module m;\nendmodule;\n",
|
|
"module m;\n ;\nendmodule\n",
|
|
"interface ifc;\n ;\nendinterface\n",
|
|
"package p;\n ;\nendpackage\n",
|
|
"class c;\n ;\nendclass\n",
|
|
"module m;\n generate\n ;\n endgenerate\nendmodule\n",
|
|
"module m;\n for (genvar i = 0; i < 2; i++) begin : g\n ;\n end\nendmodule\n",
|
|
];
|
|
for src in sources {
|
|
assert_eq!(violations(src).len(), 1, "{src}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn the_procedural_null_statement_is_not_this_rule() {
|
|
assert!(violations("module m;\n initial begin\n ;\n end\nendmodule\n").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn endfunction_and_endtask_semicolons_are_not_reported() {
|
|
// They are recorded on the declaration, not as null items.
|
|
let src =
|
|
"module m;\n function int f();\n endfunction;\n task t();\n endtask;\nendmodule\n";
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_clean_file_is_clean() {
|
|
assert!(violations("module m;\n wire a;\nendmodule\n").is_empty());
|
|
}
|
|
}
|