Skip to content

Commit

Permalink
Merge pull request #869 from bluetarpmedia/Fix456
Browse files Browse the repository at this point in the history
VSTHRD010 now recognizes when struct members require the main thread
  • Loading branch information
AArnott authored Jul 9, 2021
2 parents b0f7170 + 74996f4 commit 127e17d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[TestNS.*]
[TestNS.SomeStruct]
![TestNS.FreeThreadedType]
![TestNS2.FreeThreadedType]
[TestNS2.*]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,7 +970,7 @@ public async Task CastToManagedType_ProducesNoDiagnostic()
namespace TestNS {
class SomeClass { }
class SomeInterface { }
interface SomeInterface { }
}
class Test {
Expand Down Expand Up @@ -1053,7 +1053,7 @@ public async Task CastToManagedTypeViaAs_ProducesNoDiagnostic()
namespace TestNS {
class SomeClass { }
class SomeInterface { }
interface SomeInterface { }
}
class Test {
Expand Down Expand Up @@ -1096,7 +1096,7 @@ public async Task CastToManagedTypeViaIs_ProducesNoDiagnostic()
namespace TestNS {
class SomeClass { }
class SomeInterface { }
interface SomeInterface { }
}
class Test {
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 127e17d

Please sign in to comment.