Skip to content

Commit 487259c

Browse files
authored
Add code analysis (#463)
1 parent 4779f6b commit 487259c

File tree

14 files changed

+234
-19
lines changed

14 files changed

+234
-19
lines changed

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<LangVersion>8.0</LangVersion>
1515
<Nullable>enable</Nullable>
16+
<CodeAnalysisRuleset>$(MSBuildThisFileDirectory)Grpc.DotNet.ruleset</CodeAnalysisRuleset>
1617
</PropertyGroup>
1718

1819
<PropertyGroup Condition=" '$(IsGrpcPublishedPackage)' == 'true' ">
@@ -32,6 +33,7 @@
3233
</PropertyGroup>
3334

3435
<ItemGroup Condition="'$(IsGrpcPublishedPackage)' == 'true'">
36+
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="$(MicrosoftCodeAnalysisFxCopAnalyzersPackageVersion)" PrivateAssets="All" />
3537
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="$(MicrosoftSourceLinkGitHubPackageVersion)" PrivateAssets="All" />
3638
</ItemGroup>
3739

Grpc.DotNet.ruleset

Lines changed: 199 additions & 0 deletions
Large diffs are not rendered by default.

build/dependencies.props

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
<ConfigureAwaitCheckerAnalyzerPackageVersion>3.0.0</ConfigureAwaitCheckerAnalyzerPackageVersion>
66
<GoogleProtobufPackageVersion>3.9.1</GoogleProtobufPackageVersion>
77
<GrpcPackageVersion>2.23.0-pre1</GrpcPackageVersion>
8-
<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
9-
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>
108
<MicrosoftAspNetCorePackageVersion>3.0.0-preview8.19405.7</MicrosoftAspNetCorePackageVersion>
9+
<MicrosoftBuildLocatorPackageVersion>1.2.2</MicrosoftBuildLocatorPackageVersion>
10+
<MicrosoftBuildPackageVersion>16.0.461</MicrosoftBuildPackageVersion>
11+
<MicrosoftCodeAnalysisFxCopAnalyzersPackageVersion>2.9.4</MicrosoftCodeAnalysisFxCopAnalyzersPackageVersion>
1112
<MicrosoftExtensionsPackageVersion>2.1.1</MicrosoftExtensionsPackageVersion>
1213
<MicrosoftExtensions30PackageVersion>3.0.0-preview8.19405.4</MicrosoftExtensions30PackageVersion>
1314
<MicrosoftNETTestSdkPackageVersion>16.2.0</MicrosoftNETTestSdkPackageVersion>

src/Grpc.AspNetCore.Server.ClientFactory/Grpc.AspNetCore.Server.ClientFactory.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
88
<TargetFramework>netcoreapp3.0</TargetFramework>
9+
10+
<!-- Disable analysis for ConfigureAwait(false) -->
11+
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
12+
<NoWarn>$(NoWarn);CA2007</NoWarn>
913
</PropertyGroup>
1014

1115
<ItemGroup>

src/Grpc.AspNetCore.Server.Reflection/Grpc.AspNetCore.Server.Reflection.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
88
<TargetFramework>netcoreapp3.0</TargetFramework>
9+
10+
<!-- Disable analysis for ConfigureAwait(false) -->
11+
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
12+
<NoWarn>$(NoWarn);CA2007</NoWarn>
913
</PropertyGroup>
1014

1115
<ItemGroup>

src/Grpc.AspNetCore.Server/Grpc.AspNetCore.Server.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
<IsGrpcPublishedPackage>true</IsGrpcPublishedPackage>
88
<TargetFramework>netcoreapp3.0</TargetFramework>
9+
10+
<!-- Disable analysis for ConfigureAwait(false) -->
11+
<WarningsNotAsErrors>$(WarningsNotAsErrors);CA2007</WarningsNotAsErrors>
12+
<NoWarn>$(NoWarn);CA2007</NoWarn>
913
</PropertyGroup>
1014

1115
<ItemGroup>

src/Grpc.AspNetCore.Server/Internal/X509CertificateHelpers.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Globalization;
2122
using System.Security.Cryptography.X509Certificates;
2223

2324
namespace Grpc.AspNetCore.Server.Internal
@@ -35,7 +36,7 @@ public static string[] GetDnsFromExtensions(X509Certificate2 cert)
3536
string asnString = ext.Format(false);
3637
if (string.IsNullOrWhiteSpace(asnString))
3738
{
38-
return new string[0];
39+
return Array.Empty<string>();
3940
}
4041

4142
// SubjectAlternativeNames might contain something other than a dNSName,
@@ -50,7 +51,7 @@ public static string[] GetDnsFromExtensions(X509Certificate2 cert)
5051
for (var i = 0; i < rawDnsEntries.Length; i++)
5152
{
5253
string[] keyval = rawDnsEntries[i].Split(X509SubjectAlternativeNameConstants.Delimiter);
53-
if (string.Equals(keyval[0], X509SubjectAlternativeNameConstants.Identifier))
54+
if (string.Equals(keyval[0], X509SubjectAlternativeNameConstants.Identifier, StringComparison.Ordinal))
5455
{
5556
dnsEntries.Add(keyval[1]);
5657
}
@@ -59,7 +60,7 @@ public static string[] GetDnsFromExtensions(X509Certificate2 cert)
5960
return dnsEntries.ToArray();
6061
}
6162
}
62-
return new string[0];
63+
return Array.Empty<string>();
6364
}
6465

