Files
lucisv/crates/vlog_linter/tests/rules.rs
T
lucioandClaude Fable 5 c8b936a741 Add vfmt-server and linter-core (jointly: round 11)
vfmt-server (new crate, binary vfmt-server): LSP over stdio on
rust-analyzer's minimal stack (lsp-server/lsp-types/serde/serde_json,
exact-pinned, 18 packages audited incl. all build scripts). Ships
formatting (byte-identical to the CLI), tolerant-parse diagnostics
with UTF-16-correct positions (CRLF collapse pinned; EOF spans
widened to drawable lines), semanticTokens/full, hover with `define
expansion (file table -> workspace fallback), definition through
both resolution layers (Ambiguous -> all locations), references,
documentSymbol and capped workspace/symbol. Deferred capabilities
are not advertised. 860-file corpus session: 0.79s, 29.5MB. QA drove
a real stdio session: diagnostics clear on fix, staleness model
honored, malformed frames survived.

linter-core (new crate vlog_linter + vfmt check): ruff-style rules
with Safe/Unsafe fix applicability, inline waivers with unused-
waiver detection (Verible-spelling waivers for unimplemented rules
deliberately tolerated -- corpus-driven), seven seed rules, --fix /
--select / --ignore / --output-format json, frozen exit codes, bare
vfmt byte-identical to before (worktree-proven). Corpus baseline:
7806 violations in 93 files, deterministic. Lead-directed fix:
--fix never edits inside // vfmt: off fences (reported, not
auto-fixed), with a scan backstop for tolerant-parse trees where
fence::collect sees no regions.

Both dev agents were interrupted by a session limit mid-round and
resumed from transcripts; the workspace survived intact.

Dev: 2 Opus agents; QA: 2 Sonnet agents; reviews in
docs/features/{vfmt-server,linter-core}/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-19 09:15:54 -07:00

274 lines
8.1 KiB
Rust

//! Rule tests in Verible's `RunLintTestCases` style: the expected findings
//! are written into the source itself, and the match is **exact** — an
//! unexpected finding fails the test just as a missing one does.
//!
//! A line whose trimmed text is `//~ <rule-name>` asserts one finding of that
//! rule on the nearest preceding non-marker line. Marker lines are ordinary
//! comments to the linter, so the source stays valid Verilog.
//!
//! Every rule gets the same four cases: it fires, it stays quiet, a waiver
//! silences it, and a waiver that silences nothing is itself reported.
use std::path::PathBuf;
use vlog_linter::{default_rules, run};
/// Asserts that linting `src` (as `path`) finds exactly what its markers say.
#[track_caller]
fn assert_lint(path: Option<&str>, src: &str) {
let violations = run(src, path.map(PathBuf::from), &default_rules());
let index = vlog_parser::LineIndex::new(src);
let mut actual: Vec<(u32, String)> =
violations.iter().map(|v| (index.line_col(v.span.start).0, v.rule.to_string())).collect();
let mut expected = expectations(src);
actual.sort();
expected.sort();
assert_eq!(
expected, actual,
"\nexpected (line, rule) findings on the left, got the right\nsource:\n{src}\nmessages: {:#?}",
violations.iter().map(|v| format!("{}: {}", v.rule, v.message)).collect::<Vec<_>>()
);
}
/// The `(line, rule)` pairs the markers in `src` ask for.
fn expectations(src: &str) -> Vec<(u32, String)> {
let mut out = Vec::new();
let mut code_line = 0u32;
for (i, text) in src.lines().enumerate() {
match text.trim().strip_prefix("//~") {
Some(rule) => out.push((code_line, rule.trim().to_string())),
None => code_line = i as u32 + 1,
}
}
out
}
// ---------------------------------------------------------------- harness
#[test]
fn the_harness_notices_a_missing_marker() {
let src = "module m;\n\twire a;\nendmodule\n";
let violations = run(src, None, &default_rules());
assert_eq!(violations.len(), 1, "fixture must violate exactly one rule");
assert!(expectations(src).is_empty(), "and the unmarked source must expect none");
}
// ---------------------------------------------------------------- no-tabs
#[test]
fn no_tabs_fires() {
assert_lint(None, "module m;\n\twire a;\n//~ no-tabs\nendmodule\n");
}
#[test]
fn no_tabs_is_quiet_on_spaces() {
assert_lint(None, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn no_tabs_is_waived() {
assert_lint(None, "module m;\n// vfmt: waive no-tabs\n\twire a;\nendmodule\n");
assert_lint(None, "module m;\n\twire a; // vfmt: waive no-tabs\nendmodule\n");
}
#[test]
fn no_tabs_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n wire a; // vfmt: waive no-tabs\n//~ unused-waiver\nendmodule\n",
);
}
// ------------------------------------------------- no-trailing-whitespace
#[test]
fn no_trailing_whitespace_fires() {
assert_lint(None, "module m;\n wire a; \n//~ no-trailing-whitespace\nendmodule\n");
}
#[test]
fn no_trailing_whitespace_is_quiet_on_a_clean_line() {
assert_lint(None, "module m;\n wire a;\nendmodule\n");
}
#[test]
fn no_trailing_whitespace_is_waived() {
assert_lint(None, "module m;\n// vfmt: waive no-trailing-whitespace\n wire a; \nendmodule\n");
}
#[test]
fn no_trailing_whitespace_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n wire a; // vfmt: waive no-trailing-whitespace\n//~ unused-waiver\nendmodule\n",
);
}
// ----------------------------------------------------- case-missing-default
#[test]
fn case_missing_default_fires() {
assert_lint(
None,
"module m;\n always_comb begin\n case (s)\n//~ case-missing-default\n 0: x = 1;\n endcase\n end\nendmodule\n",
);
}
#[test]
fn case_missing_default_is_quiet_with_a_default() {
assert_lint(
None,
"module m;\n always_comb begin\n case (s)\n 0: x = 1;\n default: x = 0;\n endcase\n end\nendmodule\n",
);
}
#[test]
fn case_missing_default_is_waived() {
assert_lint(
None,
"module m;\n always_comb begin\n // vfmt: waive case-missing-default\n case (s)\n 0: x = 1;\n endcase\n end\nendmodule\n",
);
}
#[test]
fn case_missing_default_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n always_comb begin\n case (s)\n 0: x = 1;\n default: x = 0;\n endcase // vfmt: waive case-missing-default\n//~ unused-waiver\n end\nendmodule\n",
);
}
// -------------------------------------------------- always-ff-non-blocking
#[test]
fn always_ff_non_blocking_fires() {
assert_lint(
None,
"module m;\n always_ff @(posedge clk) q = d;\n//~ always-ff-non-blocking\nendmodule\n",
);
}
#[test]
fn always_ff_non_blocking_is_quiet_on_nonblocking() {
assert_lint(None, "module m;\n always_ff @(posedge clk) q <= d;\nendmodule\n");
}
#[test]
fn always_ff_non_blocking_is_waived() {
assert_lint(
None,
"module m;\n always_ff @(posedge clk) q = d; // vfmt: waive always-ff-non-blocking\nendmodule\n",
);
}
#[test]
fn always_ff_non_blocking_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n always_ff @(posedge clk) q <= d; // vfmt: waive always-ff-non-blocking\n//~ unused-waiver\nendmodule\n",
);
}
// ---------------------------------------------------- always-comb-blocking
#[test]
fn always_comb_blocking_fires() {
assert_lint(None, "module m;\n always_comb x <= y;\n//~ always-comb-blocking\nendmodule\n");
}
#[test]
fn always_comb_blocking_is_quiet_on_blocking() {
assert_lint(None, "module m;\n always_comb x = y;\nendmodule\n");
}
#[test]
fn always_comb_blocking_is_waived() {
assert_lint(
None,
"module m;\n// verilog_lint: waive always-comb-blocking\n always_comb x <= y;\nendmodule\n",
);
}
#[test]
fn always_comb_blocking_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n always_comb x = y; // vfmt: waive always-comb-blocking\n//~ unused-waiver\nendmodule\n",
);
}
// -------------------------------------- explicit-parameter-storage-type
#[test]
fn explicit_parameter_storage_type_fires() {
assert_lint(
None,
"module m;\n parameter WIDTH = 8;\n//~ explicit-parameter-storage-type\nendmodule\n",
);
}
#[test]
fn explicit_parameter_storage_type_is_quiet_when_typed() {
assert_lint(None, "module m;\n parameter int WIDTH = 8;\nendmodule\n");
}
#[test]
fn explicit_parameter_storage_type_is_waived() {
assert_lint(
None,
"module m;\n parameter WIDTH = 8; // vfmt: waive explicit-parameter-storage-type\nendmodule\n",
);
}
#[test]
fn explicit_parameter_storage_type_waiver_that_waives_nothing_is_reported() {
assert_lint(
None,
"module m;\n parameter int WIDTH = 8; // vfmt: waive explicit-parameter-storage-type\n//~ unused-waiver\nendmodule\n",
);
}
// -------------------------------------------------------- module-filename
#[test]
fn module_filename_fires() {
assert_lint(Some("rtl/bar.sv"), "module foo;\n//~ module-filename\nendmodule\n");
}
#[test]
fn module_filename_is_quiet_when_it_matches_or_has_no_path() {
assert_lint(Some("rtl/foo.sv"), "module foo;\nendmodule\n");
assert_lint(None, "module foo;\nendmodule\n");
}
#[test]
fn module_filename_is_waived() {
assert_lint(Some("rtl/bar.sv"), "module foo; // vfmt: waive module-filename\nendmodule\n");
}
#[test]
fn module_filename_waiver_that_waives_nothing_is_reported() {
assert_lint(
Some("rtl/foo.sv"),
"module foo; // vfmt: waive module-filename\n//~ unused-waiver\nendmodule\n",
);
}
// ------------------------------------------------------------- mixed file
#[test]
fn several_rules_on_one_file_report_independently() {
assert_lint(
Some("rtl/top.sv"),
"module foo #(parameter W = 1) ();\n\
//~ module-filename\n\
//~ explicit-parameter-storage-type\n\
\talways_comb x <= 1;\n\
//~ no-tabs\n\
//~ always-comb-blocking\n\
endmodule\n",
);
}