-
Notifications
You must be signed in to change notification settings - Fork 469
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
Allow static virtual members in CA1000 #7142
Conversation
edit: I found a better way |
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## main #7142 +/- ##
=======================================
Coverage 96.44% 96.44%
=======================================
Files 1413 1413
Lines 337719 337737 +18
Branches 11177 11178 +1
=======================================
+ Hits 325700 325724 +24
+ Misses 9200 9194 -6
Partials 2819 2819 |
public async Task CSharp_CA1000_ShouldNotGenerate_VirtualMember() | ||
{ | ||
await VerifyCS.VerifyAnalyzerAsync(@" | ||
#if NET7_OR_GREATER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we condition the entire test method on NET7_OR_GREATER
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although the test suite itself runs in .NET 8, when I run the test without the #if
I get diagnostics about the C# and .NET versions being insufficient
Diagnostics:
// /0/Test0.cs(5,23): hidden CA1000: Do not declare static members on generic types
VerifyCS.Diagnostic().WithSpan(5, 23, 5, 37).WithArguments("AbstractMember"),
// /0/Test0.cs(5,40): error CS8919: Target runtime doesn't support static abstract members in interfaces.
DiagnosticResult.CompilerError("CS8919").WithSpan(5, 40, 5, 43),
// /0/Test0.cs(7,27): hidden CA1000: Do not declare static members on generic types
VerifyCS.Diagnostic().WithSpan(7, 27, 7, 40).WithArguments("VirtualMember"),
// /0/Test0.cs(7,44): error CS8919: Target runtime doesn't support static abstract members in interfaces.
DiagnosticResult.CompilerError("CS8919").WithSpan(7, 44, 7, 46),
I know we can specify the C# version to test against but I could not figure out if there's a way to set the .NET version. I didn't bother setting the C# version because .NET 7 implies C# 11.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It turns out the #if
was causing nothing to be generated (since the .NET version we're testing with is less than 7).
So I specified C# 11 and added expected diagnostics about .NET 7 features. It doesn't look very pleasant, if there's a more elegant way I'd be happy to improve it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I figured out how to specify .NET 6. Thank you for bearing with my learning, I'm fairly new to this codebase.
I still get this warning in a .NET 8 project if I enable all analyzers. Using VS 17.11.4. <Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AnalysisLevel>latest</AnalysisLevel>
</PropertyGroup>
</Project> And here it gives public interface IFoo<T>
{
static abstract T Bar { get; }
static abstract int Baz { get; }
} |
Fixes #7126