Browse Source

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

pull/2408/head
Daniel Grunwald 5 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
{ {
return intptr; 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
rr = castRR; rr = castRR;
} }
} }
else if (typeHint.Kind.IsAnyPointer() && (object.Equals(rr.ConstantValue, 0) || object.Equals(rr.ConstantValue, 0u)))
{
rr = new ConstantResolveResult(typeHint, null);
}
return rr; return rr;
} }
@ -3634,7 +3638,11 @@ namespace ICSharpCode.Decompiler.CSharp
if (!success || targetType.GetStackType() != inst.ResultType) if (!success || targetType.GetStackType() != inst.ResultType)
{ {
// Figure out the target type based on 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 // targetType should be a ref-type
if (trueBranch.Type.Kind == TypeKind.ByReference) if (trueBranch.Type.Kind == TypeKind.ByReference)
@ -3653,7 +3661,7 @@ namespace ICSharpCode.Decompiler.CSharp
} }
else 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
throw new ArgumentNullException(nameof(type)); throw new ArgumentNullException(nameof(type));
if (constantValue == null) 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(); var expr = new NullReferenceExpression();
if (AddResolveResultAnnotations) if (AddResolveResultAnnotations)

Loading…
Cancel
Save