Fix some analyzer warnings

also improve globalconfig readability
This commit is contained in:
Morilli 2023-11-03 11:56:23 +01:00
parent cdf89f4b09
commit 7d4b21f9c7
11 changed files with 74 additions and 72 deletions

View File

@ -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

View File

@ -461,10 +461,6 @@
<!-- Use the Regex source generator -->
<Rule Id="MA0110" Action="Error" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeAnalysis.FxCopAnalyzers" RuleNamespace="Microsoft.CodeAnalysis.FxCopAnalyzers">
<!-- Implement serialization constructors -->
<Rule Id="CA2229" Action="Hidden" />
</Rules>
<Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers.SpacingRules">
<!-- Keywords should be spaced correctly -->
<Rule Id="SA1000" Action="Hidden" />

View File

@ -0,0 +1,2 @@
[*.cs]
csharp_using_directive_placement = inside_namespace

View File

@ -10,9 +10,10 @@
<Import Project="LibCommon.props" />
<PropertyGroup>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<NoWarn>$(NoWarn)IDE0065;RS2008</NoWarn>
<NoWarn>$(NoWarn);RS2008</NoWarn>
</PropertyGroup>
<ItemGroup>
<EditorConfigFiles Include="$(MSBuildProjectDirectory)/../Analyzers.editorconfig" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.0" />
<Compile Include="$(MSBuildProjectDirectory)/../AnalyzersCommon/**/*.cs" />

View File

@ -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
/// <remarks>
/// 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 <see cref="ISourceGenerator.Execute"/> (I tried like 5 different things), do that instead.
/// --yoshi
/// </remarks>
private const int SAMPLE_SIZE = 20;
private string? _namespace;
private readonly List<string> _namespaces = new();
public string Namespace => _namespace ??= CalcNamespace();
private string CalcNamespace()
{
/// <remarks>
/// 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 <see cref="ISourceGenerator.Execute"/> (I tried like 5 different things), do that instead.
/// --yoshi
/// </remarks>
private const int SAMPLE_SIZE = 20;
private string? _namespace;
private readonly List<string> _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));
}
}

View File

@ -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
{

View File

@ -1,6 +1,4 @@
using BizHawk.Client.Common;
namespace BizHawk.Client.EmuHawk
namespace BizHawk.Client.EmuHawk
{
public partial class TAStudio : IControlMainform
{

View File

@ -1,6 +1,5 @@
#nullable disable
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

View File

@ -1,5 +1,3 @@
using System;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS

View File

@ -2,7 +2,6 @@ using System;
using System.Runtime.InteropServices;
using BizHawk.Common;
using BizHawk.Emulation.Common;
namespace BizHawk.Emulation.Cores.Consoles.Nintendo.NDS
{

View File

@ -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;