From 850ffe593dfd2b86cef9a6e315a54ca46d572b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20Schm=C3=B6cker?= Date: Tue, 5 Aug 2025 23:39:02 +0200 Subject: [PATCH] Fix methods with mandatory arguments ignore disabled OptionalArguments setting --- .../ICSharpCode.Decompiler.Tests.csproj | 1 + .../PrettyTestRunner.cs | 11 +++++++++++ .../Pretty/OptionalArgumentsDisabled.cs | 19 +++++++++++++++++++ ICSharpCode.Decompiler/CSharp/CallBuilder.cs | 3 ++- 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArgumentsDisabled.cs diff --git a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj index 0282f9d00..6529de6ed 100644 --- a/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj +++ b/ICSharpCode.Decompiler.Tests/ICSharpCode.Decompiler.Tests.csproj @@ -251,6 +251,7 @@ + diff --git a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs index d67877e72..3df322aaf 100644 --- a/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs +++ b/ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs @@ -599,6 +599,17 @@ namespace ICSharpCode.Decompiler.Tests await RunForLibrary(cscOptions: cscOptions); } + [Test] + public async Task OptionalArgumentsDisabled([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) + { + await RunForLibrary( + cscOptions: cscOptions, + configureDecompiler: settings => { + settings.OptionalArguments = false; + } + ); + } + [Test] public async Task Comparisons([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) { diff --git a/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArgumentsDisabled.cs b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArgumentsDisabled.cs new file mode 100644 index 000000000..b33456f2c --- /dev/null +++ b/ICSharpCode.Decompiler.Tests/TestCases/Pretty/OptionalArgumentsDisabled.cs @@ -0,0 +1,19 @@ +namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty +{ + public class OptionalArgumentsDisabled + { + public void Test() + { + MixedArguments("123", 0, 0); + OnlyOptionalArguments(0, 0); + } + + public void MixedArguments(string msg, int a = 0, int b = 0) + { + } + + public void OnlyOptionalArguments(int a = 0, int b = 0) + { + } + } +} diff --git a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs index 2ff1ce4d1..5027b7370 100644 --- a/ICSharpCode.Decompiler/CSharp/CallBuilder.cs +++ b/ICSharpCode.Decompiler/CSharp/CallBuilder.cs @@ -967,7 +967,8 @@ namespace ICSharpCode.Decompiler.CSharp } else { - firstOptionalArgumentIndex = -2; + if (firstOptionalArgumentIndex != -1) + firstOptionalArgumentIndex = -2; } if (parameter.IsParams && i + 1 == callArguments.Count && argumentToParameterMap == null) {