Skip to content

Commit 646f429

Browse files
Fix analyzer RCS1250: Don't suggest a collection expression for the Dictionary (#1652)
* Fix analyzer RCS1250: Don't suggest a collection expression for the Dictionary * Update ChangeLog.md * Update ChangeLog.md --------- Co-authored-by: Josef Pihrt <[email protected]>
1 parent 6a24655 commit 646f429

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

ChangeLog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515

1616
- Fix analyzer [RCS1264](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1264) ([PR](https://github.com/dotnet/roslynator/pull/1666))
1717
- Fix analyzer [RCS1229](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1229) ([PR](https://github.com/dotnet/roslynator/pull/1667))
18+
- Fix analyzer [RCS1250](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1250) ([PR](https://github.com/dotnet/roslynator/pull/1652) by @aihnatiuk)
1819

1920
## [4.13.1] - 2025-02-23
2021

src/Analyzers/CSharp/Analysis/ObjectCreation/ImplicitOrExplicitObjectCreationAnalysis.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ protected override bool UseCollectionExpressionFromImplicit(ref SyntaxNodeAnalys
5959
var implicitObjectCreation = (ImplicitObjectCreationExpressionSyntax)context.Node;
6060

6161
return implicitObjectCreation.ArgumentList?.Arguments.Any() != true
62-
&& implicitObjectCreation.Initializer?.Expressions.Any(f => f.IsKind(SyntaxKind.SimpleAssignmentExpression)) != true
62+
&& implicitObjectCreation.Initializer?.Expressions
63+
.Any(f => f.IsKind(
64+
SyntaxKind.SimpleAssignmentExpression,
65+
SyntaxKind.ComplexElementInitializerExpression)) != true
6366
&& UseCollectionExpression(ref context);
6467
}
6568

src/Tests/Analyzers.Tests/RCS1250UseImplicitOrExplicitObjectCreationTests.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,4 +1903,31 @@ void M2()
19031903
""", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Implicit)
19041904
.AddConfigOption(ConfigOptionKeys.UseCollectionExpression, false));
19051905
}
1906+
1907+
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseImplicitOrExplicitObjectCreation)]
1908+
public async Task TestNoDiagnostic_ComplexElementInitializer()
1909+
{
1910+
await VerifyNoDiagnosticAsync("""
1911+
using System;
1912+
using System.Collections;
1913+
using System.Collections.Generic;
1914+
class C : IEnumerable<KeyValuePair<int, int>>
1915+
{
1916+
public IEnumerator<KeyValuePair<int, int>> GetEnumerator() => throw new System.NotImplementedException();
1917+
1918+
IEnumerator IEnumerable.GetEnumerator() => throw new System.NotImplementedException();
1919+
1920+
public void Add(int arg1, int arg2)
1921+
{
1922+
}
1923+
1924+
static C M()
1925+
{
1926+
C c = new() { { 1, 2 } };
1927+
return c;
1928+
}
1929+
}
1930+
""", options: Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Implicit)
1931+
.AddConfigOption(ConfigOptionKeys.UseCollectionExpression, true));
1932+
}
19061933
}

0 commit comments

Comments
 (0)