Skip to content

Commit

Permalink
fussy mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mgravell committed Sep 21, 2023
1 parent 0bd028d commit 5a46c94
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 27 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

# RS2008: Enable analyzer release tracking
dotnet_diagnostic.RS2008.severity = none

# IDE0290: Use primary constructor
dotnet_diagnostic.IDE0290.severity = none
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<AnalysisMode>latest-Recommended</AnalysisMode>
<LangVersion>preview</LangVersion>
<Features>($Features);strict</Features>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<ItemGroup>
<None Include="$(MSBuildThisFileDirectory)Dapper.png" Visible="false">
Expand Down
6 changes: 3 additions & 3 deletions src/Dapper.AOT.Analyzers/CodeAnalysis/DapperAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void OnDapperAotHit()
}
}
}
private void OnDapperAotMiss(OperationAnalysisContext ctx, Location location)
private void OnDapperAotMiss(Location location)
{
if (Thread.VolatileRead(ref _dapperHits) == 0 // fast short-circuit if we know we're all good
&& IsDapperAotAvailable) // don't warn if not available!
Expand Down Expand Up @@ -181,7 +181,7 @@ private void ValidateDapperMethod(in OperationAnalysisContext ctx, IOperation sq
else if (!aotAttribExists && !flags.HasAny(OperationFlags.NotAotSupported))
{
// we might have been able to do more, but Dapper.AOT wasn't enabled
OnDapperAotMiss(ctx, location);
OnDapperAotMiss(location);
}

// check the types
Expand Down Expand Up @@ -333,7 +333,7 @@ private void ValidatePropertyUsage(in OperationAnalysisContext ctx, IOperation s
ValidateSql(ctx, sqlSource, flags);
}

private static readonly Regex HasWhitespace = new Regex(@"\s", RegexOptions.Compiled | RegexOptions.Multiline);
private static readonly Regex HasWhitespace = new(@"\s", RegexOptions.Compiled | RegexOptions.Multiline);


private readonly SqlSyntax? DefaultSqlSyntax;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.SqlServer.TransactSql.ScriptDom;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down Expand Up @@ -54,6 +53,7 @@ internal bool PreFilter(SyntaxNode node, CancellationToken cancellationToken)

private SourceState? Parse(GeneratorSyntaxContext ctx, CancellationToken cancellationToken)
=> Parse(new(ctx, cancellationToken));
[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Chosen API")]
internal SourceState? Parse(in ParseState ctx)
{
if (ctx.Node is not InvocationExpressionSyntax ie
Expand Down Expand Up @@ -158,12 +158,6 @@ static string BuildParameterMap(in ParseState ctx, IInvocationOperation op, stri
return null;
}

private static string GetSignature(IMethodSymbol method, bool deconstruct = true)
{
if (deconstruct && method.IsGenericMethod) method = method.ConstructedFrom ?? method;
return method.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat);
}

private bool CheckPrerequisites(in GenerateState ctx, out int enabledCount)
{
enabledCount = 0;
Expand Down Expand Up @@ -658,7 +652,7 @@ private static void WriteRowFactory(in GenerateState context, CodeWriter sb, ITy
bool hasExplicitConstructor = ctorType == Inspection.ConstructorResult.SuccessSingleExplicit
&& constructor is not null;

var members = Inspection.GetMembers(type, dapperAotConstructor: constructor).ToImmutableArray();
var members = Inspection.GetMembers(type, dapperAotConstructor: constructor);
var membersCount = members.Length;

if (membersCount == 0 && !hasExplicitConstructor)
Expand Down Expand Up @@ -1136,7 +1130,7 @@ private enum SpecialCommandFlags
InitialLONGFetchSize = 1 << 1,
}

private ImmutableArray<ITypeSymbol> IdentifyDbCommandTypes(Compilation compilation, out bool needsPrepare)
private static ImmutableArray<ITypeSymbol> IdentifyDbCommandTypes(Compilation compilation, out bool needsPrepare)
{
needsPrepare = false;
var dbCommand = compilation.GetTypeByMetadataName("System.Data.Common.DbCommand");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ internal override bool IsName(SyntaxNode syntax)

internal override string GetDisplayString(ISymbol symbol)
=> symbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat);
// if (deconstruct && method.IsGenericMethod) method = method.ConstructedFrom ?? method;

internal override bool TryGetStringSpan(SyntaxToken token, string text, scoped in TSqlProcessor.Location location, out int skip, out int take)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Dapper.AOT.Analyzers/Internal/SqlTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ public static string[] GetParameters(string? sql)
{
if (string.IsNullOrWhiteSpace(sql))
{
return Array.Empty<string>();
return [];
}

if (!ParameterRegex.IsMatch(sql))
{
return Array.Empty<string>();
return [];
}
var matches = ParameterRegex.Matches(sql);
if (matches.Count == 0)
{
return Array.Empty<string>();
return [];
}
var arr = new string[matches.Count];
for (int i = 0; i < arr.Length; i++)
Expand Down
21 changes: 9 additions & 12 deletions test/Dapper.AOT.Test/Verifiers/Verifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ public abstract class Verifier
{
public CancellationToken CancellationToken { get; private set; }

protected static DiagnosticResult Diagnostic(DiagnosticDescriptor diagnostic)
=> new DiagnosticResult(diagnostic);
protected static DiagnosticResult Diagnostic(DiagnosticDescriptor diagnostic) => new(diagnostic);

protected static DiagnosticResult InterceptorsNotEnabled = Diagnostic(DapperInterceptorGenerator.Diagnostics.InterceptorsNotEnabled);
protected static readonly DiagnosticResult InterceptorsNotEnabled = Diagnostic(DapperInterceptorGenerator.Diagnostics.InterceptorsNotEnabled);

protected static DiagnosticResult InterceptorsGenerated(int handled, int total,
int interceptors, int commands, int readers)
Expand Down Expand Up @@ -120,17 +119,17 @@ static SourceText CreateEditorConfig(SqlSyntax syntax, SqlParseInputFlags sqlPar

private static readonly SourceText AssumeSqlServer = CreateEditorConfig(SqlSyntax.SqlServer, SqlParseInputFlags.None);

protected static Func<Solution, ProjectId, Solution> InterceptorsEnabled = WithFeatures(
protected static readonly Func<Solution, ProjectId, Solution> InterceptorsEnabled = WithFeatures(
new("InterceptorsPreview", "true"), // rc 1
new("InterceptorsPreviewNamespaces", "Dapper.AOT") // rc2 ?
);

protected static Func<Solution, ProjectId, Solution> CSharpPreview = WithCSharpLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Preview);
protected static readonly Func<Solution, ProjectId, Solution> CSharpPreview = WithCSharpLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion.Preview);

protected static Func<Solution, ProjectId, Solution> VisualBasic14 = WithVisualBasicLanguageVersion(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic14);
protected static readonly Func<Solution, ProjectId, Solution> VisualBasic14 = WithVisualBasicLanguageVersion(Microsoft.CodeAnalysis.VisualBasic.LanguageVersion.VisualBasic14);

protected static Func<Solution, ProjectId, Solution>[] DefaultConfig = new[] {
InterceptorsEnabled, CSharpPreview, VisualBasic14 };
protected static readonly Func<Solution, ProjectId, Solution>[] DefaultConfig = [
InterceptorsEnabled, CSharpPreview, VisualBasic14 ];

protected static Func<Solution, ProjectId, Solution> WithCSharpLanguageVersion(Microsoft.CodeAnalysis.CSharp.LanguageVersion version)
=> WithCSharpParseOptions(options => options.WithLanguageVersion(version));
Expand All @@ -154,8 +153,7 @@ protected static Func<Solution, ProjectId, Solution> WithCSharpParseOptions(Func
if (func is null) return static (solution, _) => solution;
return (solution, projectId) =>
{
var options = solution.GetProject(projectId)?.ParseOptions as CSharpParseOptions;
if (options is null) return solution;
if (solution.GetProject(projectId)?.ParseOptions is not CSharpParseOptions options) return solution;
return solution.WithProjectParseOptions(projectId, func(options));
};
}
Expand All @@ -164,8 +162,7 @@ protected static Func<Solution, ProjectId, Solution> WithVisualBasicParseOptions
if (func is null) return static (solution, _) => solution;
return (solution, projectId) =>
{
var options = solution.GetProject(projectId)?.ParseOptions as VisualBasicParseOptions;
if (options is null) return solution;
if (solution.GetProject(projectId)?.ParseOptions is not VisualBasicParseOptions options) return solution;
return solution.WithProjectParseOptions(projectId, func(options));
};
}
Expand Down

0 comments on commit 5a46c94

Please sign in to comment.