Skip to content

Commit

Permalink
Use built-in SDK analyzers
Browse files Browse the repository at this point in the history
Use .NET analyzers built into the .NET SDK.
Fix or suppress analyzer warnings.
  • Loading branch information
martincostello committed Dec 4, 2020
1 parent 0991cfc commit 98c7e4c
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
</ItemGroup>
<PropertyGroup>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>
<AssemblyOriginatorKeyFile>$(MSBuildThisFileDirectory)justeat-oss.snk</AssemblyOriginatorKeyFile>
<Authors>JUSTEAT_OSS</Authors>
<CodeAnalysisRuleSet>$(MSBuildThisFileDirectory)JustEat.StatsD.ruleset</CodeAnalysisRuleSet>
<Company>Just Eat</Company>
<Copyright>Copyright (c) Just Eat 2015-$([System.DateTime]::Now.ToString(yyyy))</Copyright>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<GenerateDocumentationFile>false</GenerateDocumentationFile>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<LangVersion>latest</LangVersion>
Expand Down
2 changes: 0 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
<PackageVersion Include="BenchmarkDotNet" Version="0.12.1" />
<PackageVersion Include="coverlet.msbuild" Version="2.9.0" />
<PackageVersion Include="MartinCostello.Logging.XUnit" Version="0.1.0" />
<PackageVersion Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" Version="3.3.2" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="2.0.0" />
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" />
Expand All @@ -28,7 +27,6 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="coverlet.msbuild" PrivateAssets="All" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
<PackageReference Include="ReportGenerator" PrivateAssets="All" />
</ItemGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/JustEat.StatsD/Buffered/BufferBasedStatsDPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ internal sealed class BufferBasedStatsDPublisher : IStatsDPublisher
private static byte[]? _buffer;
private static byte[] Buffer => _buffer ?? (_buffer = new byte[SafeUdpPacketSize]);

#pragma warning disable CA5394
[ThreadStatic]
private static Random? _random;
private static Random Random => _random ?? (_random = new Random());
#pragma warning disable CA5394

private readonly StatsDUtf8Formatter _formatter;
private readonly IStatsDTransport _transport;
Expand Down Expand Up @@ -75,11 +77,13 @@ private void SendMessage(double sampleRate, in StatsDMessage msg)
}
else
{
throw new Exception("Failed to format buffer to UTF-8.");
throw new InvalidOperationException("Failed to format buffer to UTF-8.");
}
}
}
#pragma warning disable CA2201
catch (Exception ex)
#pragma warning restore CA2201
{
var handled = _onError?.Invoke(ex) ?? true;
if (!handled)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private static IPAddress GetIpAddressOfHost(string hostName)

if (endpoints == null || endpoints.Length == 0)
{
throw new Exception($"Failed to resolve any IP addresses for StatsD host '${hostName}' using DNS.");
throw new InvalidOperationException($"Failed to resolve any IP addresses for StatsD host '${hostName}' using DNS.");
}

IPAddress? result = null;
Expand Down
4 changes: 4 additions & 0 deletions src/JustEat.StatsD/IStatsDPublisherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public static void Increment(this IStatsDPublisher publisher, long value, double
/// <param name="buckets">The bucket(s) to increment the counter(s) for.</param>
public static void Increment(this IStatsDPublisher publisher, long value, double sampleRate, params string[] buckets)
{
#pragma warning disable CA1508
if (buckets == null || buckets.Length == 0)
#pragma warning restore CA1508
{
return;
}
Expand Down Expand Up @@ -137,7 +139,9 @@ public static void Decrement(this IStatsDPublisher publisher, long value, double
/// <param name="buckets">The bucket(s) to decrement the counter(s) for.</param>
public static void Decrement(this IStatsDPublisher publisher, long value, double sampleRate, params string[] buckets)
{
#pragma warning disable CA1508
if (buckets == null || buckets.Length == 0)
#pragma warning restore CA1508
{
return;
}
Expand Down
2 changes: 2 additions & 0 deletions src/JustEat.StatsD/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

[assembly: CLSCompliant(false)]
[assembly: ComVisible(false)]
[assembly: Guid("8f4ff09e-4130-4872-a50f-b290e9ccb04b")]

Expand Down
2 changes: 1 addition & 1 deletion tests/JustEat.StatsD.Tests/JustEat.StatsD.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<NoWarn>$(NoWarn);CA1707;CA2007</NoWarn>
<NoWarn>$(NoWarn);CA1014;CA1002;CA1707;CA1711;CA2007</NoWarn>
<RootNamespace>JustEat.StatsD</RootNamespace>
<TargetFrameworks>net5.0</TargetFrameworks>
</PropertyGroup>
Expand Down

0 comments on commit 98c7e4c

Please sign in to comment.