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

ReadOnlyParameterAnalyzer: fix handling of expression bodies #943

Merged
merged 1 commit into from
Dec 6, 2023
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 @@ -3,6 +3,7 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;

Expand Down Expand Up @@ -90,7 +91,11 @@ IBlockOperation operation
return;
}

DataFlowAnalysis dataflow = operation.SemanticModel.AnalyzeDataFlow( operation.Syntax );
SyntaxNode nodeToAnalyze = operation.Syntax switch {
ArrowExpressionClauseSyntax arrow => arrow.Expression,
_ => operation.Syntax
};
DataFlowAnalysis dataflow = operation.SemanticModel.AnalyzeDataFlow( nodeToAnalyze );

foreach( IParameterSymbol parameter in readOnlyParameters ) {
Location parameterLocation = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ void OnlyRead( [ReadOnly] int foo ) {
int bar = foo;
}

int OnlyReadExpressionBody( [ReadOnly] int foo ) => foo;

void PassedByValue( [ReadOnly] int foo ) {
WrittenToInBody( foo );
}
Expand Down Expand Up @@ -42,6 +44,9 @@ void PassedToRef( /* ReadOnlyParameterIsnt(is assigned to and/or passed by refer
RefParameter( ref foo );
}

void PassedToRefExpressionBody( /* ReadOnlyParameterIsnt(is assigned to and/or passed by reference) */ [ReadOnly] int foo /**/ )
=> RefParameter( ref foo );

void RefParameter( /* ReadOnlyParameterIsnt(is an in/ref/out parameter) */ [ReadOnly] ref int foo /**/ ) { }
void InParameter( /* ReadOnlyParameterIsnt(is an in/ref/out parameter) */ [ReadOnly] in int foo /**/ ) { }

Expand Down