Skip to content

SX1412: Add UTF-8 without BOM rule - #4315

Merged
AArnott merged 2 commits into
masterfrom
aarnott-sa1412x-utf8-no-bom
Oct 16, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-sa1412x-utf8-no-bom

Conversation

@AArnott

@AArnott AArnott commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #2100

Adds the opt-in alternative rule SX1412 for projects that require UTF-8 source files without a byte order mark. It complements SA1412, reports files with a different or unknown encoding and files with a UTF-8 BOM, and provides single-document and Fix All code fixes. The analyzer is disabled by default.

Adds SX1412 to the alternative rules catalog, documents its conflict with SA1412, and adds Czech, Russian, and Simplified Chinese rule documentation. New resource strings are translated into Russian and Simplified Chinese; Czech falls back to English because this resource family has no Czech localization.

Validation: SX1412 tests pass in the C# 6 and C# 15 test projects (5 tests each). The analyzer and code-fix projects build with zero warnings. The analyzer checks the source tree's encoding without syntax-node or semantic-model analysis.

This adds a new rule rather than fixing a behavior that could fail on master, so the new-rule tests are not described as reproductions of a master failure.

Provide an alternative to SA1412 for projects that require UTF-8 source files without a byte order mark, with a code fix, translations, and documentation.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 16, 2026 00:03
Copilot AI balanced review requested due to automatic review settings October 16, 2026 00:04

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.

🟡 Changes recommended

The encoding comparison incorrectly reports compliant BOM-less UTF-8 when fallback settings differ.

1 open finding
What changed in this PR

Adds opt-in SX1412, complementing SA1412 by requiring UTF-8 source files without a byte order mark.

Changes:

  • Adds the disabled-by-default analyzer and single-document/Fix All code fixes.
  • Tests encoding detection and multi-file fixes.
  • Adds localized resources, rule documentation, and alternative-rule links.
File Description
test/​StyleCop.Analyzers.Test/​MaintainabilityRules/​SX1412UnitTests.cs Tests encoding checks and fixes.
src/​StyleCop.Analyzers/​MaintainabilityRules/​SX1412StoreFilesAsUtf8WithoutByteOrderMark.cs Implements SX1412.
src/​StyleCop.Analyzers/​MaintainabilityRules/​MaintainabilityResources.zh-CN.resx Adds Chinese resource strings.
src/​StyleCop.Analyzers/​MaintainabilityRules/​MaintainabilityResources.ru-RU.resx Adds Russian resource strings.
src/​StyleCop.Analyzers/​MaintainabilityRules/​MaintainabilityResources.resx Adds English resource strings.
src/​StyleCop.Analyzers/​Lightup/​.generated/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/​Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/​MaintainabilityResources.Designer.cs Exposes generated resource properties.
src/​StyleCop.Analyzers/​AnalyzerReleases.Unshipped.md Registers the new rule.
src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SX1412FixAllProvider.cs Implements Fix All scopes.
src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SX1412CodeFixProvider.cs Converts document encoding.
documentation/​zh-cn/​SX1412.md Documents SX1412 in Chinese.
documentation/​zh-cn/​SA1412.md Links the alternative rule.
documentation/​zh-cn/​AlternativeRules.md Lists SX1412.
documentation/​SX1412.md Documents SX1412 and its conflict with SA1412.
documentation/​SA1412.md Links the alternative rule.
documentation/​ru-ru/​SX1412.md Documents SX1412 in Russian.
documentation/​ru-ru/​SA1412.md Links the alternative rule.
documentation/​ru-ru/​AlternativeRules.md Lists SX1412.
documentation/​cs-cz/​SX1412.md Documents SX1412 in Czech.
documentation/​cs-cz/​SA1412.md Links the alternative rule.
documentation/​cs-cz/​AlternativeRules.md Lists SX1412.
documentation/​AlternativeRules.md Lists SX1412.
Files not reviewed (1)
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/MaintainabilityResources.Designer.cs: Generated file

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@codecov

codecov Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.39766% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.62%. Comparing base (9869738) to head (697efc3).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4315      +/-   ##
==========================================
- Coverage   97.63%   97.62%   -0.01%     
==========================================
  Files        1308     1312       +4     
  Lines      142748   142924     +176     
  Branches     4756     4765       +9     
==========================================
+ Hits       139377   139536     +159     
- Misses       2146     2157      +11     
- Partials     1225     1231       +6     
🚀 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 draft October 16, 2026 00:16
Recognize strict UTF-8 without a BOM, include the new rule in every localized navigation, and link to its local documentation until publication.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 16, 2026 00:27
Copilot AI balanced review requested due to automatic review settings October 16, 2026 00:27
@AArnott
AArnott merged commit 9496c90 into master Oct 16, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1412x-utf8-no-bom branch October 16, 2026 00:29

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.

🔵 Needs a closer look

The code fix loses script parsing mode, and the descriptor initializer prevents correct rule-status generation.

0 open findings

1 resolved since last review
Files not reviewed (1)
  • src/StyleCop.Analyzers/Lightup/.generated/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp/Microsoft.CodeAnalysis.ResxSourceGenerator.CSharp.CSharpResxGenerator/MaintainabilityResources.Designer.cs: Generated file
Previously missed (2)

In code that hasn't changed since last review

Medium severity Preserve SourceCodeKind when replacing documents

src/​StyleCop.Analyzers.CodeFixes/​MaintainabilityRules/​SX1412CodeFixProvider.cs:55

This AddDocument overload creates a regular C# document, losing the original SourceCodeKind. Applying the encoding fix to a script document can therefore turn valid constructs such as #r directives into compiler errors. Fix All calls the same transformation. Preserve document.SourceCodeKind on the replacement document.

Medium severity Use explicit DiagnosticDescriptor construction for status generation

src/​StyleCop.Analyzers/​MaintainabilityRules/​SX1412StoreFilesAsUtf8WithoutByteOrderMark.cs:31

The rule-status generator only recognizes explicit ObjectCreationExpressionSyntax initializers (tools/StyleCop.Analyzers.Status.Generator/SolutionReader.cs:241–253). It skips this target-typed new(...) and returns Unknown instead of DisabledByDefault for SX1412. Use new DiagnosticDescriptor(...) so the existing generator can read the rule's status.

🧠 Review effort: Balanced

This was referenced Nov 24, 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.

SX1412 rule for UTF-8 without BOM

2 participants