Skip to content

Add MA0193 Round mode analyzer and code fixes#1104

Merged
meziantou merged 4 commits intomainfrom
meziantou/round-mode-rule
Apr 24, 2026
Merged

Add MA0193 Round mode analyzer and code fixes#1104
meziantou merged 4 commits intomainfrom
meziantou/round-mode-rule

Conversation

@meziantou
Copy link
Copy Markdown
Owner

Why

Round without an explicit MidpointRounding mode can produce surprising results because it defaults to ToEven. This PR adds a dedicated rule so call sites must choose the rounding behavior intentionally.

What changed

  • Added MA0193 analyzer to report Round overloads that omit MidpointRounding for:
    • Math.Round(...)
    • MathF.Round(...)
    • decimal.Round(...)
    • IFloatingPoint<TSelf>.Round(...) and implementations
  • Added code fixes for MA0193 with one suggestion per available System.MidpointRounding member (for example ToEven, AwayFromZero, ToZero, ToNegativeInfinity, ToPositiveInfinity).
  • Enabled MA0193 by default as a suggestion.
  • Added/updated docs for MA0193 and regenerated generated docs/config sections.
  • Added tests covering analyzer behavior and per-enum-member code fix choices.

Notes for reviewers

  • The fixer inserts the enum argument at the overload position computed via OverloadFinder, so it works for both framework methods and IFloatingPoint<TSelf> implementations.
  • The MA0193 tests verify each code fix action index maps to a distinct MidpointRounding value.

meziantou and others added 3 commits April 24, 2026 17:29
Add MA0193 to enforce explicit MidpointRounding for Round overloads across Math, MathF, decimal, IFloatingPoint<TSelf>, and implementations.

Enable MA0193 as suggestion by default, add docs, and add analyzer/fixer tests including one code fix action per MidpointRounding member.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread docs/Rules/MA0193.md Outdated
@meziantou meziantou merged commit a415f35 into main Apr 24, 2026
13 checks passed
@meziantou meziantou deleted the meziantou/round-mode-rule branch April 24, 2026 21:50
This was referenced Apr 25, 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.

1 participant