Skip to content

Convert float/double += 1 into increment and decrement expressions - #3933

Open
siegfriedpammer wants to merge 1 commit into
masterfrom
test-cases-fp-types-2026
Open

Convert float/double += 1 into increment and decrement expressions#3933
siegfriedpammer wants to merge 1 commit into
masterfrom
test-cases-fp-types-2026

Conversation

@siegfriedpammer

Copy link
Copy Markdown
Member

Revives the 2020 test-cases-fp-types fixture work: ~770 lines of compound-assignment / increment / decrement coverage for float, double, and decimal in CompoundAssignmentTest.cs, mirroring the existing integer sections (fields, properties, params, locals, ref returns, custom-struct receivers).

The revived fixtures exposed an asymmetry: post-increment/decrement on float/double round-tripped as x++/x--, but the pre forms decompiled to x += 1f, because the increment detection in PrettifyAssignments only accepted integer constants. Since C# defines ++/-- on floating-point types as adding/subtracting exactly 1, a constant-1 float/double operand now also converts. Decimal needed no change (op_Increment/op_Decrement path).

Verified: CompoundAssignmentTest green in all 24 configs, full decompiler suite green on Linux (3082 passed / 0 failed / 44 skipped).

🤖 Generated with Claude Code

Reviving the 2020 test-cases-fp-types fixtures (compound assignment on
float, double and decimal) exposed an asymmetry: post-increment and
post-decrement on float/double round-tripped as x++/x--, but the pre
forms came back as x += 1f because the increment detection in
PrettifyAssignments only accepted integer constants. C# defines ++/--
on floating-point types as adding or subtracting exactly 1, so the
conversion is exact for a constant 1 operand. Decimal already works
through the op_Increment/op_Decrement path.

Assisted-by: Claude:claude-fable-5:Claude Code
@christophwille
christophwille requested a review from Copilot July 29, 2026 06:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Note

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Updates increment/decrement detection in the C# decompiler so float/double pre-increment/decrement can round-trip as ++x/--x (instead of x += 1f), and expands the pretty-printing fixture coverage for floating-point and decimal compound assignments.

Changes:

  • Extend PrettifyAssignments increment/decrement detection to accept constant-1 operands for float and double.
  • Revive/expand CompoundAssignmentTest fixtures with extensive float/double/decimal compound-assignment and inc/dec scenarios.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ICSharpCode.Decompiler/CSharp/Transforms/PrettifyAssignments.cs Broadens ++/-- detection from integer-only constants to also cover float/double constant-1 cases.
ICSharpCode.Decompiler.Tests/TestCases/Pretty/CompoundAssignmentTest.cs Adds large fixture coverage for compound assignments and inc/dec over float, double, and decimal across many receiver shapes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -76,9 +76,12 @@ public override void VisitAssignmentExpression(AssignmentExpression assignment)
}
if (context.Settings.IntroduceIncrementAndDecrement && assignment.Operator == AssignmentOperatorType.Add || assignment.Operator == AssignmentOperatorType.Subtract)
Comment on lines +79 to +80
// detect increment/decrement; ++/-- on float/double is defined as adding/subtracting
// exactly 1, so a constant 1 right-hand side qualifies there just like for integers
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