Browse Source

#2392: Avoid some redundant casts with the `?:` operator.

pull/2408/head
Daniel Grunwald 4 years ago
parent
commit
8d70d63dba
  1. 5
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
  2. 12
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 2
      ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

5
ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs

@ -556,5 +556,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -556,5 +556,10 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
return intptr;
}
public unsafe void ConditionalPointer(bool a, int* ptr)
{
UsePointer(a ? ptr : null);
}
}
}

12
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -3542,6 +3542,10 @@ namespace ICSharpCode.Decompiler.CSharp @@ -3542,6 +3542,10 @@ namespace ICSharpCode.Decompiler.CSharp
rr = castRR;
}
}
else if (typeHint.Kind.IsAnyPointer() && (object.Equals(rr.ConstantValue, 0) || object.Equals(rr.ConstantValue, 0u)))
{
rr = new ConstantResolveResult(typeHint, null);
}
return rr;
}
@ -3634,7 +3638,11 @@ namespace ICSharpCode.Decompiler.CSharp @@ -3634,7 +3638,11 @@ namespace ICSharpCode.Decompiler.CSharp
if (!success || targetType.GetStackType() != inst.ResultType)
{
// Figure out the target type based on inst.ResultType.
if (inst.ResultType == StackType.Ref)
if (context.TypeHint.Kind != TypeKind.Unknown && context.TypeHint.GetStackType() == inst.ResultType)
{
targetType = context.TypeHint;
}
else if (inst.ResultType == StackType.Ref)
{
// targetType should be a ref-type
if (trueBranch.Type.Kind == TypeKind.ByReference)
@ -3653,7 +3661,7 @@ namespace ICSharpCode.Decompiler.CSharp @@ -3653,7 +3661,7 @@ namespace ICSharpCode.Decompiler.CSharp
}
else
{
targetType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
targetType = FindType(inst.ResultType, context.TypeHint.GetSign());
}
}
}

2
ICSharpCode.Decompiler/CSharp/Syntax/TypeSystemAstBuilder.cs

@ -985,7 +985,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax @@ -985,7 +985,7 @@ namespace ICSharpCode.Decompiler.CSharp.Syntax
throw new ArgumentNullException(nameof(type));
if (constantValue == null)
{
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT))
if (type.IsReferenceType == true || type.IsKnownType(KnownTypeCode.NullableOfT) || type.Kind.IsAnyPointer())
{
var expr = new NullReferenceExpression();
if (AddResolveResultAnnotations)

Loading…
Cancel
Save