Add cop static-analysis rules for the C# emitter#10909
Open
JoshLove-msft wants to merge 6 commits into
Open
Conversation
Add cop checks scoped to the C# generator: - braces.cop: control-flow statements (if/else/for/foreach/while) must use braces - snippet-factory.cop: expression types must be created via the Snippet factory (see microsoft#3724) main.cop wires the per-rule predicates to the codebase and exposes one command per rule (CHECK-BRACES, CHECK-SNIPPETS). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
commit: |
Contributor
|
No changes needing a change description found. |
Apply the cop checks to the generator: - Add braces to inline guard clauses in PartialMethodCustomization. - Use Snippet factory helpers instead of expression constructors: Literal, TypeOf, New.Instance, Default, and receiver.Invoke(...). Tighten snippet-factory.cop to remove false positives: - KeywordExpression only flags null/this/default (yield/break have no Snippet). - InvokeMethodExpression only flags calls with a non-null instance receiver (unqualified `new InvokeMethodExpression(null, ...)` has no Snippet equivalent). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add Invoke-Cop.ps1 which downloads a pinned Agent Cop release and runs the cop-checks rules against the generator, failing on any violation. Wire it into the unit-test CI job (Test-Packages.ps1) and add an `npm run cop` convenience script. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jorgerangel-msft
approved these changes
Jun 5, 2026
…ilures The cop provider packages (code, csharp) are auto-restored from the GitHub feed on first run, which can fail transiently on CI agents (rate limiting / network blips). Retry the check run with backoff when a restore/provider failure is detected, while still reporting genuine rule violations immediately. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
jorgerangel-msft
approved these changes
Jun 5, 2026
The cop provider packages (code, csharp) auto-restored from the GitHub feed are version-locked to the cop runtime and always track the latest upstream build. Pinning the binary to an older release tag caused the provider to fail to load (assembly load error) once the feed moved ahead. Default to the latest release (via the releases/latest/download redirect, no API call) so the binary stays in lockstep with the providers, and only retry genuinely transient feed errors -- not version-mismatch load failures. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…te limit cop resolves its feed packages (code, csharp) via the GitHub REST API, which is rate-limited to 60 req/hour for unauthenticated callers. On shared CI agents that budget is exhausted, so auto-restore fails with "not found in any configured feed" and the csharp provider never loads. Fork PRs have no secrets, so a GITHUB_TOKEN cannot be supplied to raise the limit. Seed the two imported packages directly from the cop repo at the tag matching the downloaded binary (read from cop -v) via a sparse, blob-filtered git clone (git protocol, not the REST API; fetches only those two directories). cop then finds them in its cache and never calls the rate-limited feed. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds cop static-analysis rules scoped to the C# emitter generator, fixes the existing violations, and wires the checks into CI.
Rules
braces.cop— control-flow statements (if/else/else if/for/foreach/while) must use{ ... }blocks; flags inline unbraced bodies.snippet-factory.cop— expression types must be created through theSnippetfactory rather than their constructors (see Create an analyzer for MGC to ensure some APIs are not misused #3724). Excludes theSnippets/andExpressions/layers and test code. Only constructors with a clear factory equivalent are flagged (e.g.KeywordExpressionis limited tonull/this/default;InvokeMethodExpressiononly when it has a non-null instance receiver).main.copwires the per-rule predicates to the C# codebase and exposes one command per rule (CHECK-BRACES,CHECK-SNIPPETS).Fixes
Resolved all violations the rules surface: added braces to inline guard clauses and replaced expression constructors with
Snippethelpers (Literal,TypeOf,New.Instance,Default,recv.Invoke(...),Static(t).Invoke(...)).CI
eng/scripts/Invoke-Cop.ps1downloads a pinned cop release and runs the checks against the generator, failing on any violation. It is invoked from the unit-test CI job (Test-Packages.ps1). Locally:npm run cop.Run locally
Run a single rule with
-c CHECK-BRACESor-c CHECK-SNIPPETS.