From 7d4b21f9c712955445d245ae13c5908d39f8c4e2 Mon Sep 17 00:00:00 2001
From: Morilli <35152647+Morilli@users.noreply.github.com>
Date: Fri, 3 Nov 2023 11:56:23 +0100
Subject: [PATCH] Fix some analyzer warnings
also improve globalconfig readability
---
.globalconfig | 21 +++-
Common.ruleset | 4 -
ExternalProjects/Analyzers.editorconfig | 2 +
ExternalProjects/AnalyzersCommon.props | 3 +-
.../ReflectionCacheGenerator.cs | 101 +++++++++---------
.../VersionInfoGenerator.cs | 6 +-
.../TAStudio/TAStudio.IControlMainForm.cs | 4 +-
src/BizHawk.Common/Win32/Win32Imports.cs | 1 -
.../Consoles/Nintendo/NDS/MelonDS.ISaveRam.cs | 2 -
.../Consoles/Nintendo/NDS/NDSFirmware.cs | 1 -
.../Waterbox/NymaCore.Settings.cs | 1 -
11 files changed, 74 insertions(+), 72 deletions(-)
create mode 100644 ExternalProjects/Analyzers.editorconfig
diff --git a/.globalconfig b/.globalconfig
index ac86222fc3..f53702b93c 100644
--- a/.globalconfig
+++ b/.globalconfig
@@ -1,18 +1,29 @@
is_global = true
-# Globalization rules
-dotnet_code_quality.CA1305.excluded_symbol_names = T:System.Byte|T:System.SByte|T:System.Int16|T:System.UInt16|T:System.Int32|T:System.UInt32|T:System.Int64|T:System.UInt64|T:System.String|T:System.Text.StringBuilder|T:System.Convert
+## Globalization rules
+# Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = error
+dotnet_code_quality.CA1305.excluded_symbol_names = T:System.Byte|T:System.SByte|T:System.Int16|T:System.UInt16|T:System.Int32|T:System.UInt32|T:System.Int64|T:System.UInt64|T:System.String|T:System.Text.StringBuilder|T:System.Convert
+# Specify marshalling for P/Invoke string arguments
dotnet_diagnostic.CA2101.severity = suggestion
-# Performance rules
-dotnet_code_quality.CA1826.exclude_ordefault_methods = true
+## Performance rules
+# Do not initialize unnecessarily
dotnet_diagnostic.CA1805.severity = silent
+# Mark members as static
dotnet_diagnostic.CA1822.severity = silent
+# Use property instead of Linq Enumerable method
+dotnet_code_quality.CA1826.exclude_ordefault_methods = true
+# Avoid StringBuilder parameters for P/Invokes
dotnet_diagnostic.CA1838.severity = suggestion
-# Usage rules
+## Usage rules
+
+# Call GC.SuppressFinalize correctly
dotnet_diagnostic.CA1816.severity = none
+# Do not raise reserved exception types
dotnet_diagnostic.CA2201.severity = suggestion
+# Implement serialization constructors
+dotnet_diagnostic.CA2229.severity = silent
diff --git a/Common.ruleset b/Common.ruleset
index c08d225c28..c0d05e8bfa 100644
--- a/Common.ruleset
+++ b/Common.ruleset
@@ -461,10 +461,6 @@
-
-
-
-
diff --git a/ExternalProjects/Analyzers.editorconfig b/ExternalProjects/Analyzers.editorconfig
new file mode 100644
index 0000000000..9af7174c26
--- /dev/null
+++ b/ExternalProjects/Analyzers.editorconfig
@@ -0,0 +1,2 @@
+[*.cs]
+csharp_using_directive_placement = inside_namespace
diff --git a/ExternalProjects/AnalyzersCommon.props b/ExternalProjects/AnalyzersCommon.props
index 1944658b9b..d475303787 100644
--- a/ExternalProjects/AnalyzersCommon.props
+++ b/ExternalProjects/AnalyzersCommon.props
@@ -10,9 +10,10 @@
true
- $(NoWarn)IDE0065;RS2008
+ $(NoWarn);RS2008
+
diff --git a/ExternalProjects/BizHawk.SrcGen.ReflectionCache/ReflectionCacheGenerator.cs b/ExternalProjects/BizHawk.SrcGen.ReflectionCache/ReflectionCacheGenerator.cs
index b1f363e4f3..7170b791e8 100644
--- a/ExternalProjects/BizHawk.SrcGen.ReflectionCache/ReflectionCacheGenerator.cs
+++ b/ExternalProjects/BizHawk.SrcGen.ReflectionCache/ReflectionCacheGenerator.cs
@@ -1,3 +1,5 @@
+namespace BizHawk.SrcGen.ReflectionCache;
+
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,61 +9,59 @@ using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
-namespace BizHawk.SrcGen.ReflectionCache
+[Generator]
+public sealed class ReflectionCacheGenerator : ISourceGenerator
{
- [Generator]
- public sealed class ReflectionCacheGenerator : ISourceGenerator
+ private sealed class ReflectionCacheGenSyntaxReceiver : ISyntaxReceiver
{
- private sealed class ReflectionCacheGenSyntaxReceiver : ISyntaxReceiver
+ ///
+ /// I may have just added RNG to the build process...
+ /// Increase this sample size to decrease chance of random failure.
+ /// Alternatively, if you can come up with a better way of getting the project name in (I tried like 5 different things), do that instead.
+ /// --yoshi
+ ///
+ private const int SAMPLE_SIZE = 20;
+
+ private string? _namespace;
+
+ private readonly List _namespaces = new();
+
+ public string Namespace => _namespace ??= CalcNamespace();
+
+ private string CalcNamespace()
{
- ///
- /// I may have just added RNG to the build process...
- /// Increase this sample size to decrease chance of random failure.
- /// Alternatively, if you can come up with a better way of getting the project name in (I tried like 5 different things), do that instead.
- /// --yoshi
- ///
- private const int SAMPLE_SIZE = 20;
-
- private string? _namespace;
-
- private readonly List _namespaces = new();
-
- public string Namespace => _namespace ??= CalcNamespace();
-
- private string CalcNamespace()
- {
- // black magic wizardry to find common prefix https://stackoverflow.com/a/35081977
- var ns = new string(_namespaces[0]
- .Substring(0, _namespaces.Min(s => s.Length))
- .TakeWhile((c, i) => _namespaces.TrueForAll(s => s[i] == c))
- .ToArray());
- return ns[ns.Length - 1] == '.' ? ns.Substring(0, ns.Length - 1) : ns; // trim trailing '.' (can't use BizHawk.Common from Source Generators)
- }
-
- public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
- {
- static string Ser(NameSyntax nameSyn) => nameSyn switch
- {
- SimpleNameSyntax simple => simple.Identifier.ValueText,
- QualifiedNameSyntax qual => $"{Ser(qual.Left)}.{Ser(qual.Right)}",
- _ => throw new InvalidOperationException()
- };
- if (_namespace != null || syntaxNode is not NamespaceDeclarationSyntax syn) return;
- var newNS = Ser(syn.Name);
- if (!newNS.StartsWith("BizHawk.", StringComparison.Ordinal)) return;
- _namespaces.Add(newNS);
- if (_namespaces.Count == SAMPLE_SIZE) _namespace = CalcNamespace();
- }
+ // black magic wizardry to find common prefix https://stackoverflow.com/a/35081977
+ var ns = new string(_namespaces[0]
+ .Substring(0, _namespaces.Min(s => s.Length))
+ .TakeWhile((c, i) => _namespaces.TrueForAll(s => s[i] == c))
+ .ToArray());
+ return ns[ns.Length - 1] == '.' ? ns.Substring(0, ns.Length - 1) : ns; // trim trailing '.' (can't use BizHawk.Common from Source Generators)
}
- public void Initialize(GeneratorInitializationContext context)
- => context.RegisterForSyntaxNotifications(() => new ReflectionCacheGenSyntaxReceiver());
-
- public void Execute(GeneratorExecutionContext context)
+ public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
- if (context.SyntaxReceiver is not ReflectionCacheGenSyntaxReceiver receiver) return;
- var nSpace = receiver.Namespace;
- var src = $@"#nullable enable
+ static string Ser(NameSyntax nameSyn) => nameSyn switch
+ {
+ SimpleNameSyntax simple => simple.Identifier.ValueText,
+ QualifiedNameSyntax qual => $"{Ser(qual.Left)}.{Ser(qual.Right)}",
+ _ => throw new InvalidOperationException()
+ };
+ if (_namespace != null || syntaxNode is not NamespaceDeclarationSyntax syn) return;
+ var newNS = Ser(syn.Name);
+ if (!newNS.StartsWith("BizHawk.", StringComparison.Ordinal)) return;
+ _namespaces.Add(newNS);
+ if (_namespaces.Count == SAMPLE_SIZE) _namespace = CalcNamespace();
+ }
+ }
+
+ public void Initialize(GeneratorInitializationContext context)
+ => context.RegisterForSyntaxNotifications(() => new ReflectionCacheGenSyntaxReceiver());
+
+ public void Execute(GeneratorExecutionContext context)
+ {
+ if (context.SyntaxReceiver is not ReflectionCacheGenSyntaxReceiver receiver) return;
+ var nSpace = receiver.Namespace;
+ var src = $@"#nullable enable
using System;
using System.Collections.Generic;
@@ -103,7 +103,6 @@ namespace {nSpace}
}}
}}
";
- context.AddSource("ReflectionCache.cs", SourceText.From(src, Encoding.UTF8));
- }
+ context.AddSource("ReflectionCache.cs", SourceText.From(src, Encoding.UTF8));
}
}
diff --git a/ExternalProjects/BizHawk.SrcGen.VersionInfo/VersionInfoGenerator.cs b/ExternalProjects/BizHawk.SrcGen.VersionInfo/VersionInfoGenerator.cs
index 6a6e0b9281..62dcb4ff45 100644
--- a/ExternalProjects/BizHawk.SrcGen.VersionInfo/VersionInfoGenerator.cs
+++ b/ExternalProjects/BizHawk.SrcGen.VersionInfo/VersionInfoGenerator.cs
@@ -1,11 +1,11 @@
-using System;
+namespace BizHawk.SrcGen.VersionInfo;
+
+using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis;
-namespace BizHawk.SrcGen.VersionInfo;
-
[Generator]
public class VersionInfoGenerator : ISourceGenerator
{
diff --git a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IControlMainForm.cs b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IControlMainForm.cs
index cd2f790fa3..f4451d97e1 100644
--- a/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IControlMainForm.cs
+++ b/src/BizHawk.Client.EmuHawk/tools/TAStudio/TAStudio.IControlMainForm.cs
@@ -1,6 +1,4 @@
-using BizHawk.Client.Common;
-
-namespace BizHawk.Client.EmuHawk
+namespace BizHawk.Client.EmuHawk
{
public partial class TAStudio : IControlMainform
{
diff --git a/src/BizHawk.Common/Win32/Win32Imports.cs b/src/BizHawk.Common/Win32/Win32Imports.cs
index df119cc66e..bbff17584b 100644
--- a/src/BizHawk.Common/Win32/Win32Imports.cs
+++ b/src/BizHawk.Common/Win32/Win32Imports.cs
@@ -1,6 +1,5 @@
#nullable disable
-using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISaveRam.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISaveRam.cs
index 621a9c5734..f1f472a9c1 100644
--- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISaveRam.cs
+++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/MelonDS.ISaveRam.cs
@@ -1,5 +1,3 @@
-using System;
-
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
diff --git a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/NDSFirmware.cs b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/NDSFirmware.cs
index 9d8937a605..5f0908e05c 100644
--- a/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/NDSFirmware.cs
+++ b/src/BizHawk.Emulation.Cores/Consoles/Nintendo/NDS/NDSFirmware.cs
@@ -2,7 +2,6 @@ using System;
using System.Runtime.InteropServices;
using BizHawk.Common;
-using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
{
diff --git a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Settings.cs b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Settings.cs
index 0d4dbdbeb4..12737402d0 100644
--- a/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Settings.cs
+++ b/src/BizHawk.Emulation.Cores/Waterbox/NymaCore.Settings.cs
@@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
-using BizHawk.BizInvoke;
using BizHawk.Emulation.Common;
using NymaTypes;