Skip to content

Commit 03c2c7a

Browse files
committed
Analyzer for named params (TTRESG046) should not trigger on default values
1 parent affae44 commit 03c2c7a

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Diagnostics/ThinktectureRuntimeExtensionsAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ private static void AnalyzeSwitchMap(
343343
{
344344
var argument = args[argIndex];
345345

346-
if (argument.Syntax is not ArgumentSyntax argSyntax)
346+
if (argument.ArgumentKind == ArgumentKind.DefaultValue || argument.Syntax is not ArgumentSyntax argSyntax)
347347
continue;
348348

349349
if (argSyntax.NameColon is not null)

test/Thinktecture.Runtime.Extensions.SourceGenerator.Tests/AnalyzerAndCodeFixTests/TTRESG046_IndexBasedSwitchAndMapMustUseNamedParameters.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,66 @@ public void Do()
19551955

19561956
await Verifier.VerifyAnalyzerAsync(code, [typeof(UnionAttribute).Assembly, typeof(TestUnion).Assembly]);
19571957
}
1958+
1959+
[Fact]
1960+
public async Task Should_not_trigger_when_all_args_are_named_but_not_all_are_present()
1961+
{
1962+
var code = """
1963+
1964+
using System;
1965+
using System.Collections.Generic;
1966+
using Thinktecture;
1967+
using Thinktecture.Runtime.Tests.TestRegularUnions;
1968+
1969+
namespace TestNamespace
1970+
{
1971+
public class Test
1972+
{
1973+
public void Do()
1974+
{
1975+
var value = new PlaceId.RegionId.InnerRegionId();
1976+
var res = value.SwitchPartially(
1977+
@default: _ => "default",
1978+
regionId: _ => "RegionId" ,
1979+
regionIdInnerRegionId: _ => "InnerRegionId");
1980+
}
1981+
}
1982+
}
1983+
""";
1984+
1985+
await Verifier.VerifyAnalyzerAsync(code, [typeof(UnionAttribute).Assembly, typeof(TestUnion).Assembly]);
1986+
}
1987+
1988+
[Fact]
1989+
public async Task Should_not_trigger_when_all_args_are_named_and_result_is_inlined_as_method_arg()
1990+
{
1991+
var code = """
1992+
1993+
using System;
1994+
using System.Collections.Generic;
1995+
using Thinktecture;
1996+
using Thinktecture.Runtime.Tests.TestRegularUnions;
1997+
1998+
namespace TestNamespace
1999+
{
2000+
public class Test
2001+
{
2002+
public void Do()
2003+
{
2004+
var value = new PlaceId.RegionId.InnerRegionId();
2005+
List<string> res = [];
2006+
2007+
res.Add(value.SwitchPartially(
2008+
@default: _ => "default",
2009+
regionId: _ => "RegionId" ,
2010+
regionIdInnerRegionId: _ => "InnerRegionId"));
2011+
}
2012+
}
2013+
}
2014+
""";
2015+
2016+
await Verifier.VerifyAnalyzerAsync(code, [typeof(UnionAttribute).Assembly, typeof(TestUnion).Assembly]);
2017+
}
19582018
}
19592019

19602020
public class SwitchWithFuncAndContext

0 commit comments

Comments
 (0)