Skip to content

C# 14: User increment/decrement support.#21827

Open
michaelnebel wants to merge 12 commits into
github:mainfrom
michaelnebel:csharp14/userincrementdecrement
Open

C# 14: User increment/decrement support.#21827
michaelnebel wants to merge 12 commits into
github:mainfrom
michaelnebel:csharp14/userincrementdecrement

Conversation

@michaelnebel
Copy link
Copy Markdown
Contributor

@michaelnebel michaelnebel commented May 11, 2026

In this PR we add support for user defined instance decrement/increment operators.

Example:

public class C
{
    public int Value { get; private set; }

    public C(int value)
    {
        Value = value;
    }

    public void operator ++()
    {
        Value++;
    }
}

Similar to user defined compound assignment operators, the increment/decrement operators manipulates the instance they are applied to. This requires changes to the dispatch logic for such operator calls.

@github-actions github-actions Bot added the C# label May 11, 2026
@michaelnebel michaelnebel force-pushed the csharp14/userincrementdecrement branch from e93876d to dab1525 Compare May 12, 2026 08:52
@michaelnebel
Copy link
Copy Markdown
Contributor Author

DCA looks good

  • Performance appears to be un-affected.
  • There are some minor changes to the number of extraction errors/missing call targets, but it appears to be due to the wobliness that we sometimes experience (this is at least indicated by the re-runs). Also, I didn't expect any changes here, as the increment/decrement operators were extracted prior to this change - but the name was just incorrect and the dispatch logic was missing.
  • There is some alert wobliness as well, but it doesn't appear to be related to the changes in this PR either.

@michaelnebel michaelnebel marked this pull request as ready for review May 13, 2026 07:03
@michaelnebel michaelnebel requested a review from a team as a code owner May 13, 2026 07:03
@michaelnebel michaelnebel requested review from Copilot and hvitved May 13, 2026 07:03
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds C# 14 support for user-defined instance increment/decrement operators (mutating this), updating both extraction and QL-side call/dispatch modeling so these operator calls are represented and dispatched correctly in the C# CodeQL libraries and tests.

Changes:

  • Extend C# extractor operator-name handling to correctly render op_*Assignment-style increment/decrement operators without appending =.
  • Add QL library support for modeling instance mutator operator calls and include them in dispatch logic alongside other instance-operator-style calls.
  • Expand/adjust C# library tests (AST, operator call, and dataflow) to cover instance ++/-- (including checked forms) and extension-operator scenarios.
Show a summary per file
File Description
csharp/ql/test/library-tests/operators/PrintAst.expected Updated AST expectation output to include new instance ++/-- operator declarations and call sites.
csharp/ql/test/library-tests/operators/Operators6.ql New library-test query covering increment/decrement operator targets and calls.
csharp/ql/test/library-tests/operators/Operators6.expected Expected results for the new operator-target/operator-call test.
csharp/ql/test/library-tests/operators/Operators5.expected Updated expected locations due to added code shifting line numbers.
csharp/ql/test/library-tests/operators/Operators4.expected Updated expected location due to added code shifting line numbers.
csharp/ql/test/library-tests/operators/Operators3.expected Updated expected location due to added code shifting line numbers.
csharp/ql/test/library-tests/operators/operators.cs Added a class with instance ++/-- operators and call sites (including checked block usage).
csharp/ql/test/library-tests/extension/PrintAst.expected Updated AST expectations for extension operators, including ++/--.
csharp/ql/test/library-tests/extension/extensionTypes.expected Updated expected locations due to added extension-operator content shifting line numbers.
csharp/ql/test/library-tests/extension/extensions.expected Updated expected call/parameter/operator expectations to include ++/-- extension operators and new line numbers.
csharp/ql/test/library-tests/extension/extensions.cs Added extension operator ++ and operator -- plus call sites to exercise extraction/dispatch.
csharp/ql/test/library-tests/dataflow/operators/operatorFlow.expected Updated dataflow expectations to include flow through instance mutator operator ++.
csharp/ql/test/library-tests/dataflow/operators/Operator.cs Added a new test class exercising mutation/dataflow through instance operator ++.
csharp/ql/lib/semmle/code/csharp/exprs/Call.qll Introduced an InstanceMutatorOperatorCall concept for instance mutator operator invocations.
csharp/ql/lib/semmle/code/csharp/dispatch/Dispatch.qll Updated dispatch modeling to treat instance operator calls (compound assignment + mutator) consistently.
csharp/ql/lib/semmle/code/csharp/Callable.qll Updated unary-operator classification to allow parameterless non-static ++/-- operators.
csharp/ql/lib/change-notes/2026-05-12-user-increment-decrement.md Added change note for the new C# 14 instance ++/-- operator support.
csharp/extractor/Semmle.Extraction.CSharp/Entities/Expressions/Invocation.cs Updated invocation target-resolution to use context-aware GetTargetSymbol.
csharp/extractor/Semmle.Extraction.CSharp/Entities/Expression.cs Refactored target-resolution to be context-driven and reused in call-type detection.
csharp/extractor/Semmle.Extraction.CSharp.Util/SymbolExtensions.cs Adjusted operator-symbol rendering to avoid incorrectly producing ++=/--= for assignment-suffixed operator method names.

Copilot's findings

  • Files reviewed: 20/20 changed files
  • Comments generated: 3

Comment on lines +56 to +58
/// The operatorname for user-defined increment and decrement operators are "op_IncrementAssignment" and
/// "op_DecrementAssignment" respectively.
/// Thus we need to handle this explicitly to avoid postfixing them with an "=".
/// "op_DecrementAssignment" respectively.
/// Thus we need to handle this explicitly to avoid postfixing them with an "=".
/// </summary>
private static bool isIncrementOrDecrement(string operatorName) => operatorName == "++" || operatorName == "--";
Comment thread csharp/ql/lib/semmle/code/csharp/exprs/Call.qll Outdated
@michaelnebel michaelnebel force-pushed the csharp14/userincrementdecrement branch from 4d850a2 to fa2d633 Compare May 13, 2026 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants