diff --git a/src/.editorconfig b/src/.editorconfig index ca231ab5..b9aab4ca 100644 --- a/src/.editorconfig +++ b/src/.editorconfig @@ -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.Clear() instead of Span.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 diff --git a/src/AustralianElectorates/DataLoader.cs b/src/AustralianElectorates/DataLoader.cs index d9b5bdab..30e09301 100644 --- a/src/AustralianElectorates/DataLoader.cs +++ b/src/AustralianElectorates/DataLoader.cs @@ -160,7 +160,7 @@ public static void ValidateElectorates(IEnumerable names) { var missing = FindInvalidateElectorates(names) .ToList(); - if (missing.Any()) + if (missing.Count != 0) { throw new ElectoratesNotFoundException(missing); } @@ -170,7 +170,7 @@ public static bool TryFindInvalidateElectorates(IEnumerable names, out L { invalid = FindInvalidateElectorates(names) .ToList(); - return invalid.Any(); + return invalid.Count != 0; } public static IEnumerable FindInvalidateElectorates(params string[] names) => diff --git a/src/AustralianElectorates/Serialization/Serializer.cs b/src/AustralianElectorates/Serialization/Serializer.cs index a423f10d..d6724eff 100644 --- a/src/AustralianElectorates/Serialization/Serializer.cs +++ b/src/AustralianElectorates/Serialization/Serializer.cs @@ -3,9 +3,11 @@ static class Serializer { - public static T Deserialize(Stream stream) + static JsonSerializerOptions options; + + static Serializer() { - var options = new JsonSerializerOptions(); + options = new(); options.Converters.Add(new StateConverter()); options.Converters.Add(new InterfaceConverter()); options.Converters.Add(new InterfaceConverter()); @@ -17,9 +19,11 @@ public static T Deserialize(Stream stream) options.Converters.Add(new InterfaceConverter()); options.Converters.Add(new InterfaceConverter()); options.Converters.Add(new InterfaceConverter()); - return JsonSerializer.Deserialize(ReadToEnd(stream), options)!; } + public static T Deserialize(Stream stream) => + JsonSerializer.Deserialize(ReadToEnd(stream), options)!; + static string ReadToEnd(Stream stream) { using var reader = new StreamReader(stream); diff --git a/src/AustralianElectorates/ZipExtensions.cs b/src/AustralianElectorates/ZipExtensions.cs index 75d74829..b88cbb9b 100644 --- a/src/AustralianElectorates/ZipExtensions.cs +++ b/src/AustralianElectorates/ZipExtensions.cs @@ -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; } diff --git a/src/Tests/Extensions.cs b/src/Tests/Extensions.cs index 5424e4dc..91fd741c 100644 --- a/src/Tests/Extensions.cs +++ b/src/Tests/Extensions.cs @@ -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; diff --git a/src/Tests/GeoJsonExtensionsTests.cs b/src/Tests/GeoJsonExtensionsTests.cs index 57c85381..834e3a38 100644 --- a/src/Tests/GeoJsonExtensionsTests.cs +++ b/src/Tests/GeoJsonExtensionsTests.cs @@ -24,7 +24,7 @@ public void BoundingBox() simpleBoundingBox); } - public List BuildFeatures(params Position[] positions) => + static List BuildFeatures(params Position[] positions) => [ new( new Polygon( diff --git a/src/Tests/MetadataCleaner.cs b/src/Tests/MetadataCleaner.cs index 575a71fd..fb670ad4 100644 --- a/src/Tests/MetadataCleaner.cs +++ b/src/Tests/MetadataCleaner.cs @@ -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"]; diff --git a/src/Tests/Sync.cs b/src/Tests/Sync.cs index 11f60658..edec5811 100644 --- a/src/Tests/Sync.cs +++ b/src/Tests/Sync.cs @@ -409,7 +409,7 @@ public partial class ElectorateDataSet writer.WriteLine( $""" - public IElectorate {name}() => + public static IElectorate {name}() => DataLoader.{name}; """); }