提交 5b70e999 编写于 作者: M Manish Vasani

Tweak bail out from unused member analyzer for struct layout types

Current implementation bailed out in symbol start action, which caused both analysis and reporting to be skipped for unused members in a type with StructLayout attribute. New implementation bails out in symbol end action, so we only skip reporting not analysis.

Fixes #32727
上级 19628761
......@@ -2084,6 +2084,26 @@ class C
}}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedMembers)]
[WorkItem(32727, "https://github.com/dotnet/roslyn/issues/32727")]
public async Task NestedStructLayoutTypeWithReference()
{
await TestDiagnosticMissingAsync(
@"using System.Runtime.InteropServices;
class Program
{
private const int [|MAX_PATH|] = 260;
[StructLayout(LayoutKind.Sequential)]
internal struct ProcessEntry32
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)]
public string szExeFile;
}
}");
}
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnusedMembers)]
public async Task FixAllFields_Document()
{
......
......@@ -154,13 +154,6 @@ private void RegisterActions(CompilationStartAnalysisContext compilationStartCon
Action<ISymbol, ValueUsageInfo> onSymbolUsageFound = OnSymbolUsage;
compilationStartContext.RegisterSymbolStartAction(symbolStartContext =>
{
if (symbolStartContext.Symbol.GetAttributes().Any(a => a.AttributeClass == _structLayoutAttributeType))
{
// Bail out for types with 'StructLayoutAttribute' as the ordering of the members is critical,
// and removal of unused members might break semantics.
return;
}
var hasInvalidOrDynamicOperation = false;
symbolStartContext.RegisterOperationAction(AnalyzeMemberReferenceOperation, OperationKind.FieldReference, OperationKind.MethodReference, OperationKind.PropertyReference, OperationKind.EventReference);
symbolStartContext.RegisterOperationAction(AnalyzeFieldInitializer, OperationKind.FieldInitializer);
......@@ -351,6 +344,13 @@ private void OnSymbolEnd(SymbolAnalysisContext symbolEndContext, bool hasInvalid
return;
}
if (symbolEndContext.Symbol.GetAttributes().Any(a => a.AttributeClass == _structLayoutAttributeType))
{
// Bail out for types with 'StructLayoutAttribute' as the ordering of the members is critical,
// and removal of unused members might break semantics.
return;
}
// Report diagnostics for unused candidate members.
var first = true;
PooledHashSet<ISymbol> symbolsReferencedInDocComments = null;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册