Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace calls to .Any() with .Count is not 0. #37

Merged
merged 2 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
_ = 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 @@
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)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
{
_ = sourceBuilder.Append(@"
Friend ReadOnly Property KeepOpen As Boolean
Expand All @@ -49,7 +49,7 @@
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)
{
Expand All @@ -58,7 +58,7 @@
}
}

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 (classItem.Fields.Any())
if (classItem.Fields.Count is not 0)
{
foreach (var fieldItem in classItem.Fields)
{
Expand All @@ -85,7 +85,7 @@
}
}

if (classItem.SetNull.Any())
if (classItem.SetNull.Count is not 0)
{
foreach (var nullItem in classItem.SetNull)
{
Expand All @@ -104,7 +104,7 @@
MyBase.Dispose(disposing)
");
}

_ = sourceBuilder.Append(@" End Sub
End Class
");
Expand Down Expand Up @@ -135,13 +135,13 @@
{{
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)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
{
_ = sourceBuilder.Append(@"
internal bool KeepOpen { get; }
Expand All @@ -160,7 +160,7 @@
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 @@
}
}

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 @@
");
}

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

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 @@
{{
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)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
{
_ = sourceBuilder.Append(@"
internal bool KeepOpen { get; }
Expand All @@ -274,7 +274,7 @@
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 @@
}
}

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 @@
");
}

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

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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too sure if applying this pattern is healthy

_ = 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;

internal class WorkItemCollection
internal class WorkItemCollection(Compilation compilation)
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
{
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 @@
var classItemsQuery =
from att in testClass.GetAttributes()
where att.AttributeClass!.Name.Equals(
"GenerateDisposeAttribute")
"GenerateDisposeAttribute", StringComparison.Ordinal)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use nameof(GenerateDisposeAttribute) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like that very much however the attribute code files does not get directly compiled to be a part of the generator assembly, instead it gets provided as basically an output from the generator itself. I guess a workaround is to reference the generator and having it run here and then use nameof(GenerateDisposeAttribute) though.

Copy link
Collaborator

@Rekkonnect Rekkonnect Feb 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's included in the generated output files, it's fine. I don't find a reason to change how the API for the generator is accessible to the user.

select GetClassItem(att, testClass);
var memberQuery =
from member in testClass.GetMembers()
Expand Down Expand Up @@ -94,7 +91,7 @@
return;
}

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