Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: dont report DAP037 on array types #125

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(""),
]);

}
Loading