Skip to content

Commit

Permalink
#2392: Avoid some redundant casts with the ?: operator.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed May 5, 2021
1 parent f7460a0 commit 8d70d63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/UnsafeCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,5 +556,10 @@ private unsafe static int Issue2305(StructWithFixedSizeMembers value, StringComp
{
return intptr;
}

public unsafe void ConditionalPointer(bool a, int* ptr)
{
UsePointer(a ? ptr : null);
}
}
}
12 changes: 10 additions & 2 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,10 @@ private ResolveResult AdjustConstantToType(ResolveResult rr, IType typeHint)
rr = castRR;
}
}
else if (typeHint.Kind.IsAnyPointer() && (object.Equals(rr.ConstantValue, 0) || object.Equals(rr.ConstantValue, 0u)))
{
rr = new ConstantResolveResult(typeHint, null);
}
return rr;
}

Expand Down Expand Up @@ -3634,7 +3638,11 @@ protected internal override TranslatedExpression VisitIfInstruction(IfInstructio
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)
Expand All @@ -3653,7 +3661,7 @@ protected internal override TranslatedExpression VisitIfInstruction(IfInstructio
}
else
{
targetType = compilation.FindType(inst.ResultType.ToKnownTypeCode());
targetType = FindType(inst.ResultType, context.TypeHint.GetSign());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ public Expression ConvertConstantValue(IType expectedType, IType type, object co
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)
Expand Down

0 comments on commit 8d70d63

Please sign in to comment.