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

CA2237: Don't report diagnostic when ISerializable is inherited #6997

Merged
merged 1 commit into from
Nov 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public void AnalyzeSymbol(SymbolAnalysisContext context)
return;
}

var implementsISerializable = namedTypeSymbol.AllInterfaces.Contains(_iserializableTypeSymbol);
var implementsISerializable = namedTypeSymbol.Interfaces.Contains(_iserializableTypeSymbol);
var isSerializable = IsSerializable(namedTypeSymbol);

// If the type is public and implements ISerializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Test.Utilities;
using Xunit;
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<
Microsoft.NetCore.Analyzers.Runtime.SerializationRulesDiagnosticAnalyzer,
Expand Down Expand Up @@ -177,6 +178,42 @@ ReadOnly Property Name() As String
End Interface");
}

[Fact, WorkItem(6916, "https://github.com/dotnet/roslyn-analyzers/issues/6916")]
public async Task CA2237_NoDiagnosticForSerializableImplementedOnBaseClassAsync()
{
await VerifyCS.VerifyAnalyzerAsync(@"
using System;
using System.Runtime.Serialization;

public class B : A { }

[Serializable]
public class A : ISerializable
{
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
throw new NotImplementedException();
}
}");

await VerifyVB.VerifyAnalyzerAsync(@"
Imports System
Imports System.Runtime.Serialization

Class B
Inherits A
End Class

<Serializable>
Public Class A
Implements ISerializable

Public Sub GetObjectData(info as SerializationInfo, context as StreamingContext) Implements ISerializable.GetObjectData
throw new NotImplementedException()
End Sub
End Class");
}

private static DiagnosticResult GetCA2237CSharpResultAt(int line, int column, string objectName) =>
#pragma warning disable RS0030 // Do not use banned APIs
VerifyCS.Diagnostic(SerializationRulesDiagnosticAnalyzer.RuleCA2237)
Expand Down