-
Notifications
You must be signed in to change notification settings - Fork 470
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
Add [ThreadStatic] correctness analyzers #5920
Conversation
Codecov Report
@@ Coverage Diff @@
## main #5920 +/- ##
==========================================
- Coverage 96.03% 96.03% -0.01%
==========================================
Files 1310 1312 +2
Lines 302825 302990 +165
Branches 9572 9579 +7
==========================================
+ Hits 290808 290964 +156
- Misses 9733 9739 +6
- Partials 2284 2287 +3 |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
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.
Edge cases to consider whether they should be supported:
using System;
public class C
{
[field: ThreadStatic]
public string S { get; }
[field: ThreadStatic]
event EventHandler E;
}
public record R([field: ThreadStatic] string S);
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseThreadStaticCorrectly.cs
Outdated
Show resolved
Hide resolved
2585e3c
to
c67a612
Compare
I added support for properties (which implicitly adds support for records). I don't believe it's possible today to find the backing field for an IEventSymbol. |
Thanks for reviewing, @Youssef1313. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/UseThreadStaticCorrectly.cs
Outdated
Show resolved
Hide resolved
c67a612
to
9e05a37
Compare
Fixes dotnet/runtime#46626
Note that we'd discussed adding a fixer for the case of
[ThreadStatic]
applied to an instance field, but on further thought I don't think that's a good idea. If someone is using[ThreadStatic]
on an instance field, it's possible it's a mistake and they intended for it to be static, but it's also quite likely they assumed it would create a per-thread instance value (as you might get withThreadLocal<T>
) so providing a fix to just mark it static would potentially lead someone down the wrong path, and just addingstatic
is trivial for someone to do. There should also be very few violations of this, so a bulk fix isn't going to be meaningful.