Repository navigation
SA1501: Handle code fixes at end of file - #4327
Merged
Merged
Conversation
Avoid dereferencing the missing next token when a single-line top-level statement ends at EOF, and cover the fix with a regression test. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
The narrow guard addresses the crash, preserves existing formatting behavior, and includes targeted regression coverage.
No open issues 🎉
What changed in this PR
Fixes an SA1501 code-fix crash when a statement ends the file.
Changes:
- Guards against a missing next token before accessing line information.
- Adds a C# 9 regression test for a top-level statement at end of file.
| File | Description |
|---|---|
| test/StyleCop.Analyzers.Test.CSharp9/LayoutRules/SA1501CSharp9UnitTests.cs | Adds end-of-file regression coverage. |
| src/StyleCop.Analyzers.CodeFixes/LayoutRules/SA1501CodeFixProvider.cs | Skips following-token formatting when no token exists. |
Review effort: Balanced
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #4327 +/- ##
==========================================
- Coverage 97.66% 97.66% -0.01%
==========================================
Files 1324 1324
Lines 146324 146348 +24
Branches 4945 4948 +3
==========================================
+ Hits 142904 142926 +22
Misses 2171 2171
- Partials 1249 1251 +2 🚀 New features to boost your workflow:
|
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 23, 2026
Open
Open
Closed
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 #987
The SA1501 code fix dereferenced the next token's line information even when the statement was the last token in the file. Guard the EOF token before checking the following statement's line. The regression test fails on master with a
NullReferenceExceptionand passes with this change.Validation: the SA1501 suite passed on the C# 6 and C# 15 test projects; focused SA1500, SA1505, and SA1515 tests also passed. No analyzer performance impact; this changes only code-fix handling.