Skip to content

Commit 13ce990

Browse files
committed
Remove SingleOrDefault() use in SymbolInfo extension
1 parent fd7fa69 commit 13ce990

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Extensions/SymbolInfoExtensions.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Diagnostics.CodeAnalysis;
6-
using System.Linq;
76
using Microsoft.CodeAnalysis;
87

98
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
@@ -26,8 +25,16 @@ internal static class SymbolInfoExtensions
2625
/// </remarks>
2726
public static bool TryGetAttributeTypeSymbol(this SymbolInfo symbolInfo, [NotNullWhen(true)] out INamedTypeSymbol? typeSymbol)
2827
{
29-
if ((symbolInfo.Symbol ?? symbolInfo.CandidateSymbols.SingleOrDefault()) is not ISymbol attributeSymbol ||
30-
(attributeSymbol as INamedTypeSymbol ?? attributeSymbol.ContainingType) is not INamedTypeSymbol resultingSymbol)
28+
ISymbol? attributeSymbol = symbolInfo.Symbol;
29+
30+
// If no symbol is selected and there is a single candidate symbol, use that
31+
if (attributeSymbol is null && symbolInfo.CandidateSymbols is [ISymbol candidateSymbol])
32+
{
33+
attributeSymbol = candidateSymbol;
34+
}
35+
36+
// Extract the symbol from either the current one or the containing type
37+
if ((attributeSymbol as INamedTypeSymbol ?? attributeSymbol?.ContainingType) is not INamedTypeSymbol resultingSymbol)
3138
{
3239
typeSymbol = null;
3340

0 commit comments

Comments
 (0)