Skip to content

Commit

Permalink
Merge pull request #7142 from RenderMichael/main
Browse files Browse the repository at this point in the history
Allow static virtual members in CA1000
  • Loading branch information
mavasani authored Jan 16, 2024
2 parents 2e64f1f + 9358402 commit fe0b19a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public override void Initialize(AnalysisContext context)
return;
}

// Virtual members on generic types can't be called directly, so they don't suffer the problem this analyzer exists to prevent.
if (symbol.IsAbstract || symbol.IsVirtual)
{
return;
}

symbolAnalysisContext.ReportDiagnostic(symbol.CreateDiagnostic(Rule, symbol.Name));
}, SymbolKind.Method, SymbolKind.Property);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
Expand Down Expand Up @@ -428,6 +428,28 @@ public override int GetHashCode()
");
}

[Fact, WorkItem(7126, "https://github.com/dotnet/roslyn-analyzers/issues/7126")]
public async Task CSharp_CA1000_ShouldNotGenerate_VirtualMember()
{
string code = @"
public interface ITestInterface<T>
{
static abstract T AbstractMember { get; }
static virtual string VirtualMember => """";
}
";
await new VerifyCS.Test
{
LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp11,
TestState =
{
Sources = { code },
ReferenceAssemblies = ReferenceAssemblies.Net.Net60
}
}.RunAsync();
}

private static DiagnosticResult GetCSharpResultAt(int line, int column)
#pragma warning disable RS0030 // Do not use banned APIs
=> VerifyCS.Diagnostic().WithLocation(line, column);
Expand Down

0 comments on commit fe0b19a

Please sign in to comment.