Skip to content

Fix SA1500 NRE on a top-level block at the start of a file - #4383

Merged
AArnott merged 1 commit into
masterfrom
aarnott-fuzz-sa1500-top-level-block-nre
Oct 25, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-fuzz-sa1500-top-level-block-nre

Conversation

@AArnott

@AArnott AArnott commented Oct 25, 2026

Copy link
Copy Markdown
Contributor

Found by the fuzz harness (tools/StyleCop.Analyzers.Fuzz, mutate over 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 not IsMissing, 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.TestTopLevelBlockAtStartOfFileAsync and TestTopLevelBlockAtStartOfFileSharingLineAsync. Both failed before the fix.

Analyzer cost: one extra IsKind check per brace. Negligible.

@codecov

codecov Bot commented Oct 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.63%. Comparing base (46245d0) to head (5999765).

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AArnott
AArnott marked this pull request as ready for review October 25, 2026 14:39
Copilot AI balanced review requested due to automatic review settings October 25, 2026 14:39

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 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

@AArnott
AArnott merged commit c8c18b4 into master Oct 25, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-fuzz-sa1500-top-level-block-nre branch October 25, 2026 14:47
This was referenced Nov 11, 2026
This was referenced Nov 22, 2026
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.

2 participants