Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve S6640: Support type declarations #7486

Merged
merged 3 commits into from
Jun 21, 2023
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
30 changes: 30 additions & 0 deletions analyzers/its/expected/Net5/Net5--net5.0-S6640.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"issues": [
{
"id": "S6640",
"message": "Make sure that using "unsafe" is safe here.",
"location": {
"uri": "sources\Net5\Net5\FunctionPointers.cs",
"region": {
"startLine": 3,
"startColumn": 12,
"endLine": 3,
"endColumn": 18
}
}
},
{
"id": "S6640",
"message": "Make sure that using "unsafe" is safe here.",
"location": {
"uri": "sources\Net5\Net5\S4000.cs",
"region": {
"startLine": 5,
"startColumn": 12,
"endLine": 5,
"endColumn": 18
}
}
}
Comment on lines +3 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TPs

]
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +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 => 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 @@ -36,12 +36,12 @@ public void UnsafeCodeBlocks() =>

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

[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
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ unsafe void Local(byte* pointer) { } // FN
void SafeLocal(byte noPointer) { } // Compliant
}

unsafe class UnsafeClass { } // FN
unsafe class UnsafeClass { } // Noncompliant

unsafe struct UnsafeStruct // FN
unsafe struct UnsafeStruct // Noncompliant
{
unsafe fixed byte unsafeFixedBuffer[16]; // FN
}

unsafe interface IUnsafeInterface { } // FN
unsafe interface IUnsafeInterface { } // Noncompliant

unsafe Sample(byte* pointer) { } // Noncompliant

Expand Down