Skip to content

Commit 3d28556

Browse files
committed
Fixed or suppressed warnings.
1 parent 26658ba commit 3d28556

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

DynamicCode.Test/CompilerUtilsTests.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
// Ignore Spelling: Utils
1+
// Ignore Spelling: Utils Clr csharp
22

33
using Microsoft.CodeAnalysis.CSharp;
44
using DynamicCode.Compiler;
55

66
namespace DynamicCode.Test;
77

8+
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
9+
#pragma warning disable CS8603 // Possible null reference return.
10+
#pragma warning disable CS8602 // Dereference of a possibly null reference.
11+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
812
public class CompilerUtilsTests
913
{
1014
[Fact(DisplayName = "ExtractSingleClassName returns class name for single class")]
@@ -103,7 +107,12 @@ private static class CompilerUtilsTestAccessor
103107
public static string GetClrTypeName(string csharpType)
104108
{
105109
var method = typeof(CompilerUtils).GetMethod("GetClrTypeName", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
110+
106111
return (string)method.Invoke(null, new object[] { csharpType });
107112
}
108113
}
114+
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
115+
#pragma warning restore CS8602 // Dereference of a possibly null reference.
116+
#pragma warning restore CS8603 // Possible null reference return.
117+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
109118
}

DynamicCode/Builder/DelegateTypeBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// </summary>
66
public class DelegateTypeBuilder
77
{
8-
private readonly List<Type> _inputs = new();
8+
private readonly List<Type> _inputs = [];
99
private Type? _output;
1010

1111
private DelegateTypeBuilder() { }

DynamicCode/Compiler/CompilerUtils.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ public static string ExtractSingleClassName(SyntaxTree syntaxTree)
3737
/// <exception cref="MissingMethodException">Thrown if no matching method is found.</exception>
3838
public static string ExtractMethodNameMatchingDelegate(Type delegateType, SyntaxTree syntaxTree)
3939
{
40-
var delegateInvoke = delegateType.GetMethod("Invoke");
41-
var delegateParams = delegateInvoke.GetParameters();
40+
var delegateInvoke = delegateType.GetMethod(Constants.InvokeMethodName);
41+
var delegateParams = delegateInvoke!.GetParameters();
4242
var delegateReturn = delegateInvoke.ReturnType;
4343
var root = syntaxTree.GetRoot();
4444
var methodDecls = root.DescendantNodes().OfType<MethodDeclarationSyntax>();
@@ -56,6 +56,11 @@ public static string ExtractMethodNameMatchingDelegate(Type delegateType, Syntax
5656
for (int i = 0; i < parameters.Count; i++)
5757
{
5858
var paramType = parameters[i].Type?.ToString();
59+
if (paramType == null)
60+
{
61+
match = false;
62+
break;
63+
}
5964
var clrParamType = GetClrTypeName(paramType);
6065
var delegateParamType = delegateParams[i].ParameterType;
6166
if (clrParamType != delegateParamType.Name && clrParamType != delegateParamType.FullName)
@@ -101,7 +106,7 @@ private static string GetClrTypeName(string csharpType)
101106
return csharpType;
102107

103108
// Handle nullable types (e.g., int?)
104-
if (csharpType.EndsWith("?"))
109+
if (csharpType.EndsWith('?'))
105110
{
106111
var baseType = csharpType.TrimEnd('?');
107112
var clrBase = GetClrTypeName(baseType);

DynamicCode/Compiler/Constants.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
internal class Constants
44
{
55
internal const string AssemblyName = "DynamicAssembly";
6+
internal const string InvokeMethodName = "Invoke";
67
}

0 commit comments

Comments
 (0)