Skip to content

Commit 7f85c37

Browse files
committed
Remove unnecessary immutable array builders
1 parent 539e617 commit 7f85c37

File tree

3 files changed

+12
-17
lines changed

3 files changed

+12
-17
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/INotifyPropertyChangedGenerator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public INotifyPropertyChangedGenerator()
3030
/// <inheritdoc/>
3131
private protected override INotifyPropertyChangedInfo? ValidateTargetTypeAndGetInfo(INamedTypeSymbol typeSymbol, AttributeData attributeData, Compilation compilation, out ImmutableArray<DiagnosticInfo> diagnostics)
3232
{
33-
ImmutableArray<DiagnosticInfo>.Builder builder = ImmutableArray.CreateBuilder<DiagnosticInfo>();
33+
diagnostics = ImmutableArray<DiagnosticInfo>.Empty;
3434

3535
INotifyPropertyChangedInfo? info = null;
3636

3737
// Check if the type already implements INotifyPropertyChanged
3838
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged")))
3939
{
40-
builder.Add(DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol);
40+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangedInterfaceForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol));
4141

4242
goto End;
4343
}
@@ -46,7 +46,7 @@ public INotifyPropertyChangedGenerator()
4646
if (typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
4747
typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
4848
{
49-
builder.Add(InvalidAttributeCombinationForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol);
49+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForINotifyPropertyChangedAttributeError, typeSymbol, typeSymbol));
5050

5151
goto End;
5252
}
@@ -56,8 +56,6 @@ public INotifyPropertyChangedGenerator()
5656
info = new INotifyPropertyChangedInfo(includeAdditionalHelperMethods);
5757

5858
End:
59-
diagnostics = builder.ToImmutable();
60-
6159
return info;
6260
}
6361

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableObjectGenerator.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,20 @@ public ObservableObjectGenerator()
2929
/// <inheritdoc/>
3030
private protected override object? ValidateTargetTypeAndGetInfo(INamedTypeSymbol typeSymbol, AttributeData attributeData, Compilation compilation, out ImmutableArray<DiagnosticInfo> diagnostics)
3131
{
32-
ImmutableArray<DiagnosticInfo>.Builder builder = ImmutableArray.CreateBuilder<DiagnosticInfo>();
32+
diagnostics = ImmutableArray<DiagnosticInfo>.Empty;
3333

3434
// Check if the type already implements INotifyPropertyChanged...
3535
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanged")))
3636
{
37-
builder.Add(DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol);
37+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangedInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
3838

3939
goto End;
4040
}
4141

4242
// ...or INotifyPropertyChanging
4343
if (typeSymbol.AllInterfaces.Any(i => i.HasFullyQualifiedName("global::System.ComponentModel.INotifyPropertyChanging")))
4444
{
45-
builder.Add(DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol);
45+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateINotifyPropertyChangingInterfaceForObservableObjectAttributeError, typeSymbol, typeSymbol));
4646

4747
goto End;
4848
}
@@ -51,14 +51,12 @@ public ObservableObjectGenerator()
5151
if (typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObjectAttribute") ||
5252
typeSymbol.HasOrInheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute"))
5353
{
54-
builder.Add(InvalidAttributeCombinationForObservableObjectAttributeError, typeSymbol, typeSymbol);
54+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForObservableObjectAttributeError, typeSymbol, typeSymbol));
5555

5656
goto End;
5757
}
5858

5959
End:
60-
diagnostics = builder.ToImmutable();
61-
6260
return null;
6361
}
6462

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableRecipientGenerator.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
88
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
9+
using CommunityToolkit.Mvvm.SourceGenerators.Input.Models;
910
using CommunityToolkit.Mvvm.SourceGenerators.Models;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.CSharp;
@@ -32,22 +33,22 @@ public ObservableRecipientGenerator()
3233
/// <inheritdoc/>
3334
private protected override ObservableRecipientInfo? ValidateTargetTypeAndGetInfo(INamedTypeSymbol typeSymbol, AttributeData attributeData, Compilation compilation, out ImmutableArray<DiagnosticInfo> diagnostics)
3435
{
35-
ImmutableArray<DiagnosticInfo>.Builder builder = ImmutableArray.CreateBuilder<DiagnosticInfo>();
36+
diagnostics = ImmutableArray<DiagnosticInfo>.Empty;
3637

3738
ObservableRecipientInfo? info = null;
3839

3940
// Check if the type already inherits from ObservableRecipient
4041
if (typeSymbol.InheritsFromFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableRecipient"))
4142
{
42-
builder.Add(DuplicateObservableRecipientError, typeSymbol, typeSymbol);
43+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(DuplicateObservableRecipientError, typeSymbol, typeSymbol));
4344

4445
goto End;
4546
}
4647

4748
// Check if the type already inherits [ObservableRecipient]
4849
if (typeSymbol.InheritsAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute"))
4950
{
50-
builder.Add(InvalidAttributeCombinationForObservableRecipientAttributeError, typeSymbol, typeSymbol);
51+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(InvalidAttributeCombinationForObservableRecipientAttributeError, typeSymbol, typeSymbol));
5152

5253
goto End;
5354
}
@@ -60,7 +61,7 @@ public ObservableRecipientGenerator()
6061
a.AttributeClass?.HasFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.INotifyPropertyChangedAttribute") == true &&
6162
!a.HasNamedArgument("IncludeAdditionalHelperMethods", false)))
6263
{
63-
builder.Add(MissingBaseObservableObjectFunctionalityError, typeSymbol, typeSymbol);
64+
diagnostics = ImmutableArray.Create(DiagnosticInfo.Create(MissingBaseObservableObjectFunctionalityError, typeSymbol, typeSymbol));
6465

6566
goto End;
6667
}
@@ -84,8 +85,6 @@ public ObservableRecipientGenerator()
8485
hasOnDeactivatedMethod);
8586

8687
End:
87-
diagnostics = builder.ToImmutable();
88-
8988
return info;
9089
}
9190

0 commit comments

Comments
 (0)