Skip to content

Commit

Permalink
Review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim-Pohlmann committed Jun 21, 2023
1 parent 4df673a commit 2a19fa3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,12 @@ public UnsafeCodeBlocks(IAnalyzerConfiguration configuration) : base(configurati
protected override void Initialize(SonarAnalysisContext context)
{
context.RegisterNodeAction(c => Report(c, ((UnsafeStatementSyntax)c.Node).UnsafeKeyword), SyntaxKind.UnsafeStatement);
context.RegisterNodeAction(c =>
{
if (c.Node is BaseMethodDeclarationSyntax { Modifiers: var modifiers })
{
ReportIfUnsafe(c, modifiers);
}
},
context.RegisterNodeAction(
c => ReportIfUnsafe(c, ((BaseMethodDeclarationSyntax)c.Node).Modifiers),
SyntaxKind.MethodDeclaration, SyntaxKind.ConstructorDeclaration, SyntaxKind.DestructorDeclaration, SyntaxKind.OperatorDeclaration);
context.RegisterNodeAction(c =>
{
if (c.Node is BaseTypeDeclarationSyntax { Modifiers: var modifiers }
&& modifiers.Find(SyntaxKind.UnsafeKeyword) is { } unsafeModifier)
{
Report(c, unsafeModifier);
}
}, SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKindEx.RecordClassDeclaration);
context.RegisterNodeAction(
c => ReportIfUnsafe(c, ((BaseTypeDeclarationSyntax)c.Node).Modifiers),
SyntaxKind.ClassDeclaration, SyntaxKind.InterfaceDeclaration, SyntaxKind.StructDeclaration, SyntaxKindEx.RecordClassDeclaration, SyntaxKindEx.RecordStructDeclaration);
}

private void ReportIfUnsafe(SonarSyntaxNodeReportingContext context, SyntaxTokenList modifiers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void UnsafeRecord() =>

[TestMethod]
public void UnsafeRecordStruct() =>
builder.AddSnippet("""unsafe record struct MyRecord(byte* Pointer); // FN""")
builder.AddSnippet("""unsafe record struct MyRecord(byte* Pointer); // Noncompliant""")
.WithOptions(ParseOptionsHelper.FromCSharp10).Verify();

#endif
Expand Down

0 comments on commit 2a19fa3

Please sign in to comment.