Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Mar 4, 2024
1 parent 1c4bd68 commit 838fa4a
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 15 deletions.
142 changes: 138 additions & 4 deletions src/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,148 @@ indent_style = space
indent_size = 4
charset = utf-8

# Avoid constant arrays as arguments
dotnet_diagnostic.CA1861.severity = error

# Declare types in namespaces
dotnet_diagnostic.CA1050.severity = None

# Use Literals Where Appropriate
dotnet_diagnostic.CA1802.severity = error

# Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = error

# Avoid unsealed attributes
dotnet_diagnostic.CA1813.severity = error

# Test for empty strings using string length
dotnet_diagnostic.CA1820.severity = error

# Remove empty finalizers
dotnet_diagnostic.CA1821.severity = error

# Mark members as static
dotnet_diagnostic.CA1822.severity = error

# Avoid unused private fields
dotnet_diagnostic.CA1823.severity = error

# Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = error

# Use property instead of Linq Enumerable method
dotnet_diagnostic.CA1826.severity = error

# Do not use Count()/LongCount() when Any() can be used
dotnet_diagnostic.CA1827.severity = error
dotnet_diagnostic.CA1828.severity = error

# Use Length/Count property instead of Enumerable.Count method
dotnet_diagnostic.CA1829.severity = error

# Prefer strongly-typed Append and Insert method overloads on StringBuilder
dotnet_diagnostic.CA1830.severity = error

# Use AsSpan instead of Range-based indexers for string when appropriate
dotnet_diagnostic.CA1831.severity = error

# Use AsSpan instead of Range-based indexers for string when appropriate
dotnet_diagnostic.CA1831.severity = error
dotnet_diagnostic.CA1832.severity = error
dotnet_diagnostic.CA1833.severity = error

# Use StringBuilder.Append(char) for single character strings
dotnet_diagnostic.CA1834.severity = error

# Prefer IsEmpty over Count when available
dotnet_diagnostic.CA1836.severity = error

# Prefer IsEmpty over Count when available
dotnet_diagnostic.CA1836.severity = error

# Use Environment.ProcessId instead of Process.GetCurrentProcess().Id
dotnet_diagnostic.CA1837.severity = error

# Use Environment.ProcessPath instead of Process.GetCurrentProcess().MainModule.FileName
dotnet_diagnostic.CA1839.severity = error

# Use Environment.CurrentManagedThreadId instead of Thread.CurrentThread.ManagedThreadId
dotnet_diagnostic.CA1840.severity = error

# Prefer Dictionary Contains methods
dotnet_diagnostic.CA1841.severity = error

# Do not use WhenAll with a single task
dotnet_diagnostic.CA1842.severity = error

# Do not use WhenAll/WaitAll with a single task
dotnet_diagnostic.CA1842.severity = error
dotnet_diagnostic.CA1843.severity = error

# Use span-based 'string.Concat'
dotnet_diagnostic.CA1845.severity = error

# Prefer AsSpan over Substring
dotnet_diagnostic.CA1846.severity = error

# Use string.Contains(char) instead of string.Contains(string) with single characters
dotnet_diagnostic.CA1847.severity = error

# Use the LoggerMessage delegates
dotnet_diagnostic.CA1848.severity = error

# Prefer static HashData method over ComputeHash
dotnet_diagnostic.CA1850.severity = error

# Possible multiple enumerations of IEnumerable collection
dotnet_diagnostic.CA1851.severity = error

# Unnecessary call to Dictionary.ContainsKey(key)
dotnet_diagnostic.CA1853.severity = error

# Prefer the IDictionary.TryGetValue(TKey, out TValue) method
dotnet_diagnostic.CA1854.severity = error

# Use Span<T>.Clear() instead of Span<T>.Fill()
dotnet_diagnostic.CA1855.severity = error

# Incorrect usage of ConstantExpected attribute
dotnet_diagnostic.CA1856.severity = error

# The parameter expects a constant for optimal performance
dotnet_diagnostic.CA1857.severity = error

# Use StartsWith instead of IndexOf
dotnet_diagnostic.CA1858.severity = error

# Avoid using Enumerable.Any() extension method
dotnet_diagnostic.CA1860.severity = error

# Declare types in namespaces
dotnet_diagnostic.CA1050.severity = None
# Avoid constant arrays as arguments
dotnet_diagnostic.CA1861.severity = error

# Use the StringComparison method overloads to perform case-insensitive string comparisons
dotnet_diagnostic.CA1862.severity = error

