Skip to content

Commit d2bb973

Browse files
authored
fix: dont report DAP037 on array types (#125)
* dont throw on array return * fix broken dbstring.dapperspecialtype * rework
1 parent a9328b1 commit d2bb973

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,9 @@ private void ValidateDapperMethod(in OperationAnalysisContext ctx, IOperation sq
191191
}
192192

193193
// check the types
194+
// resultType can be an array, so let's unwrap it to determine actual type
194195
var resultType = invoke.GetResultType(flags);
195-
if (resultType is not null && IdentifyDbType(resultType, out _) is null) // don't warn if handled as an inbuilt
196+
if (resultType is not null && IdentifyDbType(resultType, out _) is null && !IsSpecialCaseAllowedResultType(resultType)) // don't warn if handled as an inbuilt
196197
{
197198
var resultMap = MemberMap.CreateForResults(resultType, location);
198199
if (resultMap is not null)

src/Dapper.AOT.Analyzers/Internal/Inspection.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,21 @@ public static ITypeSymbol MakeNonNullable(ITypeSymbol type)
10581058
? type : type.WithNullableAnnotation(NullableAnnotation.None);
10591059
}
10601060

1061+
/// <summary>
1062+
/// There are special types we are allowing user to query,
1063+
/// which are not handled in the general checks
1064+
/// </summary>
1065+
public static bool IsSpecialCaseAllowedResultType(ITypeSymbol? type)
1066+
{
1067+
// byte[] or sbyte[] are basically blobs
1068+
if (type is IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Byte or SpecialType.System_SByte })
1069+
{
1070+
return true;
1071+
}
1072+
1073+
return false;
1074+
}
1075+
10611076
public static DbType? IdentifyDbType(ITypeSymbol? type, out string? readerMethod)
10621077
{
10631078
if (type is null)
@@ -1207,7 +1222,7 @@ public static bool IsDapperMethod(this IInvocationOperation operation, out Opera
12071222

12081223
public static bool TryGetConstantValue<T>(IOperation op, out T? value)
12091224
=> TryGetConstantValueWithSyntax(op, out value, out _, out _);
1210-
1225+
12111226
public static ITypeSymbol? GetResultType(this IInvocationOperation invocation, OperationFlags flags)
12121227
{
12131228
if (flags.HasAny(OperationFlags.TypedResult))

test/Dapper.AOT.Test/Verifiers/DAP037.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class DAP037 : Verifier<DapperAnalyzer>
1010
[Fact]
1111
public Task UserTypeNoSettableMembersFound() => CSVerifyAsync(""""
1212
using Dapper;
13+
using System.Collections.Generic;
1314
using System.Data.Common;
1415
using System.Runtime.Serialization;
1516
@@ -33,6 +34,9 @@ from SomeTable
3334
_ = conn.{|#1:Query<ReadOnlyField>|}(sql, args);
3435
_ = conn.Query<HazImplicitConstructor>(sql, args);
3536
_ = conn.Query<HazExplicitConstructor>(sql, args);
37+
_ = conn.Query<sbyte[]>(sql, args);
38+
_ = conn.Query<byte[]>(sql, args);
39+
_ = conn.{|#2:Query<int[]>|}(sql, args);
3640
}
3741
}
3842
@@ -76,6 +80,7 @@ static file class IsExternalInit {}
7680
"""", DefaultConfig, [
7781
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(0).WithArguments("NoSettable"),
7882
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(1).WithArguments("ReadOnlyField"),
83+
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(2).WithArguments(""),
7984
]);
8085

8186
}

0 commit comments

Comments
 (0)