Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit350a8fc

Browse files
Fix RemoveUnusedMembers handling of default parameter reads (#80810)
2 parentse5cfbbc +ec96df6 commit350a8fc

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

‎src/Analyzers/CSharp/Tests/RemoveUnusedMembers/RemoveUnusedMembersTests.cs‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,6 +822,147 @@ public void M()
822822
awaitVerifyCS.VerifyCodeFixAsync(code,code);
823823
}
824824

825+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
826+
publicasyncTaskFieldIsRead_LambdaDefaultParameter()
827+
{
828+
varcode="""
829+
class MyClass
830+
{
831+
private const int _goo = 42;
832+
public void M()
833+
{
834+
var lam = (int x = _goo) => x;
835+
lam();
836+
}
837+
}
838+
""";
839+
840+
awaitnewVerifyCS.Test
841+
{
842+
TestCode=code,
843+
FixedCode=code,
844+
LanguageVersion=LanguageVersion.CSharp12,
845+
}.RunAsync();
846+
}
847+
848+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
849+
publicasyncTaskFieldIsRead_LocalFunctionDefaultParameter()
850+
{
851+
varcode="""
852+
class MyClass
853+
{
854+
private const int _goo = 42;
855+
public void M()
856+
{
857+
int LocalFunc(int x = _goo) => x;
858+
LocalFunc();
859+
}
860+
}
861+
""";
862+
863+
awaitnewVerifyCS.Test
864+
{
865+
TestCode=code,
866+
FixedCode=code,
867+
LanguageVersion=LanguageVersion.CSharp12,
868+
}.RunAsync();
869+
}
870+
871+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
872+
publicasyncTaskFieldIsRead_LambdaDefaultParameter_NestedReferences()
873+
{
874+
varcode="""
875+
class MyClass
876+
{
877+
private const int _goo = 42;
878+
private const int _bar = 10;
879+
public void M()
880+
{
881+
var lam = (int x = _goo + _bar * 2) => x;
882+
lam();
883+
}
884+
}
885+
""";
886+
887+
awaitnewVerifyCS.Test
888+
{
889+
TestCode=code,
890+
FixedCode=code,
891+
LanguageVersion=LanguageVersion.CSharp12,
892+
}.RunAsync();
893+
}
894+
895+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
896+
publicasyncTaskFieldIsRead_LambdaDefaultParameter_MultipleParameters()
897+
{
898+
varcode="""
899+
class MyClass
900+
{
901+
private const int _goo = 42;
902+
private const int _bar = 10;
903+
public void M()
904+
{
905+
var lam = (int x = _goo, int y = _bar) => x + y;
906+
lam();
907+
}
908+
}
909+
""";
910+
911+
awaitnewVerifyCS.Test
912+
{
913+
TestCode=code,
914+
FixedCode=code,
915+
LanguageVersion=LanguageVersion.CSharp12,
916+
}.RunAsync();
917+
}
918+
919+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
920+
publicasyncTaskFieldIsRead_LambdaDefaultParameter_BinaryExpression()
921+
{
922+
varcode="""
923+
class MyClass
924+
{
925+
private const int _goo = 42;
926+
private const int _bar = 10;
927+
public void M()
928+
{
929+
var lam = (int x = _goo + _bar) => x;
930+
lam();
931+
}
932+
}
933+
""";
934+
935+
awaitnewVerifyCS.Test
936+
{
937+
TestCode=code,
938+
FixedCode=code,
939+
LanguageVersion=LanguageVersion.CSharp12,
940+
}.RunAsync();
941+
}
942+
943+
[Fact,WorkItem("https://github.com/dotnet/roslyn/issues/63892")]
944+
publicasyncTaskFieldIsRead_LambdaDefaultParameter_QualifiedAccess()
945+
{
946+
varcode="""
947+
class MyClass
948+
{
949+
private const int _goo = 42;
950+
public void M()
951+
{
952+
var lam = (int x = MyClass._goo) => x;
953+
lam();
954+
}
955+
}
956+
""";
957+
958+
awaitnewVerifyCS.Test
959+
{
960+
TestCode=code,
961+
FixedCode=code,
962+
LanguageVersion=LanguageVersion.CSharp12,
963+
}.RunAsync();
964+
}
965+
825966
[Fact]
826967
publicasyncTaskFieldIsRead_Delegate()
827968
{

‎src/Analyzers/Core/Analyzers/RemoveUnusedMembers/AbstractRemoveUnusedMembersDiagnosticAnalyzer.cs‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ private void RegisterActions(CompilationStartAnalysisContext compilationStartCon
237237
symbolStartContext.RegisterOperationAction(AnalyzeInvocationOperation,OperationKind.Invocation);
238238
symbolStartContext.RegisterOperationAction(AnalyzeLoopOperation,OperationKind.Loop);
239239
symbolStartContext.RegisterOperationAction(AnalyzeMemberReferenceOperation,OperationKind.FieldReference,OperationKind.MethodReference,OperationKind.PropertyReference,OperationKind.EventReference);
240+
symbolStartContext.RegisterOperationAction(AnalyzeParameterInitializerOperation,OperationKind.ParameterInitializer);
241+
symbolStartContext.RegisterOperationAction(AnalyzeFunctionParameterDefaults,OperationKind.AnonymousFunction,OperationKind.LocalFunction);
240242
symbolStartContext.RegisterOperationAction(AnalyzeNameOfOperation,OperationKind.NameOf);
241243
symbolStartContext.RegisterOperationAction(AnalyzeObjectCreationOperation,OperationKind.ObjectCreation);
242244

@@ -441,6 +443,87 @@ memberReference.Parent is IIncrementOrDecrementOperation ||
441443
}
442444
}
443445

