Skip to content
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

Mark used deconstruct methods. #76012

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3271,4 +3271,73 @@ static IEnumerator GetEnumerator(this Range range)
ReferenceAssemblies = ReferenceAssemblies.Net.Net90,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75995")]
public async Task KeepUsedDeconstructMethod()
{
await new VerifyCS.Test
{
TestCode = """
#nullable enable

class C
{
public void M(
ref object? o,
ref object? p)
{
(o, p) = this;
}

void Deconstruct(
out object? o,
out object? p)
{
o = null;
p = null;
}
}
""",
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/75995")]
public async Task RemoveUnusedDeconstructMethod()
{
await new VerifyCS.Test
{
TestCode = """
#nullable enable

class C
{
public void M(
ref object o,
ref object p)
{
}

void [|Deconstruct|](
out object? o,
out object? p)
{
o = null;
p = null;
}
}
""",
FixedCode = """
#nullable enable

class C
{
public void M(
ref object o,
ref object p)
{
}
}
""",
}.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,22 @@ private void RegisterActions(CompilationStartAnalysisContext compilationStartCon
if (!ShouldAnalyze(symbolStartContext, (INamedTypeSymbol)symbolStartContext.Symbol))
return;

var hasUnsupportedOperation = false;
symbolStartContext.RegisterOperationAction(AnalyzeMemberReferenceOperation, OperationKind.FieldReference, OperationKind.MethodReference, OperationKind.PropertyReference, OperationKind.EventReference);
symbolStartContext.RegisterOperationAction(AnalyzeDeconstructionAssignment, OperationKind.DeconstructionAssignment);
symbolStartContext.RegisterOperationAction(AnalyzeFieldInitializer, OperationKind.FieldInitializer);
symbolStartContext.RegisterOperationAction(AnalyzeInvocationOperation, OperationKind.Invocation);
symbolStartContext.RegisterOperationAction(AnalyzeLoopOperation, OperationKind.Loop);
symbolStartContext.RegisterOperationAction(AnalyzeMemberReferenceOperation, OperationKind.FieldReference, OperationKind.MethodReference, OperationKind.PropertyReference, OperationKind.EventReference);
symbolStartContext.RegisterOperationAction(AnalyzeNameOfOperation, OperationKind.NameOf);
symbolStartContext.RegisterOperationAction(AnalyzeObjectCreationOperation, OperationKind.ObjectCreation);
symbolStartContext.RegisterOperationAction(AnalyzeLoopOperation, OperationKind.Loop);

// We bail out reporting diagnostics for named types if it contains following kind of operations:
// 1. Invalid operations, i.e. erroneous code:
// We do so to ensure that we don't report false positives during editing scenarios in the IDE, where the user
// is still editing code and fixing unresolved references to symbols, such as overload resolution errors.
// 1. Invalid operations, i.e. erroneous code: We do so to ensure that we don't report false positives
// during editing scenarios in the IDE, where the user is still editing code and fixing unresolved
// references to symbols, such as overload resolution errors.
// 2. Dynamic operations, where we do not know the exact member being referenced at compile time.
// 3. Operations with OperationKind.None.

var hasUnsupportedOperation = false;
symbolStartContext.RegisterOperationAction(
_ => hasUnsupportedOperation = true,
OperationKind.Invalid, OperationKind.None, OperationKind.DynamicIndexerAccess, OperationKind.DynamicInvocation, OperationKind.DynamicMemberReference, OperationKind.DynamicObjectCreation);
Expand Down Expand Up @@ -255,14 +257,19 @@ private void AnalyzeSymbolDeclaration(SymbolAnalysisContext symbolContext)
// Note that we might receive a symbol reference (AnalyzeMemberOperation) callback before
// this symbol declaration callback, so even though we cannot receive duplicate callbacks for a symbol,
// an entry might already be present of the declared symbol here.
if (!_symbolValueUsageStateMap.ContainsKey(symbol))
{
_symbolValueUsageStateMap.Add(symbol, ValueUsageInfo.None);
}
_symbolValueUsageStateMap.TryAdd(symbol, ValueUsageInfo.None);
}
}
}

private void AnalyzeDeconstructionAssignment(OperationAnalysisContext operationContext)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is likely another similar scenario, which is foreach-deconstruction: foreach (var (x, y) in ...). There's another semantic model API for that one (DeconstructionInfo GetDeconstructionInfo(ForEachVariableStatementSyntax node)).

{
var operation = operationContext.Operation;
var methods = _analyzer.SemanticFacts.GetDeconstructionAssignmentMethods(operation.SemanticModel!, operation.Syntax);
foreach (var method in methods)
OnSymbolUsage(method, ValueUsageInfo.Read);
}

private void AnalyzeFieldInitializer(OperationAnalysisContext operationContext)
{
// Check if the initialized fields are being initialized a non-constant value.
Expand Down
Loading