Skip to content

SA1501: Handle code fixes at end of file - #4327

Merged
AArnott merged 1 commit into
masterfrom
aarnott-review-getnexttoken-eof
Oct 17, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-review-getnexttoken-eof

Conversation

@AArnott

@AArnott AArnott commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

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 NullReferenceException and 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.

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>
@AArnott
AArnott marked this pull request as ready for review October 17, 2026 02:27
Copilot AI balanced review requested due to automatic review settings October 17, 2026 02:27

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 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

@AArnott
AArnott merged commit e5e3672 into master Oct 17, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-review-getnexttoken-eof branch October 17, 2026 02:35
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.66%. Comparing base (c1c052b) to head (2d797d0).
⚠️ Report is 4 commits behind head on master.

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:
  • ❄️ 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.

This was referenced Nov 11, 2026
This was referenced Nov 23, 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.

Review uses of GetNextToken at the end of a file

2 participants