-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
src/Neo.SmartContract.Analyzer/MultipleCatchBlockAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using System.Collections.Immutable; | ||
|
||
namespace Neo.SmartContract.Analyzer | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public class MultipleCatchBlockAnalyzer : DiagnosticAnalyzer | ||
{ | ||
public const string DiagnosticId = "NC4024"; | ||
|
||
private static readonly LocalizableString Title = "Multiple catch blocks are not allowed in Neo smart contracts"; | ||
private static readonly LocalizableString MessageFormat = "Neo smart contracts only support a single catch block: {0}"; | ||
private static readonly LocalizableString Description = "Neo smart contracts are limited to one catch block per try statement."; | ||
private const string Category = "Usage"; | ||
|
||
private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( | ||
DiagnosticId, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Error, | ||
isEnabledByDefault: true, | ||
description: Description); | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); | ||
context.EnableConcurrentExecution(); | ||
context.RegisterSyntaxNodeAction(AnalyzeTryStatement, SyntaxKind.TryStatement); | ||
} | ||
|
||
private static void AnalyzeTryStatement(SyntaxNodeAnalysisContext context) | ||
{ | ||
var tryStatement = (TryStatementSyntax)context.Node; | ||
|
||
if (tryStatement.Catches.Count > 1) | ||
{ | ||
var diagnostic = Diagnostic.Create(Rule, tryStatement.GetLocation(), tryStatement.Catches.Count); | ||
context.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
tests/Neo.SmartContract.Analyzer.UnitTests/MultipleCatchBlockAnalyzerUnitTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Verifier = Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier<Neo.SmartContract.Analyzer.MultipleCatchBlockAnalyzer>; | ||
|
||
namespace Neo.SmartContract.Analyzer.UnitTests | ||
{ | ||
[TestClass] | ||
public class MultipleCatchBlockAnalyzerUnitTest | ||
{ | ||
[TestMethod] | ||
public async Task MultipleCatchBlockAnalyzer_DetectMultipleCatchBlocks() | ||
{ | ||
const string sourceCode = """ | ||
using System; | ||
|
||
public class TestClass | ||
{ | ||
public void TestMethod() | ||
{ | ||
try | ||
{ | ||
// Some code that might throw an exception | ||
} | ||
catch (FormatException ex) | ||
{ | ||
// Handle general exception | ||
} | ||
catch (Exception ex) | ||
{ | ||
// Handle specific exception | ||
} | ||
} | ||
} | ||
"""; | ||
|
||
var expected = Verifier.Diagnostic(MultipleCatchBlockAnalyzer.DiagnosticId) | ||
.WithSpan(7, 9, 18, 10) | ||
.WithArguments("2"); | ||
|
||
await Verifier.VerifyAnalyzerAsync(sourceCode, expected); | ||
} | ||
} | ||
} |