Skip to content

Commit

Permalink
fix: dont report DAP037 on array types (#125)
Browse files Browse the repository at this point in the history
* dont throw on array return

* fix broken dbstring.dapperspecialtype

* rework
  • Loading branch information
DeagleGross authored Aug 30, 2024
1 parent a9328b1 commit d2bb973
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,9 @@ private void ValidateDapperMethod(in OperationAnalysisContext ctx, IOperation sq
}

// check the types
// resultType can be an array, so let's unwrap it to determine actual type
var resultType = invoke.GetResultType(flags);
if (resultType is not null && IdentifyDbType(resultType, out _) is null) // don't warn if handled as an inbuilt
if (resultType is not null && IdentifyDbType(resultType, out _) is null && !IsSpecialCaseAllowedResultType(resultType)) // don't warn if handled as an inbuilt
{
var resultMap = MemberMap.CreateForResults(resultType, location);
if (resultMap is not null)
Expand Down
17 changes: 16 additions & 1 deletion src/Dapper.AOT.Analyzers/Internal/Inspection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,21 @@ public static ITypeSymbol MakeNonNullable(ITypeSymbol type)
? type : type.WithNullableAnnotation(NullableAnnotation.None);
}

/// <summary>
/// There are special types we are allowing user to query,
/// which are not handled in the general checks
/// </summary>
public static bool IsSpecialCaseAllowedResultType(ITypeSymbol? type)
{
// byte[] or sbyte[] are basically blobs
if (type is IArrayTypeSymbol { ElementType.SpecialType: SpecialType.System_Byte or SpecialType.System_SByte })
{
return true;
}

return false;
}

public static DbType? IdentifyDbType(ITypeSymbol? type, out string? readerMethod)
{
if (type is null)
Expand Down Expand Up @@ -1207,7 +1222,7 @@ public static bool IsDapperMethod(this IInvocationOperation operation, out Opera

public static bool TryGetConstantValue<T>(IOperation op, out T? value)
=> TryGetConstantValueWithSyntax(op, out value, out _, out _);

public static ITypeSymbol? GetResultType(this IInvocationOperation invocation, OperationFlags flags)
{
if (flags.HasAny(OperationFlags.TypedResult))
Expand Down
5 changes: 5 additions & 0 deletions test/Dapper.AOT.Test/Verifiers/DAP037.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class DAP037 : Verifier<DapperAnalyzer>
[Fact]
public Task UserTypeNoSettableMembersFound() => CSVerifyAsync(""""
using Dapper;
using System.Collections.Generic;
using System.Data.Common;
using System.Runtime.Serialization;
Expand All @@ -33,6 +34,9 @@ from SomeTable
_ = conn.{|#1:Query<ReadOnlyField>|}(sql, args);
_ = conn.Query<HazImplicitConstructor>(sql, args);
_ = conn.Query<HazExplicitConstructor>(sql, args);
_ = conn.Query<sbyte[]>(sql, args);
_ = conn.Query<byte[]>(sql, args);
_ = conn.{|#2:Query<int[]>|}(sql, args);
}
}
Expand Down Expand Up @@ -76,6 +80,7 @@ static file class IsExternalInit {}
"""", DefaultConfig, [
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(0).WithArguments("NoSettable"),
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(1).WithArguments("ReadOnlyField"),
Diagnostic(Diagnostics.UserTypeNoSettableMembersFound).WithLocation(2).WithArguments(""),
]);

}

0 comments on commit d2bb973

Please sign in to comment.