Skip to content

Commit

Permalink
Fixes microsoft#456. VSTHRD010 now recognizes when struct members req…
Browse files Browse the repository at this point in the history
…uire the main thread.
  • Loading branch information
bluetarpmedia committed Jun 28, 2021
1 parent a0ab9d8 commit c3db4ce
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Expand Up @@ -443,7 +443,7 @@ private bool AnalyzeMemberWithinContext(ITypeSymbol type, ISymbol? symbol, Synta
throw new ArgumentNullException(nameof(type));
}

bool requiresUIThread = (type.TypeKind == TypeKind.Interface || type.TypeKind == TypeKind.Class)
bool requiresUIThread = (type.TypeKind == TypeKind.Interface || type.TypeKind == TypeKind.Class || type.TypeKind == TypeKind.Struct)
&& this.MembersRequiringMainThread.Contains(type, symbol);

if (requiresUIThread)
Expand Down
@@ -1,4 +1,5 @@
[TestNS.*]
[TestNS.SomeStruct]
![TestNS.FreeThreadedType]
![TestNS2.FreeThreadedType]
[TestNS2.*]
Expand Down
Expand Up @@ -1864,6 +1864,46 @@ async void SecondAsync()
await Verify.VerifyAnalyzerAsync(test, expect);
}

[Fact]
public async Task StructMembers()
{
var test = @"
namespace TestNS
{
struct SomeStruct
{
public static void DoSomething()
{
}
public string Name { get; set; }
}
}
namespace Foo
{
class MyProgram
{
static void Main()
{
TestNS.SomeStruct.DoSomething();
var st = new TestNS.SomeStruct();
st.Name = ""TheValue"";
string val = st.Name;
}
}
}
";
var expect = new DiagnosticResult[]
{
Verify.Diagnostic(DescriptorSync).WithSpan(20, 31, 20, 42).WithArguments("SomeStruct", "Test.VerifyOnUIThread"),
Verify.Diagnostic(DescriptorSync).WithSpan(23, 16, 23, 20).WithArguments("SomeStruct", "Test.VerifyOnUIThread"),
Verify.Diagnostic(DescriptorSync).WithSpan(24, 29, 24, 33).WithArguments("SomeStruct", "Test.VerifyOnUIThread"),
};
await Verify.VerifyAnalyzerAsync(test, expect);
}

private static class CodeFixIndex
{
public const int SwitchToMainThreadAsync = 0;
Expand Down

0 comments on commit c3db4ce

Please sign in to comment.