Skip to content

Commit 4b449ae

Browse files
[patch] Fix SonarCloud issues in library and benchmark code
Library (9 issues): - S1939 (8): drop interfaces already implied by another interface in the base list. ContiguousCollection/InsertionOrderCollection/ OrderedCollection drop IReadOnlyCollection<T> (implied by IReadOnlyList<T>); RingBuffer drops IEnumerable<T> and IReadOnlyCollection<T> for the same reason. For the three Set types IReadOnlySet<T> only exists on NET5_0_OR_GREATER, and the library still targets netstandard2.0/2.1, so IReadOnlyCollection<T> moved into an #else branch rather than being removed outright - removing it would have dropped the interface from the lower targets. - S4136 (1): ContiguousMap.Entry - move Equals(Entry) next to Equals(object?) so the overloads are adjacent. Benchmarks (2 issues): - S4487 (1, HIGH): remove the unread keyValuePairs field in OrderedMapBenchmarks and its GlobalSetup assignment. - S1481 (1): discard the intentional indexer read in CacheUsagePatternRingBuffer instead of binding an unused local. Build clean on all 8 TFMs, 351/351 tests pass.
1 parent cd260c9 commit 4b449ae

10 files changed

Lines changed: 20 additions & 19 deletions

Containers.Benchmarks/CrossCollectionComparisonBenchmarks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public RingBuffer<int> CacheUsagePatternRingBuffer()
462462
if (cache.Count > 10 && random.Next(5) == 0)
463463
{
464464
int index = random.Next(Math.Min(cache.Count, 10));
465-
int value = cache[index]; // Access recent item
465+
_ = cache[index]; // Access recent item; result intentionally unused
466466
}
467467
}
468468

Containers.Benchmarks/OrderedMapBenchmarks.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ public class OrderedMapBenchmarks
1414
private readonly Random random = new(42);
1515
private int[] keys = [];
1616
private string[] values = [];
17-
private KeyValuePair<int, string>[] keyValuePairs = [];
1817

1918
/// <summary>
2019
/// Gets or sets the number of elements to use in benchmarks.
@@ -30,7 +29,6 @@ public void Setup()
3029
{
3130
keys = [.. Enumerable.Range(1, ElementCount).OrderBy(x => random.Next())];
3231
values = [.. keys.Select(k => $"value{k}")];
33-
keyValuePairs = [.. keys.Zip(values, (k, v) => new KeyValuePair<int, string>(k, v))];
3432
}
3533

3634
/// <summary>

Containers/ContiguousCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace ktsu.Containers;
4242
"CA1710:Identifiers should have correct suffix",
4343
Justification = "ContiguousCollection is a known collection name"
4444
)]
45-
public class ContiguousCollection<T> : ICollection<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
45+
public class ContiguousCollection<T> : ICollection<T>, IReadOnlyList<T>
4646
{
4747
/// <summary>
4848
/// The backing array that stores elements in contiguous memory.

Containers/ContiguousMap.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,15 @@ obj is Entry other
7979
&& EqualityComparer<TKey>.Default.Equals(Key, other.Key)
8080
&& EqualityComparer<TValue>.Default.Equals(Value, other.Value);
8181

82+
/// <summary>
83+
/// Determines whether the current entry is equal to another entry.
84+
/// </summary>
85+
/// <param name="other">An entry to compare with this entry.</param>
86+
/// <returns>true if the current entry is equal to the other parameter; otherwise, false.</returns>
87+
public bool Equals(Entry other) =>
88+
EqualityComparer<TKey>.Default.Equals(Key, other.Key)
89+
&& EqualityComparer<TValue>.Default.Equals(Value, other.Value);
90+
8291
/// <summary>
8392
/// Returns the hash code for this entry.
8493
/// </summary>
@@ -95,15 +104,6 @@ public override int GetHashCode()
95104
#endif
96105
}
97106

98-
/// <summary>
99-
/// Determines whether the current entry is equal to another entry.
100-
/// </summary>
101-
/// <param name="other">An entry to compare with this entry.</param>
102-
/// <returns>true if the current entry is equal to the other parameter; otherwise, false.</returns>
103-
public bool Equals(Entry other) =>
104-
EqualityComparer<TKey>.Default.Equals(Key, other.Key)
105-
&& EqualityComparer<TValue>.Default.Equals(Value, other.Value);
106-
107107
/// <summary>
108108
/// Determines whether two entries are equal.
109109
/// </summary>

Containers/ContiguousSet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ namespace ktsu.Containers;
4747
public class ContiguousSet<T> : ISet<T>
4848
#if NET5_0_OR_GREATER
4949
, IReadOnlySet<T>
50-
#endif
50+
#else
5151
, IReadOnlyCollection<T>
52+
#endif
5253
{
5354
/// <summary>
5455
/// The backing array that stores elements in contiguous memory.

Containers/InsertionOrderCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ktsu.Containers;
3030
"CA1710:Identifiers should have correct suffix",
3131
Justification = "InsertionOrderCollection is a known collection name"
3232
)]
33-
public class InsertionOrderCollection<T> : ICollection<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
33+
public class InsertionOrderCollection<T> : ICollection<T>, IReadOnlyList<T>
3434
{
3535
/// <summary>
3636
/// The internal list that stores elements in insertion order.

Containers/InsertionOrderSet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ namespace ktsu.Containers;
3434
public class InsertionOrderSet<T> : ISet<T>
3535
#if NET5_0_OR_GREATER
3636
, IReadOnlySet<T>
37-
#endif
37+
#else
3838
, IReadOnlyCollection<T>
39+
#endif
3940
{
4041
/// <summary>
4142
/// The internal list that stores elements in insertion order.

Containers/OrderedCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace ktsu.Containers;
3232
"CA1710:Identifiers should have correct suffix",
3333
Justification = "OrderedCollection is a known collection name"
3434
)]
35-
public class OrderedCollection<T> : ICollection<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
35+
public class OrderedCollection<T> : ICollection<T>, IReadOnlyList<T>
3636
{
3737
/// <summary>
3838
/// The internal list that stores elements in sorted order.

Containers/OrderedSet.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ namespace ktsu.Containers;
3636
public class OrderedSet<T> : ISet<T>
3737
#if NET5_0_OR_GREATER
3838
, IReadOnlySet<T>
39-
#endif
39+
#else
4040
, IReadOnlyCollection<T>
41+
#endif
4142
{
4243
/// <summary>
4344
/// The internal list that stores elements in sorted order.

Containers/RingBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ namespace ktsu.Containers;
3131
"CA1710:Identifiers should have correct suffix",
3232
Justification = "RingBuffer is a known collection name"
3333
)]
34-
public class RingBuffer<T> : IEnumerable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>
34+
public class RingBuffer<T> : IReadOnlyList<T>
3535
{
3636
/// <summary>
3737
/// Gets or sets the internal buffer array.

0 commit comments

Comments
 (0)