Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions scripts/check_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,15 @@ def is_executable_source_line(
return False
if text.startswith("Ok(Self") or text in {")}", "})"}:
return False
if text.endswith(",") and _has_executable_comma_syntax(text):
return True
if text in {"} else {", "else {", "));"} or text.startswith(
(".", "||", "&&", "/")
(".", "||", "&&")
):
return False
if text.startswith("*") and "=" not in text:
if text.startswith(("/", "*")) and not any(
character in text for character in "()="
):
return False
Comment thread
seonghobae marked this conversation as resolved.
Comment thread
seonghobae marked this conversation as resolved.
if text.endswith("=> event"):
return False
Expand All @@ -320,6 +324,12 @@ def is_executable_source_line(
return True


def _has_executable_comma_syntax(text: str) -> bool:
"""Return whether a comma line contains expression-only syntax."""

return any(character in text for character in ".()[]=+-*/%<>!&|?")
Comment thread
seonghobae marked this conversation as resolved.


def _is_structural_comma_continuation(
lines: list[str], line_number: int, text: str
) -> bool:
Expand Down Expand Up @@ -498,6 +508,7 @@ def _line_in_multiline_string(lines: list[str], line_number: int) -> bool:
)
return False


def _is_multiline_match_guard(lines: list[str], line_number: int) -> bool:
"""Recognize a guard continued onto the lines immediately before an arm."""

Expand Down
53 changes: 52 additions & 1 deletion tests/quality/test_check_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,16 +1277,62 @@ def test_line_filter_excludes_split_expression_regions(self) -> None:
with tempfile.TemporaryDirectory() as temporary:
source = Path(temporary) / "split.rs"
source.write_text("\n".join(lines) + "\n", encoding="utf-8")
for line_number in (1, 2, 5, 6, 9, 11, 12, 13):
for line_number in (1, 2, 6, 9, 11, 12, 13):
self.assertFalse(
coverage_contract.is_executable_source_line(
str(source), line_number
)
)
self.assertTrue(
coverage_contract.is_executable_source_line(str(source), 5)
)
self.assertTrue(
coverage_contract.is_executable_source_line(str(source), 17)
)

def test_line_filter_keeps_observable_nested_expressions(self) -> None:
"""Structural-depth handling cannot hide calls inside expressions."""

cases = {
"array_call": "fn f() {\n let x = [\n side_effect(),\n ];\n}\n",
"tuple_call": "fn f() {\n let x = (\n side_effect(),\n );\n}\n",
"struct_call": "fn f() {\n let x = Item {\n field: side_effect(),\n };\n}\n",
"division_call": "fn f(a: f64) {\n let x = a\n / denominator();\n}\n",
"multiplication_call": "fn f(a: f64) {\n let x = a\n * multiplier();\n}\n",
"array_block_call": "fn f() {\n let x = [\n { side_effect(); 1 },\n ];\n}\n",
"dot_call": "fn f() {\n let x = [value\n .side_effect(),\n ];\n}\n",
"dot_await": "fn f() {\n let x = [future\n .await,\n ];\n}\n",
"indexed_value": "fn f() {\n let x = [\n values[index],\n ];\n}\n",
"dereference": "fn f() {\n let x = [\n *pointer,\n ];\n}\n",
}
with tempfile.TemporaryDirectory() as temporary:
for name, source_text in cases.items():
with self.subTest(name=name):
source = Path(temporary) / f"{name}.rs"
source.write_text(source_text, encoding="utf-8")
self.assertTrue(
coverage_contract.is_executable_source_line(str(source), 3)
)

def test_lcov_keeps_uncovered_dot_call_in_authored_denominator(self) -> None:
"""An uncovered chained call cannot pass as structural punctuation."""

with tempfile.TemporaryDirectory() as temporary:
root = Path(temporary)
source = root / "src.rs"
source.write_text(
"fn f() {\n value\n .side_effect(),\n covered();\n}\n",
encoding="utf-8",
)
report = self.write_lcov(
temporary,
"SF:src.rs\nDA:3,0\nDA:4,1\nend_of_record\n",
)
self.assertEqual(
coverage_contract.load_lcov_line_totals(report, root),
{"lines": {"count": 2, "covered": 1}},
)

def test_line_filter_keeps_inline_functions_and_block_comment_followers(self) -> None:
"""Inline function bodies and code after quoted block comments stay visible."""

Expand Down Expand Up @@ -1484,6 +1530,11 @@ def test_multiline_string_empty_lines_returns_false(self) -> None:
def test_structural_comma_continuation_edge_cases(self) -> None:
"""Exercise structural comma continuation detection edge branches."""

self.assertFalse(
coverage_contract._is_structural_comma_continuation(
[], 1, ".side_effect(),"
)
)
with tempfile.TemporaryDirectory() as temporary:
source = Path(temporary) / "commas.rs"
# A comma-terminated line whose preceding lines are entirely blank
Expand Down