# Use CompositeFormat
dotnet_diagnostic.CA1863.severity = error

# Prefer the IDictionary.TryAdd(TKey, TValue) method
dotnet_diagnostic.CA1864.severity = error

# Use string.Method(char) instead of string.Method(string) for string with single char
dotnet_diagnostic.CA1865.severity = error
dotnet_diagnostic.CA1866.severity = error
dotnet_diagnostic.CA1867.severity = error

# Unnecessary call to 'Contains' for sets
dotnet_diagnostic.CA1868.severity = error

# Cache and reuse 'JsonSerializerOptions' instances
dotnet_diagnostic.CA1869.severity = error

# Use a cached 'SearchValues' instance
dotnet_diagnostic.CA1870.severity = error


# Microsoft .NET properties
trim_trailing_whitespace = true
Expand Down
4 changes: 2 additions & 2 deletions src/AustralianElectorates/DataLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public static void ValidateElectorates(IEnumerable<string> names)
{
var missing = FindInvalidateElectorates(names)
.ToList();
if (missing.Any())
if (missing.Count != 0)
{
throw new ElectoratesNotFoundException(missing);
}
Expand All @@ -170,7 +170,7 @@ public static bool TryFindInvalidateElectorates(IEnumerable<string> names, out L
{
invalid = FindInvalidateElectorates(names)
.ToList();
return invalid.Any();
return invalid.Count != 0;
}

public static IEnumerable<string> FindInvalidateElectorates(params string[] names) =>
Expand Down
10 changes: 7 additions & 3 deletions src/AustralianElectorates/Serialization/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

static class Serializer
{
public static T Deserialize<T>(Stream stream)
static JsonSerializerOptions options;

static Serializer()
{
var options = new JsonSerializerOptions();
options = new();
options.Converters.Add(new StateConverter());
options.Converters.Add(new InterfaceConverter<TwoCandidatePreferred, ITwoCandidatePreferred>());
options.Converters.Add(new InterfaceConverter<Address, IAddress>());
Expand All @@ -17,9 +19,11 @@ public static T Deserialize<T>(Stream stream)
options.Converters.Add(new InterfaceConverter<Party, IParty>());
options.Converters.Add(new InterfaceConverter<Branch, IBranch>());
options.Converters.Add(new InterfaceConverter<StateMap, IStateMap>());
return JsonSerializer.Deserialize<T>(ReadToEnd(stream), options)!;
}

public static T Deserialize<T>(Stream stream) =>
JsonSerializer.Deserialize<T>(ReadToEnd(stream), options)!;

static string ReadToEnd(Stream stream)
{
using var reader = new StreamReader(stream);
Expand Down
2 changes: 1 addition & 1 deletion src/AustralianElectorates/ZipExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static void ExtractToDirectory(this ZipArchive archive, string directory)
var fileDirectory = Path.GetDirectoryName(completeFileName)!;
Directory.CreateDirectory(fileDirectory);

if (file.Name == "")
if (file.Name.Length == 0)
{
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static string ReplaceCaseless(this string str, string oldValue, string ne
var index = str.IndexOf(oldValue, StringComparison.OrdinalIgnoreCase);
while (index != -1)
{
stringBuilder.Append(str.Substring(previousIndex, index - previousIndex));
stringBuilder.Append(str.AsSpan(previousIndex, index - previousIndex));
stringBuilder.Append(newValue);
index += oldValue.Length;

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/GeoJsonExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void BoundingBox()
simpleBoundingBox);
}

public List<Feature> BuildFeatures(params Position[] positions) =>
static List<Feature> BuildFeatures(params Position[] positions) =>
[
new(
new Polygon(
Expand Down
4 changes: 2 additions & 2 deletions src/Tests/MetadataCleaner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ public static void CleanMetadata(FeatureCollection featureCollection, State? sta
{
if (!featureCollection
.Features.First()
.Properties.ContainsKey("Elect_div"))
.Properties.TryGetValue("Elect_div", out var value))
{
return;
}

foreach (var feature in featureCollection.Features)
{
var electorate = (string) feature.Properties["Elect_div"];
var electorate = (string)value;
var stateFromProperties = GetState(feature, state);
var area = (double) feature.Properties["Area_SqKm"];

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/Sync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public partial class ElectorateDataSet
writer.WriteLine(
$"""
public IElectorate {name}() =>
public static IElectorate {name}() =>
DataLoader.{name};
""");
}
Expand Down

0 comments on commit 838fa4a

Please sign in to comment.