diff --git a/src/IDisposableGenerator/ClassItems.cs b/src/IDisposableGenerator/ClassItems.cs index 08d767f..4d19549 100644 --- a/src/IDisposableGenerator/ClassItems.cs +++ b/src/IDisposableGenerator/ClassItems.cs @@ -5,10 +5,10 @@ internal class ClassItems public string? Name { get; set; } public Accessibility Accessibility { get; set; } public bool Stream { get; set; } - public List Owns { get; } = new(); - public List Fields { get; } = new(); - public List SetNull { get ; } = new(); - public List Methods { get; } = new(); + public List Owns { get; } = []; + public List Fields { get; } = []; + public List SetNull { get; } = []; + public List Methods { get; } = []; public bool AddSetNull(ISymbol member) { @@ -42,13 +42,14 @@ public bool NameEquals(string name) [ExcludeFromCodeCoverage] public override string ToString() { - return $@"Class: Name {( - this.Name)}, Accessibility: {( - this.Accessibility)}, Stream: {( - this.Stream)}, Owns Count: {( - this.Owns.Count)}, Fields Count: {( - this.Fields.Count)}, SetNull Count: {( - this.SetNull.Count)}, Methods Count: {( - this.Methods.Count)}"; + var result = new StringBuilder(); + _ = result.Append($"Class: Name {this.Name}") + .Append($", Accessibility: {this.Accessibility}") + .Append($", Stream: {this.Stream}") + .Append($", Owns Count: {this.Owns.Count}") + .Append($", Fields Count: {this.Fields.Count}") + .Append($", SetNull Count: {this.SetNull.Count}") + .Append($", Methods Count: {this.Methods.Count}"); + return result.ToString(); } } diff --git a/src/IDisposableGenerator/DisposableCodeWriter.cs b/src/IDisposableGenerator/DisposableCodeWriter.cs index 0eba3b3..a424d69 100644 --- a/src/IDisposableGenerator/DisposableCodeWriter.cs +++ b/src/IDisposableGenerator/DisposableCodeWriter.cs @@ -24,13 +24,13 @@ Implements IDisposable Private isDisposed As Boolean "); - if (classItem.Owns.Any() && !classItem.Stream) + if (classItem.Owns.Count is not 0 && !classItem.Stream) { _ = sourceBuilder.Append(@" Friend Property IsOwned As Boolean "); } - else if (classItem.Owns.Any() && classItem.Stream) + else if (classItem.Owns.Count is not 0 && classItem.Stream) { _ = sourceBuilder.Append(@" Friend ReadOnly Property KeepOpen As Boolean @@ -49,7 +49,7 @@ End Sub Protected Overrides")} Sub Dispose(ByVal disposing As Boolean) If Not Me.isDisposed AndAlso disposing Then "); - if (classItem.Methods.Any()) + if (classItem.Methods.Count is not 0) { foreach (var methodItem in classItem.Methods) { @@ -58,7 +58,7 @@ If Not Me.isDisposed AndAlso disposing Then } } - if (classItem.Owns.Any()) + if (classItem.Owns.Count is not 0) { _ = sourceBuilder.Append($@" If {(classItem.Stream ? "Not Me.KeepOpen" : "Me.IsOwned")} Then "); @@ -74,7 +74,7 @@ If Not Me.isDisposed AndAlso disposing Then "); } - if (classItem.Fields.Any()) + if (classItem.Fields.Count is not 0) { foreach (var fieldItem in classItem.Fields) { @@ -85,7 +85,7 @@ If Not Me.isDisposed AndAlso disposing Then } } - if (classItem.SetNull.Any()) + if (classItem.SetNull.Count is not 0) { foreach (var nullItem in classItem.SetNull) { @@ -104,7 +104,7 @@ End If MyBase.Dispose(disposing) "); } - + _ = sourceBuilder.Append(@" End Sub End Class "); @@ -135,13 +135,13 @@ namespace {workItem.Namespace}; {{ private bool isDisposed; "); - if (classItem.Owns.Any() && !classItem.Stream) + if (classItem.Owns.Count is not 0 && !classItem.Stream) { _ = sourceBuilder.Append(@" internal bool IsOwned { get; set; } "); } - else if (classItem.Owns.Any() && classItem.Stream) + else if (classItem.Owns.Count is not 0 && classItem.Stream) { _ = sourceBuilder.Append(@" internal bool KeepOpen { get; } @@ -160,7 +160,7 @@ namespace {workItem.Namespace}; if (!this.isDisposed && disposing) {{ "); - if (classItem.Methods.Any()) + if (classItem.Methods.Count is not 0) { foreach (var methodItem in classItem.Methods) { @@ -169,7 +169,7 @@ namespace {workItem.Namespace}; } } - if (classItem.Owns.Any()) + if (classItem.Owns.Count is not 0) { _ = sourceBuilder.Append($@" if ({(classItem.Stream ? "!this.KeepOpen" : "this.IsOwned")}) {{ @@ -186,7 +186,7 @@ namespace {workItem.Namespace}; "); } - if (classItem.Fields.Any()) + if (classItem.Fields.Count is not 0) { foreach (var fieldItem in classItem.Fields) { @@ -197,7 +197,7 @@ namespace {workItem.Namespace}; } } - if (classItem.SetNull.Any()) + if (classItem.SetNull.Count is not 0) { foreach (var nullItem in classItem.SetNull) { @@ -249,13 +249,13 @@ namespace {workItem.Namespace} {{ private bool isDisposed; "); - if (classItem.Owns.Any() && !classItem.Stream) + if (classItem.Owns.Count is not 0 && !classItem.Stream) { _ = sourceBuilder.Append(@" internal bool IsOwned { get; set; } "); } - else if (classItem.Owns.Any() && classItem.Stream) + else if (classItem.Owns.Count is not 0 && classItem.Stream) { _ = sourceBuilder.Append(@" internal bool KeepOpen { get; } @@ -274,7 +274,7 @@ namespace {workItem.Namespace} if (!this.isDisposed && disposing) {{ "); - if (classItem.Methods.Any()) + if (classItem.Methods.Count is not 0) { foreach (var methodItem in classItem.Methods) { @@ -283,7 +283,7 @@ namespace {workItem.Namespace} } } - if (classItem.Owns.Any()) + if (classItem.Owns.Count is not 0) { _ = sourceBuilder.Append($@" if ({(classItem.Stream ? "!this.KeepOpen" : "this.IsOwned")}) {{ @@ -300,7 +300,7 @@ namespace {workItem.Namespace} "); } - if (classItem.Fields.Any()) + if (classItem.Fields.Count is not 0) { foreach (var fieldItem in classItem.Fields) { @@ -311,7 +311,7 @@ namespace {workItem.Namespace} } } - if (classItem.SetNull.Any()) + if (classItem.SetNull.Count is not 0) { foreach (var nullItem in classItem.SetNull) { diff --git a/src/IDisposableGenerator/WorkItem.cs b/src/IDisposableGenerator/WorkItem.cs index 9e6683d..4d2a955 100644 --- a/src/IDisposableGenerator/WorkItem.cs +++ b/src/IDisposableGenerator/WorkItem.cs @@ -3,7 +3,7 @@ namespace IDisposableGenerator; internal class WorkItem { public string Namespace { get; set; } = null!; - public List Classes { get; } = new(); + public List Classes { get; } = []; public ClassItems? GetClassItems(INamedTypeSymbol testClass) => this.Classes.FirstOrDefault( @@ -15,8 +15,8 @@ public override string ToString() var sb = new StringBuilder($"Namespace: Name: {this.Namespace}"); foreach (var classItems in this.Classes) { - sb.AppendLine(); - sb.Append($"Class Item {this.Classes.IndexOf(classItems)}: {classItems}"); + _ = sb.AppendLine(); + _ = sb.Append($"Class Item {this.Classes.IndexOf(classItems)}: {classItems}"); } return sb.ToString(); diff --git a/src/IDisposableGenerator/WorkItemCollection.cs b/src/IDisposableGenerator/WorkItemCollection.cs index 9d95e27..9530cb6 100644 --- a/src/IDisposableGenerator/WorkItemCollection.cs +++ b/src/IDisposableGenerator/WorkItemCollection.cs @@ -1,20 +1,17 @@ namespace IDisposableGenerator; -internal class WorkItemCollection +internal class WorkItemCollection(Compilation compilation) { - internal Compilation Compilation { get; } - private List WorkItems { get; } = new(); - - public WorkItemCollection(Compilation compilation) - => this.Compilation = compilation; + internal Compilation Compilation { get; } = compilation; + private List WorkItems { get; } = []; public int Count => this.WorkItems.Count; public void Process(INamedTypeSymbol testClass, CancellationToken ct) { ct.ThrowIfCancellationRequested(); - AddFromNamespace(testClass.FullNamespace()); - var workItem = FindWithNamespace(testClass.FullNamespace()); + this.AddFromNamespace(testClass.FullNamespace()); + var workItem = this.FindWithNamespace(testClass.FullNamespace()); ct.ThrowIfCancellationRequested(); // Avoid a bug that would set namespace to "IDisposableGenerator" @@ -28,7 +25,7 @@ public void Process(INamedTypeSymbol testClass, CancellationToken ct) var classItemsQuery = from att in testClass.GetAttributes() where att.AttributeClass!.Name.Equals( - "GenerateDisposeAttribute") + "GenerateDisposeAttribute", StringComparison.Ordinal) select GetClassItem(att, testClass); var memberQuery = from member in testClass.GetMembers() @@ -94,7 +91,7 @@ private void AddFromNamespace(string nameSpace) return; } - WorkItems.Add(new WorkItem + this.WorkItems.Add(new WorkItem { Namespace = nameSpace, });