Repository navigation
SA1008: allow whitespace before comments after opening parentheses - #4214
Merged
Merged
Conversation
Reproduce #3945 on master: the exact repro and four whitespace/comment variants fail with SA1008; three near-miss code-fix cases and two unspaced comment cases pass. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Exempt immediately following single-line and block comments from SA1008's trailing-whitespace diagnostic. Inspect only the second trailing trivia after the existing whitespace guard, preserving ordinary whitespace diagnostics and avoiding scans or allocations. Document the exemption. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4214 +/- ##
=======================================
Coverage 97.57% 97.57%
=======================================
Files 1276 1276
Lines 135692 135738 +46
Branches 4383 4384 +1
=======================================
+ Hits 132397 132445 +48
+ Misses 2135 2134 -1
+ Partials 1160 1159 -1 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
The focused implementation matches the documented behavior and includes comprehensive regression coverage.
0 open findings
What changed in this PR
Updates SA1008 to permit whitespace immediately before comments following opening parentheses.
Changes:
- Exempts single-line and block comments from SA1008.
- Adds valid and near-miss regression coverage.
- Documents the new exemption.
| File | Description |
|---|---|
SA1008OpeningParenthesisMustBeSpacedCorrectly.cs |
Implements the comment exemption. |
SA1008UnitTests.cs |
Adds regression and code-fix tests. |
documentation/SA1008.md |
Documents the permitted spacing. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This was referenced Nov 11, 2026
Bump StyleCop.Analyzers from 1.2.0-beta.556 to 1.3.180
microsoftgraph/aspnetcore-webhooks-sample#436
Open
Closed
Open
This was referenced Nov 20, 2026
Open
Open
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.
Fixes #3945
SA1008 incorrectly reports the opening parenthesis in
!( // commentbecause it treats whitespace before a comment like whitespace before code. Allow whitespace immediately before single-line and block comments after opening parentheses, and document this exemption. Leading-parenthesis spacing is unchanged; whitespace before code or before a newline followed by a later comment remains reported.Test-first evidence
Commit d680fa1 adds the exact minimal valid repro and related coverage before the analyzer change. Against master 0931d70, the exact repro fails with one unexpected SA1008 at line 5, column 17 (
Opening parenthesis should not be followed by a space.). Four additional whitespace/comment variants fail too; two unspaced comment cases and three near-miss code-fix cases pass. Tests cover spaces/tabs, single-line/block comments, parameter/argument lists, nested expressions, comments after code, and comments on the next line. The near-miss tests verify both individual fixes and Fix All while preserving comments.Validation
Using the SDK selected by global.json (10.0.401 via latestFeature): C#6 baseline build and CSharp15 Debug build succeeded with zero warnings and zero errors. The highest-project build also builds all inherited projects and both Windows target frameworks. All ten SA1008 suites passed sequentially on net472 for C#6–15: 50, 65, 71, 81, 87, 88, 94, 94, 99, and 99 tests (828 total, zero failures or skips).
Performance impact
No additional registrations, token/tree scans, semantic-model work, LINQ, or allocations. Only opening parentheses for which the existing whitespace guard would report inspect the trailing-trivia count and index 1 (at most two SyntaxKind checks). Other tokens and opening parentheses without following whitespace perform the same work as before. Product code remains compatible with Roslyn 1.2.1.
Kept as a draft for CI and human review; not merged.