Add Analyzer to require `class`/`struct` keyword on records

fixes eef190d33, e8c957a7a, and c68dd703d
This commit is contained in:
YoshiRulz 2024-10-19 04:42:58 +10:00
parent c68dd703dc
commit a3901f66e5
No known key found for this signature in database
GPG Key ID: C4DE31C245353FB7
7 changed files with 28 additions and 4 deletions

View File

@ -35,6 +35,8 @@ dotnet_diagnostic.BHI1105.severity = error
dotnet_diagnostic.BHI1110.severity = warning
# Expression-bodied member should be flowed to next line correctly
dotnet_diagnostic.BHI1120.severity = silent
# Record type declaration missing class (or struct) keyword
dotnet_diagnostic.BHI1130.severity = error
# Check result of IDictionary.TryGetValue, or discard it if default(T) is desired
dotnet_diagnostic.BHI1200.severity = error

View File

@ -69,6 +69,14 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor DiagRecordImplicitlyRefType = new(
id: "BHI1130",
title: "Record type declaration missing class (or struct) keyword",
messageFormat: "Add class (or struct) keyword",
category: "Usage",
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true);
private static readonly DiagnosticDescriptor DiagSwitchShouldThrowIOE = new(
id: "BHI1005",
title: "Default branch of switch expression should throw InvalidOperationException/SwitchExpressionException or not throw",
@ -84,6 +92,7 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer
DiagNoAnonDelegates,
DiagNoDiscardingLocals,
DiagNoQueryExpression,
DiagRecordImplicitlyRefType,
DiagSwitchShouldThrowIOE);
public override void Initialize(AnalysisContext context)
@ -147,6 +156,9 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer
case QueryExpressionSyntax:
snac.ReportDiagnostic(Diagnostic.Create(DiagNoQueryExpression, snac.Node.GetLocation()));
break;
case RecordDeclarationSyntax rds:
if (!rds.ClassOrStructKeyword.ToFullString().Contains("class")) snac.ReportDiagnostic(Diagnostic.Create(DiagRecordImplicitlyRefType, rds.GetLocation()));
break;
case SwitchExpressionArmSyntax { WhenClause: null, Pattern: DiscardPatternSyntax, Expression: ThrowExpressionSyntax tes }:
var thrownExceptionType = snac.SemanticModel.GetThrownExceptionType(tes);
if (thrownExceptionType is null)
@ -173,6 +185,7 @@ public class HawkSourceAnalyzer : DiagnosticAnalyzer
SyntaxKind.InterpolatedStringExpression,
SyntaxKind.ListPattern,
SyntaxKind.QueryExpression,
SyntaxKind.RecordDeclaration,
SyntaxKind.SimpleAssignmentExpression,
SyntaxKind.SwitchExpressionArm);
}

View File

@ -132,6 +132,14 @@ public sealed class HawkSourceAnalyzerTests
}
""");
[TestMethod]
public Task CheckMisuseOfRecordDeclKeywords()
=> Verify.VerifyAnalyzerAsync("""
internal record struct Y {}
internal record class Z {}
{|BHI1130:internal record A {}|}
""");
[TestMethod]
public Task CheckMisuseOfQuerySyntax()
=> Verify.VerifyAnalyzerAsync("""

Binary file not shown.

View File

@ -143,6 +143,7 @@ namespace BizHawk.Bizware.Graphics
private List<ITexture2D> TexturePages = [ ];
private readonly Dictionary<char, TexCoords> CharTexCoords = [ ];
private record TexCoords(float U0, float V0, float U1, float V1);
/// <remarks>TODO can this be a struct? it's only 16o and only used here, in the above dict</remarks>
private sealed record class TexCoords(float U0, float V0, float U1, float V1);
}
}

View File

@ -74,7 +74,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.BSNES
private SnesSettings _settings;
private SnesSyncSettings _syncSettings;
public record SnesSettings : SNES.IBSNESForGfxDebugger.SettingsObj
public sealed record class SnesSettings : SNES.IBSNESForGfxDebugger.SettingsObj
{
public bool ShowBG1_0 { get; set; } = true;
public bool ShowBG2_0 { get; set; } = true;
@ -97,7 +97,7 @@ namespace BizHawk.Emulation.Cores.Nintendo.BSNES
public BsnesApi.ASPECT_RATIO_CORRECTION AspectRatioCorrection { get; set; } = BsnesApi.ASPECT_RATIO_CORRECTION.Auto;
}
public record SnesSyncSettings
public sealed record class SnesSyncSettings
{
public BsnesApi.BSNES_PORT1_INPUT_DEVICE LeftPort { get; set; } = BsnesApi.BSNES_PORT1_INPUT_DEVICE.Gamepad;

View File

@ -1,6 +1,6 @@
namespace BizHawk.Emulation.Cores.Nintendo.N64
{
public record N64Settings
public sealed record class N64Settings
{
public int VideoSizeX = 320;
public int VideoSizeY = 240;