446+
privatevoidAnalyzeParameterInitializerOperation(OperationAnalysisContextoperationContext)
447+
{
448+
varparameterInitializer=(IParameterInitializerOperation)operationContext.Operation;
449+
varvalue=parameterInitializer.Value;
450+
451+
if(valueisnull||value.Syntaxisnull)
452+
return;
453+
454+
varsemanticModel=parameterInitializer.SemanticModel;
455+
456+
if(semanticModelisnull)
457+
return;
458+
459+
AnalyzeDefaultValueSyntax(semanticModel,value.Syntax,operationContext.CancellationToken);
460+
}
461+
462+
privatevoidAnalyzeFunctionParameterDefaults(OperationAnalysisContextoperationContext)
463+
{
464+
varsemanticModel=operationContext.Operation.SemanticModel;
465+
466+
if(semanticModelisnull)
467+
return;
468+
469+
varparameters=operationContext.Operationswitch
470+
{
471+
IAnonymousFunctionOperationanonymousFunction=>anonymousFunction.Symbol.Parameters,
472+
ILocalFunctionOperationlocalFunction=>localFunction.Symbol.Parameters,
473+
_=>default,
474+
};
475+
476+
if(parameters.IsDefaultOrEmpty)
477+
return;
478+
479+
varsyntaxFacts=_analyzer.SemanticFacts.SyntaxFacts;
480+
varcancellationToken=operationContext.CancellationToken;
481+
482+
foreach(varparameterinparameters)
483+
{
484+
if(!parameter.HasExplicitDefaultValue)
485+
continue;
486+
487+
foreach(varreferenceinparameter.DeclaringSyntaxReferences)
488+
{
489+
varparameterSyntax=reference.GetSyntax(cancellationToken);
490+
varequalsValueSyntax=syntaxFacts.GetDefaultOfParameter(parameterSyntax);
491+
492+
if(equalsValueSyntaxisnull)
493+
continue;
494+
495+
varvalueSyntax=syntaxFacts.GetValueOfEqualsValueClause(equalsValueSyntax);
496+
497+
if(valueSyntaxisnull)
498+
continue;
499+
500+
AnalyzeDefaultValueSyntax(semanticModel,valueSyntax,cancellationToken);
501+
}
502+
}
503+
}
504+
505+
privatevoidAnalyzeDefaultValueSyntax(
506+
SemanticModelsemanticModel,
507+
SyntaxNodevalueSyntax,
508+
CancellationTokencancellationToken)
509+
{
510+
varsyntaxFacts=_analyzer.SemanticFacts.SyntaxFacts;
511+
512+
foreach(varnodeinvalueSyntax.DescendantNodesAndSelf())
513+
{
514+
if(!syntaxFacts.IsSimpleName(node))
515+
continue;
516+
517+
varsymbolInfo=semanticModel.GetSymbolInfo(node,cancellationToken);
518+
519+
foreach(varsymbolinsymbolInfo.GetAllSymbols())
520+
{
521+
if(IsCandidateSymbol(symbol))
522+
OnSymbolUsage(symbol,ValueUsageInfo.Read);
523+
}
524+
}
525+
}
526+
444527
privatevoidAnalyzeLoopOperation(OperationAnalysisContextoperationContext)
445528
{
446529
varoperation=operationContext.Operation;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp