Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/dotnes.analyzers.tests/NESAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -992,4 +992,41 @@ static class Config
var expected = Diagnostic(NESAnalyzer.NES012).WithLocation(0).WithArguments("Speed");
await VerifyLibraryAsync(test, expected);
}

// ==================== NES013: throw is not supported ====================

[Fact]
public async Task NES013_ThrowStatement_Diagnostic()
{
var test = """
{|#0:throw null;|}
while (true) ;
""";

var expected = Diagnostic(NESAnalyzer.NES013).WithLocation(0);
await VerifyAsync(test, expected);
}

[Fact]
public async Task NES013_ThrowExpression_Diagnostic()
{
var test = """
static byte GetValue(bool b) => b ? (byte)1 : {|#0:throw null|};
while (true) ;
""";

var expected = Diagnostic(NESAnalyzer.NES013).WithLocation(0);
await VerifyAsync(test, expected);
}

[Fact]
public async Task NES013_NoThrow_NoDiagnostic()
{
var test = """
byte x = 0;
while (true) ;
""";

await VerifyAsync(test);
}
}
6 changes: 6 additions & 0 deletions src/dotnes.analyzers/AnalyzerReleases.Unshipped.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
; Unshipped analyzer release
; https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md

### New Rules

Rule ID | Category | Severity | Notes
--------|----------|----------|-------
NES013 | NES | Error | throw is not supported
18 changes: 17 additions & 1 deletion src/dotnes.analyzers/NESAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class NESAnalyzer : DiagnosticAnalyzer
public const string NES010 = nameof(NES010);
public const string NES011 = nameof(NES011);
public const string NES012 = nameof(NES012);
public const string NES013 = nameof(NES013);

const string Category = "NES";

Expand Down Expand Up @@ -133,9 +134,18 @@ public class NESAnalyzer : DiagnosticAnalyzer
isEnabledByDefault: true,
description: "Properties generate hidden getter/setter methods that the NES transpiler cannot handle. Use public fields instead.");

static readonly DiagnosticDescriptor NES013Rule = new(
NES013,
"throw is not supported",
"throw is not supported on the NES. The 6502 CPU has no exception handling mechanism.",
Category,
DiagnosticSeverity.Error,
isEnabledByDefault: true,
description: "The NES 6502 CPU has no exception handling mechanism. throw statements and throw expressions are not supported.");

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics =>
ImmutableArray.Create(NES001Rule, NES002Rule, NES003Rule, NES004Rule, NES005Rule, NES006Rule,
NES007Rule, NES008Rule, NES009Rule, NES010Rule, NES011Rule, NES012Rule);
NES007Rule, NES008Rule, NES009Rule, NES010Rule, NES011Rule, NES012Rule, NES013Rule);

public override void Initialize(AnalysisContext context)
{
Expand All @@ -160,6 +170,7 @@ public override void Initialize(AnalysisContext context)
context.RegisterSyntaxNodeAction(AnalyzeForEach, SyntaxKind.ForEachStatement);
context.RegisterSyntaxNodeAction(AnalyzeTryCatchFinally, SyntaxKind.TryStatement);
context.RegisterSyntaxNodeAction(AnalyzePropertyDeclaration, SyntaxKind.PropertyDeclaration);
context.RegisterSyntaxNodeAction(AnalyzeThrowStatement, SyntaxKind.ThrowStatement, SyntaxKind.ThrowExpression);
}

static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
Expand Down Expand Up @@ -541,6 +552,11 @@ static void AnalyzePropertyDeclaration(SyntaxNodeAnalysisContext context)
context.ReportDiagnostic(Diagnostic.Create(NES012Rule, property.Identifier.GetLocation(), property.Identifier.Text));
}

static void AnalyzeThrowStatement(SyntaxNodeAnalysisContext context)
{
context.ReportDiagnostic(Diagnostic.Create(NES013Rule, context.Node.GetLocation()));
}

Comment thread
jonathanpeppers marked this conversation as resolved.
static bool IsSupportedType(ITypeSymbol type)
{
// byte, sbyte, ushort are supported
Expand Down