Skip to content

feat(rules): add C# review rules - #1840

Merged
lizhengfeng101 merged 4 commits into
alibaba:mainfrom
abhisheksharma2411:feat/1612-csharp-rules
Nov 4, 2026
Merged

lizhengfeng101 merged 4 commits into
alibaba:mainfrom
abhisheksharma2411:feat/1612-csharp-rules

Conversation

@abhisheksharma2411

Copy link
Copy Markdown
Contributor

Description

.cs and .vb are already in the extension allowlist, so C# files are reviewed
today — but path_rule_map has no entry for them, so every C# file falls through
to default.md and gets the generic prompt. F# already has fsharp.md, so .NET
is half covered.

This adds csharp.md and maps **/*.cs to it, in the same shape as the existing
language docs: a precision preamble that defers formatting to dotnet format and
analyzers, then six sections of defect classes that are specific to C# and likely
to change behaviour — nullability and the null-forgiving operator, disposal,
async/cancellation, LINQ deferred execution and multiple enumeration, equality
and value semantics, and interop/serialization/trust boundaries.

Scope note: I mapped only **/*.cs. .vb is in the allowlist too, but VB.NET is
a different language and most of the examples here would not read correctly for
it, so I would rather leave it than map it to a doc written for C#. Happy to add
vbnet.md separately if you want it.

Type of Change

  • New feature (non-breaking change that adds functionality)

How Has This Been Tested?

  • make test passes locally
  • Manual testing (describe below)

go test ./internal/config/... passes; go fmt and go vet are clean.

I checked the mapping is load-bearing rather than assuming it: removing the
**/*.cs line makes the two new rows in system_rules_test.go fail with
Resolve(...) returning default.md's "#### Correctness" text, which is exactly
the behaviour reported in #1612. It also trips the repo's own
TestSystemRulesIntegrity/no_orphan_files, which confirms doc and mapping stay
consistent.

Checklist

  • My code follows the project's coding style (go fmt, go vet)
  • I have performed a self-review of my code
  • I have added tests that prove my fix is effective or my feature works
  • New and existing unit tests pass locally with my changes
  • I have updated the documentation accordingly (if applicable)
  • I have signed the CLA
  • I did not use AI/LLM to create this PR, or I disclosed the tool/model below and reviewed its output; I did not attribute commits to AI and will answer maintainer questions and review comments myself without AI/LLM.

AI/LLM disclosure: AI-assisted (Claude). I reviewed the rule content and ran the
tests myself.

Related Issues

Closes #1612

Note: PR #1616 attempted this and was closed on 2026-10-20 only because the CLA
was never signed, not on merit. I have signed it.

Fixes alibaba#1612

Signed-off-by: Abhishek Sharma <[email protected]>
@github-actions

Copy link
Copy Markdown
Contributor

✅ OpenCodeReview: Review complete: 0 finding(s) across 1 selected item(s).

@Qiyuanqiii Qiyuanqiii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding dedicated C# review rules. The mapping and regression tests look sound, and the focused scope makes sense.

After reviewing the rule text against Microsoft's official C#/.NET documentation, I found two areas where a small wording adjustment could help avoid false-positive findings:

  1. Resource ownership and disposal (csharp.md, L10–L12): The current wording doesn't explicitly account for DI-managed dependencies or transferred ownership. In ASP.NET Core, a service receiving a scoped DbContext shouldn't dispose it itself. Also, an owning type may legitimately implement only IAsyncDisposable, without implementing IDisposable.

    I suggest limiting missing-disposal findings to resources actually owned by the code under review, and recognizing IDisposable, IAsyncDisposable, and explicit ownership transfer as valid lifetime-management mechanisms.

    Reference: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection/guidelines

  2. Mutable structs (csharp.md, L30): Copying and independently mutating a value-type instance is normal C# behavior and doesn't necessarily indicate a defect. This rule would be more precise if it required evidence that value copying or defensive copies cause unintended behavior or lost mutations, rather than treating mutability itself as sufficient.

    Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types

These are rule-precision suggestions

Narrow two rules so they do not fire on correct code:

- disposal now applies only to resources the reviewed code owns, so an
  injected scoped DbContext the container disposes is not a finding, and
  IAsyncDisposable alone is accepted as a lifetime mechanism
- the mutable-struct rule now needs evidence that copying loses a write,
  rather than treating mutability as sufficient

Signed-off-by: Abhishek Sharma <[email protected]>
@abhisheksharma2411

abhisheksharma2411 commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for adding dedicated C# review rules. The mapping and regression tests look sound, and the focused scope makes sense.

After reviewing the rule text against Microsoft's official C#/.NET documentation, I found two areas where a small wording adjustment could help avoid false-positive findings:

  1. Resource ownership and disposal (csharp.md, L10–L12): The current wording doesn't explicitly account for DI-managed dependencies or transferred ownership. In ASP.NET Core, a service receiving a scoped DbContext shouldn't dispose it itself. Also, an owning type may legitimately implement only IAsyncDisposable, without implementing IDisposable.
    I suggest limiting missing-disposal findings to resources actually owned by the code under review, and recognizing IDisposable, IAsyncDisposable, and explicit ownership transfer as valid lifetime-management mechanisms.
    Reference: https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection/guidelines
  2. Mutable structs (csharp.md, L30): Copying and independently mutating a value-type instance is normal C# behavior and doesn't necessarily indicate a defect. This rule would be more precise if it required evidence that value copying or defensive copies cause unintended behavior or lost mutations, rather than treating mutability itself as sufficient.
    Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-types

These are rule-precision suggestions

Both applied and pushed.

Disposal now only covers resources this code owns, so a scoped DbContext the container injects and disposes isn't a finding any more. And a type needs either IDisposable or IAsyncDisposable, not specifically IDisposable — the old wording implied IAsyncDisposable alone wasn't enough.

The mutable-struct rule now needs evidence that copying actually loses a write, through a readonly field, an in parameter, or an indexer returning a copy. Copying and mutating a value type on its own no longer gets reported.

Thanks for the references, both were fair.

@Qiyuanqiii Qiyuanqiii left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

@wu21-web wu21-web left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When can people remember to update the documentation.

The rule table in review-rules.md documents path_rule_map in matching
order, and the new **/*.cs entry was missing from it. Added in all five
locales, immediately after the F# row, which is where it sits in
system_rules.json.

The F# addition in alibaba#1448 is the shape this follows: the rule doc, the
map entry, the tests, and the same row in en, ja, ko, ru and zh.

Signed-off-by: Abhishek Sharma <[email protected]>
@abhisheksharma2411

Copy link
Copy Markdown
Contributor Author

Added the **/*.cs row to all five locales in 6aa6d4c. Each one sits right after the F# row, just like in system_rules.json — the table shows matching order, so placement is key.

While checking things out, I cross-checked path_rule_map with the table and found eight more that were never documented: pug, nix, haskell, nim, swift, zig, ocaml, and reason. So this isn't a one-off thing, the docs have been drifting for a while.

Opened #1857 with those eight rows plus a test that compares the map against every locale's table in both directions. If you revert the doc files, the test fails and calls out the exact missing patterns per locale. That seemed way better than just adding the rows and promising to remember next time — people forget, so the test handles it instead.

One quick note on that PR: the non-English rows are mine and they're super basic and factual. If the Chinese wording sounds a bit weird, feel free to just overwrite it or let me know and I'll fix it.

@lizhengfeng101 lizhengfeng101 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lizhengfeng101
lizhengfeng101 merged commit 8041473 into alibaba:main Nov 4, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation rules for the C# .NET language.

4 participants