Repository navigation
Fix SA1500 NRE on a top-level block at the start of a file - #4383
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4383 +/- ##
=======================================
Coverage 97.63% 97.63%
=======================================
Files 1345 1345
Lines 151674 151711 +37
Branches 5216 5216
=======================================
+ Hits 148082 148119 +37
Misses 2268 2268
Partials 1324 1324 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
🟢 Approval recommended
The narrow guards prevent invalid location access while preserving existing brace checks, with regression coverage for both affected paths.
No open issues 🎉
What changed in this PR
Fixes SA1500 crashing when a C# 9 top-level block begins a file, covering both diagnostic analysis and code-fix execution.
Changes:
- Guard previous-token location checks against
SyntaxKind.None. - Add regression tests for correctly formatted and line-sharing top-level opening braces.
| File | Description |
|---|---|
| test/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1500CSharp9UnitTests.cs | Adds analyzer and code-fix regression cases. |
| src/StyleCop.Analyzers/LayoutRules/SA1500BracesForMultiLineStatementsMustNotShareLine.cs | Skips location access for an absent previous token. |
| src/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1500CodeFixProvider.cs | Applies the same guard during brace fixes. |
Review effort: Balanced
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 22, 2026
Open
[DPDBT] Bump Microsoft.CodeAnalysis.Analyzers and 6 others
sergey-rybalkin/CommandLine.Generators#63
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.
Found by the fuzz harness (
tools/StyleCop.Analyzers.Fuzz,mutateover the extracted-literal corpus, random seed 87109).Bug: SA1500 throws a
NullReferenceException(AD0001) on a top-level-statement block{at the very start of a file (valid C# 9 code). The previous token is the default token: it is notIsMissing, but it has no syntax tree. The code fix had the same issue.Fix: skip the previous-token check when the token is
SyntaxKind.None, in both the analyzer and the code fix.Tests:
SA1500CSharp9UnitTests.TestTopLevelBlockAtStartOfFileAsyncandTestTopLevelBlockAtStartOfFileSharingLineAsync. Both failed before the fix.Analyzer cost: one extra
IsKindcheck per brace. Negligible.