Browse Source

Merge pull request #3969 from sailro/fix-null-conditional-query-syntax

Keep null-conditional LINQ chains in method syntax
pull/3971/head
Siegfried Pammer 1 month ago committed by GitHub
parent
commit
a57a0a593f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 29
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs
  2. 11
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

29
ICSharpCode.Decompiler.Tests/TestCases/Pretty/QueryExpressions.cs

@ -44,6 +44,20 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -44,6 +44,20 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
public class QueryExpressions
{
public class MaybeHolder
{
public Maybe<int> Value;
#if CS60
public Maybe<int> this[int index] => default(Maybe<int>);
#endif
public Func<Maybe<int>> Factory()
{
return () => default(Maybe<int>);
}
}
public class HbmParam
{
public string Name { get; set; }
@ -217,6 +231,21 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -217,6 +231,21 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
return arglist?.OrderByDescending((string f) => f.Length).ThenBy((string f) => f.ToLower()).ToList();
}
public Maybe<string>? NullConditionalValueTypeQuery(MaybeHolder holder)
{
return holder?.Value.Where((int value) => value > 0).Select((int value) => value.ToString());
}
public Maybe<string>? NullConditionalNestedInvocationQuery(MaybeHolder holder)
{
return holder?.Factory()().Where((int value) => value > 0).Select((int value) => value.ToString());
}
public Maybe<string>? NullConditionalIndexerQuery(MaybeHolder holder)
{
return holder?[0].Where((int value) => value > 0).Select((int value) => value.ToString());
}
#endif
public static IEnumerable<char> Issue1310a(bool test)

11
ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

@ -377,10 +377,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms @@ -377,10 +377,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
}
}
bool IsNullConditional(Expression target)
{
return target is UnaryOperatorExpression uoe && uoe.Operator == UnaryOperatorType.NullConditional;
}
bool IsNullConditional(Expression target) => target switch {
UnaryOperatorExpression { Operator: UnaryOperatorType.NullConditional } => true,
MemberReferenceExpression member => IsNullConditional(member.Target),
InvocationExpression invocation => IsNullConditional(invocation.Target),
IndexerExpression { Target: { } indexerTarget } => IsNullConditional(indexerTarget),
_ => false
};
/// <summary>
/// This fixes #437: Decompilation of query expression loses material parentheses

Loading…
Cancel
Save