Browse Source

Fix #2267: Use discard assignment with query expressions, if necessary.

pull/2276/head
Siegfried Pammer 5 years ago
parent
commit
6b6a794c3a
  1. 16
      ICSharpCode.Decompiler/CSharp/Transforms/IntroduceQueryExpressions.cs

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

@ -30,10 +30,13 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
/// </summary> /// </summary>
public class IntroduceQueryExpressions : IAstTransform public class IntroduceQueryExpressions : IAstTransform
{ {
TransformContext context;
public void Run(AstNode rootNode, TransformContext context) public void Run(AstNode rootNode, TransformContext context)
{ {
if (!context.Settings.QueryExpressions) if (!context.Settings.QueryExpressions)
return; return;
this.context = context;
DecompileQueries(rootNode); DecompileQueries(rootNode);
// After all queries were decompiled, detect degenerate queries (queries not property terminated with 'select' or 'group') // After all queries were decompiled, detect degenerate queries (queries not property terminated with 'select' or 'group')
// and fix them, either by adding a degenerate select, or by combining them with another query. // and fix them, either by adding a degenerate select, or by combining them with another query.
@ -76,9 +79,14 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
void DecompileQueries(AstNode node) void DecompileQueries(AstNode node)
{ {
QueryExpression query = DecompileQuery(node as InvocationExpression); Expression query = DecompileQuery(node as InvocationExpression);
if (query != null) if (query != null)
{
if (node.Parent is ExpressionStatement && CanUseDiscardAssignment())
query = new AssignmentExpression(new IdentifierExpression("_"), query);
node.ReplaceWith(query); node.ReplaceWith(query);
}
AstNode next; AstNode next;
for (AstNode child = (query ?? node).FirstChild; child != null; child = next) for (AstNode child = (query ?? node).FirstChild; child != null; child = next)
{ {
@ -88,6 +96,12 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
} }
} }
bool CanUseDiscardAssignment()
{
// TODO : check whether there exists a variable named '_' in scope.
return context.Settings.Discards;
}
QueryExpression DecompileQuery(InvocationExpression invocation) QueryExpression DecompileQuery(InvocationExpression invocation)
{ {
if (invocation == null) if (invocation == null)

Loading…
Cancel
Save