Skip to content

Commit

Permalink
Replace calls to .Any() with .Count is not 0.
Browse files Browse the repository at this point in the history
Signed-off-by: AraHaan <[email protected]>
  • Loading branch information
AraHaan committed Feb 23, 2024
1 parent f521889 commit c1e9407
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
25 changes: 13 additions & 12 deletions src/IDisposableGenerator/ClassItems.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ internal class ClassItems
public string? Name { get; set; }
public Accessibility Accessibility { get; set; }
public bool Stream { get; set; }
public List<string> Owns { get; } = new();
public List<string> Fields { get; } = new();
public List<string> SetNull { get ; } = new();
public List<string> Methods { get; } = new();
public List<string> Owns { get; } = [];
public List<string> Fields { get; } = [];
public List<string> SetNull { get; } = [];
public List<string> Methods { get; } = [];

public bool AddSetNull(ISymbol member)
{
Expand Down Expand Up @@ -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();

Check warning on line 45 in src/IDisposableGenerator/ClassItems.cs

View workflow job for this annotation

GitHub Actions / build

Use explicit type instead of 'var' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0008)
_ = 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();
}
}
38 changes: 19 additions & 19 deletions src/IDisposableGenerator/DisposableCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Check warning on line 54 in src/IDisposableGenerator/DisposableCodeWriter.cs

View workflow job for this annotation

GitHub Actions / Codacy Security Scan

Use explicit type instead of 'var' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0008)
{
Expand All @@ -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
");
Expand All @@ -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)

Check warning on line 79 in src/IDisposableGenerator/DisposableCodeWriter.cs

View workflow job for this annotation

GitHub Actions / Codacy Security Scan

Use explicit type instead of 'var' (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0008)
{
Expand All @@ -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)
{
Expand All @@ -104,7 +104,7 @@ End If
MyBase.Dispose(disposing)
");
}

_ = sourceBuilder.Append(@" End Sub
End Class
");
Expand Down Expand Up @@ -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; }
Expand All @@ -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)
{
Expand All @@ -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")})
{{
Expand All @@ -186,7 +186,7 @@ namespace {workItem.Namespace};
");
}

if (classItem.Fields.Any())
if (classItem.Fields.Count is not 0)
{
foreach (var fieldItem in classItem.Fields)
{
Expand All @@ -197,7 +197,7 @@ namespace {workItem.Namespace};
}
}

if (classItem.SetNull.Any())
if (classItem.SetNull.Count is not 0)
{
foreach (var nullItem in classItem.SetNull)
{
Expand Down Expand Up @@ -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; }
Expand All @@ -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)
{
Expand All @@ -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")})
{{
Expand All @@ -300,7 +300,7 @@ namespace {workItem.Namespace}
");
}

if (classItem.Fields.Any())
if (classItem.Fields.Count is not 0)
{
foreach (var fieldItem in classItem.Fields)
{
Expand All @@ -311,7 +311,7 @@ namespace {workItem.Namespace}
}
}

if (classItem.SetNull.Any())
if (classItem.SetNull.Count is not 0)
{
foreach (var nullItem in classItem.SetNull)
{
Expand Down
6 changes: 3 additions & 3 deletions src/IDisposableGenerator/WorkItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace IDisposableGenerator;
internal class WorkItem
{
public string Namespace { get; set; } = null!;
public List<ClassItems> Classes { get; } = new();
public List<ClassItems> Classes { get; } = [];

public ClassItems? GetClassItems(INamedTypeSymbol testClass)
=> this.Classes.FirstOrDefault(
Expand All @@ -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();
Expand Down
17 changes: 7 additions & 10 deletions src/IDisposableGenerator/WorkItemCollection.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
namespace IDisposableGenerator;

Check warning on line 1 in src/IDisposableGenerator/WorkItemCollection.cs

View workflow job for this annotation

GitHub Actions / build

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

Check warning on line 1 in src/IDisposableGenerator/WorkItemCollection.cs

View workflow job for this annotation

GitHub Actions / Codacy Security Scan

Convert to block scoped namespace (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/ide0160)

internal class WorkItemCollection
internal class WorkItemCollection(Compilation compilation)
{
internal Compilation Compilation { get; }
private List<WorkItem> WorkItems { get; } = new();

public WorkItemCollection(Compilation compilation)
=> this.Compilation = compilation;
internal Compilation Compilation { get; } = compilation;
private List<WorkItem> 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"
Expand All @@ -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()
Expand Down Expand Up @@ -94,7 +91,7 @@ private void AddFromNamespace(string nameSpace)
return;
}

WorkItems.Add(new WorkItem
this.WorkItems.Add(new WorkItem
{
Namespace = nameSpace,
});
Expand Down

0 comments on commit c1e9407

Please sign in to comment.