6566
// We don't have a strongly typed extension to parse Subject Alt Names, so we have to do a workaround
@@ -106,6 +107,7 @@ private static void EnsureInitialized()
106107
if (!s_successfullyInitialized)
107108
{
108109
throw new FormatException(string.Format(
110+
CultureInfo.InvariantCulture,
109111
"There was an error detecting the identifier, delimiter, and separator for X509CertificateClaims on this platform.{0}" +
110112
"Detected values were: Identifier: '{1}'; Delimiter:'{2}'; Separator:'{3}'",
111113
Environment.NewLine,
@@ -138,7 +140,7 @@ static X509SubjectAlternativeNameConstants()
138140
// Linux: x509ExtensionFormattedString is: "DNS:not-real-subject-name, DNS:example.com"
139141
// Parse: <identifier><delimter><value><separator(s)>
140142

141-
int delimiterIndex = x509ExtensionFormattedString.IndexOf(subjectName1) - 1;
143+
int delimiterIndex = x509ExtensionFormattedString.IndexOf(subjectName1, StringComparison.Ordinal) - 1;
142144
s_delimiter = x509ExtensionFormattedString[delimiterIndex];
143145

144146
// Make an assumption that all characters from the the start of string to the delimiter

src/Grpc.Net.Client/Grpc.Net.Client.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<!-- Roslyn analyzer that enforces ConfigureAwait(false) -->
13-
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="$(ConfigureAwaitCheckerAnalyzerPackageVersion)" PrivateAssets="All" />
1412
<PackageReference Include="Grpc.Core.Api" Version="$(GrpcPackageVersion)" />
1513
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(MicrosoftExtensionsPackageVersion)" />
1614
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourcePackageVersion)" />

src/Grpc.Net.Client/Internal/GrpcProtocolHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Globalization;
2122
using System.Linq;
2223
using System.Net.Http;
2324
using System.Net.Http.Headers;
@@ -139,7 +140,7 @@ static long RoundUp(long x, long divisor)
139140

140141
private static string FormatTimeout(long value, char ext)
141142
{
142-
return value.ToString() + ext;
143+
return value.ToString(CultureInfo.InvariantCulture) + ext;
143144
}
144145

145146
private static string EncodeTimeoutSeconds(long sec)

src/Grpc.Net.ClientFactory/Grpc.Net.ClientFactory.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
<!-- PrivateAssets set to None to ensure the build targets/props are propagated to parent project -->
1313
<ProjectReference Include="..\Grpc.Net.Client\Grpc.Net.Client.csproj" PrivateAssets="None" />
1414

15-
<!-- Roslyn analyzer that enforces ConfigureAwait(false) -->
16-
<PackageReference Include="ConfigureAwaitChecker.Analyzer" Version="$(ConfigureAwaitCheckerAnalyzerPackageVersion)" PrivateAssets="All" />
1715
<PackageReference Include="Grpc.Core.Api" Version="$(GrpcPackageVersion)" />
1816
<PackageReference Include="Microsoft.Extensions.Http" Version="$(MicrosoftExtensionsPackageVersion)" />
1917
</ItemGroup>

0 commit comments

Comments
 (0)