fix(loc): C# drops a declaration under a brace-less for body from lloc

Open Beginner friendly
#1,887 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
rust
Domain
testing-qa

Research direction

Start with src/metrics/loc/csharp.rs and inspect the local_declaration_statement ancestor walk that skips declarations beneath a for. Compare its behavior with the JavaScript family fix referenced as #1283. Add a C# regression test for the reported brace-less for and switch input, then confirm lloc is 4 and remains 4 when braces surround the for body.

Written by the indexing model from the issue text.

Description

bug

A declaration that is the unbraced body of a for, or sits under one with no block between, is dropped from lloc in C#.

Reproduction

Inside class A { void F() { … } }, values are lloc of the unit.

Input Expected Actual
for (int i = 0; i < 2; i++) switch (i) { case 0: int y = 1; break; } 4 (for, switch, the declaration, break) 3
the same with braces around the for body 4 4

Cause

src/metrics/loc/csharp.rs skips a local_declaration_statement whose ancestor walk reaches a for before a block, to keep a for header's declaration from counting as a statement. In tree-sitter-c-sharp 0.23.5 a for initializer is a variable_declaration, never a local_declaration_statement, so the gate cannot fire for a header. It fires only for a declaration in the for body with no block between, where it drops a real statement.

This is the miss #1283 fixed for the JS family (js_family_braceless_for_body_declaration_counts). The same brace-less switch shape in Java also read 3; not investigated further.

Why it is filed from #1736

The escaped mutant replace == with != in <impl Loc for CsharpCode>::compute on that gate differs from the original only on inputs like the first row, where the original undercounts and the mutant reads 4. A test that kills it would pin the wrong value. It stays open on #1736 until this is fixed.

Dominant language
Rust
Stars
32
Forks
3
Avg merge
9h 55m
Merged PRs (30d)
102

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dekobon/big-code-analysis

All issues in dekobon/big-code-analysis

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.