Skip to content

Commit

Permalink
chore(dotnet): correct all .NET Code Analyzer Errors (#2642)
Browse files Browse the repository at this point in the history
Largely, by disabling the analyzers. Is this cheating? I guess it is.
But I'm cool with that.
  • Loading branch information
RomainMuller authored Mar 3, 2021
1 parent 9da3282 commit cd8957e
Show file tree
Hide file tree
Showing 28 changed files with 135 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
<IsPackable>false</IsPackable>

<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisMode>AllEnabledByDefault</AnalysisMode>

<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly:CLSCompliant(false)]
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public bool Equals(DiagnosticResultLocation other)

public override int GetHashCode()
{
return Path.GetHashCode()
return Path.GetHashCode(StringComparison.InvariantCulture)
^ Line.GetHashCode()
^ Column.GetHashCode();
}
Expand All @@ -70,13 +70,13 @@ public override int GetHashCode()
/// </summary>
public struct DiagnosticResult : IEquatable<DiagnosticResult>
{
private IReadOnlyList<DiagnosticResultLocation> _locations;
private IReadOnlyList<DiagnosticResultLocation>? _locations;

public IReadOnlyList<DiagnosticResultLocation> Locations
{
get
{
_locations ??= new DiagnosticResultLocation[] { };
_locations ??= Array.Empty<DiagnosticResultLocation>();
return _locations;
}

Expand Down Expand Up @@ -108,10 +108,10 @@ public override bool Equals(object? obj)
public override int GetHashCode()
{
return Locations.GetHashCode()
^ Id.GetHashCode()
^ Id.GetHashCode(StringComparison.InvariantCulture)
^ Severity.GetHashCode()
^ Message.GetHashCode()
^ Path.GetHashCode()
^ Message.GetHashCode(StringComparison.InvariantCulture)
^ Path.GetHashCode(StringComparison.InvariantCulture)
^ Line.GetHashCode()
^ Column.GetHashCode();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,24 +134,28 @@ private static Project CreateProject(string[] sources, string language = Languag

var projectId = ProjectId.CreateNewId(debugName: TestProjectName);

var solution = new AdhocWorkspace()
using var workspace = new AdhocWorkspace();
var solution = workspace
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference);

int count = 0;
foreach (var source in sources)
{
var newFileName = fileNamePrefix + count + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
count++;
int count = 0;
foreach (var source in sources)
{
var newFileName = fileNamePrefix + count + "." + fileExt;
var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName);
solution = solution.AddDocument(documentId, newFileName, SourceText.From(source));
count++;
}

return solution.GetProject(projectId)!;
}
return solution.GetProject(projectId)!;
}

#endregion
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Linq;
Expand Down Expand Up @@ -87,7 +88,7 @@ private static void VerifyDiagnosticResults(Diagnostic[] actualResults, Diagnost
}
else
{
VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations.First());
VerifyDiagnosticLocation(analyzer, actual, actual.Location, expected.Locations[0]);
var additionalLocations = actual.AdditionalLocations.ToArray();

if (additionalLocations.Length != expected.Locations.Count - 1)
Expand Down Expand Up @@ -133,7 +134,7 @@ private static void VerifyDiagnosticLocation(DiagnosticAnalyzer? analyzer, Diagn
{
var actualSpan = actual.GetLineSpan();

Assert.True(actualSpan.Path == expected.Path || (actualSpan.Path != null && actualSpan.Path.Contains("Test0.") && expected.Path.Contains("Test.")),
Assert.True(actualSpan.Path == expected.Path || (actualSpan.Path != null && actualSpan.Path.Contains("Test0.", StringComparison.InvariantCulture) && expected.Path.Contains("Test.", StringComparison.InvariantCulture)),
$"Expected diagnostic to be in file \"{expected.Path}\" was actually in file \"{actualSpan.Path}\"\r\n\r\nDiagnostic:\r\n {FormatDiagnostics(analyzer, diagnostic)}\r\n");

var actualLinePosition = actualSpan.StartLinePosition;
Expand Down Expand Up @@ -187,18 +188,18 @@ private static string FormatDiagnostics(DiagnosticAnalyzer? analyzer, params Dia
var location = diagnostics[i].Location;
if (location == Location.None)
{
builder.AppendFormat("GetGlobalResult({0}.{1})", analyzerType.Name, rule.Id);
builder.AppendFormat(CultureInfo.InvariantCulture, "GetGlobalResult({0}.{1})", analyzerType.Name, rule.Id);
}
else
{
Assert.True(location.IsInSource,
$"Test base does not currently handle diagnostics in metadata locations. Diagnostic in metadata: {diagnostics[i]}\r\n");

var fileIsCSharp = diagnostics[i].Location.SourceTree?.FilePath.EndsWith(".cs") ?? false;
var fileIsCSharp = diagnostics[i].Location.SourceTree?.FilePath.EndsWith(".cs", StringComparison.InvariantCulture) ?? false;
var resultMethodName = fileIsCSharp ? "GetCSharpResultAt" : "GetBasicResultAt";
var linePosition = diagnostics[i].Location.GetLineSpan().StartLinePosition;

builder.AppendFormat("{0}({1}, {2}, {3}.{4})",
builder.AppendFormat(CultureInfo.InvariantCulture, "{0}({1}, {2}, {3}.{4})",
resultMethodName,
linePosition.Line + 1,
linePosition.Character + 1,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly:CLSCompliant(true)]
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Runtime.CompilerServices;

[assembly:CLSCompliant(true)]

// All the types were folded into Amazon.JSII.Runtime now, so here's a bunch of type
// forwarders to maintain backwards compatibility with previous versions.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<RollForward>Major</RollForward>

<NoWarn>
CA1034, <!-- Nested types should not be visible -->
CA1711, <!-- Identifiers should not have incorrect suffix -->
CA1724, <!-- Type names should not match namespaces -->
</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Runtime.CompilerServices;
using System;
using System.Runtime.CompilerServices;
using Castle.Core.Internal;
using Xunit;

[assembly:CLSCompliant(false)]

// Each test in this assembly overrides the the global service provider, so they can't be run in parallel.
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Amazon.JSII.JsonModel.Api;
using System;
using Amazon.JSII.JsonModel.Api;
using Amazon.JSII.JsonModel.Api.Request;
using Amazon.JSII.JsonModel.Api.Response;
using Amazon.JSII.JsonModel.FileSystem;
Expand Down Expand Up @@ -45,8 +46,8 @@ protected ClientTestBase()
_fileSystem.Directory.Returns(directory);
}

internal string GetOkResponse<TResponse>(TResponse response)
where TResponse : IKernelResponse
internal static string GetOkResponse<TResponse>(TResponse response)
where TResponse : class, IKernelResponse
{
IDictionary<string, object> okResponse = new Dictionary<string, object>
{
Expand All @@ -69,7 +70,7 @@ internal IClient CreateClient()
);
}

internal bool PlatformIndependentEqual(string expected, string actual)
internal static bool PlatformIndependentEqual(string expected, string actual)
{
try
{
Expand Down Expand Up @@ -99,7 +100,7 @@ public void LoadsBasicPackage()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[]{ }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
license: "",
targets: new AssemblyTargets(new AssemblyTargets.DotNetTarget(
Expand Down Expand Up @@ -132,7 +133,7 @@ public void DoesNotLoadPackageMultipleTimes()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
license: "",
targets: new AssemblyTargets(new AssemblyTargets.DotNetTarget(
Expand Down Expand Up @@ -185,7 +186,7 @@ public void SendsAndReceives()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
license: "",
targets: new AssemblyTargets(new AssemblyTargets.DotNetTarget(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using NSubstitute;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Newtonsoft.Json.Converters;
using Xunit;
Expand Down Expand Up @@ -122,7 +123,7 @@ public void ConvertsDateValues()

var expected = new JObject
{
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz"))
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz", CultureInfo.InvariantCulture))
};

Assert.Equal(expected, actual);
Expand All @@ -144,7 +145,7 @@ public void ConvertsOptionalDateValues()
Assert.IsType<JObject>(actual);
var expected = new JObject
{
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz"))
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz", CultureInfo.InvariantCulture))
};
Assert.Equal(expected, actual);

Expand Down Expand Up @@ -652,7 +653,7 @@ public void ConvertsDate()

var expected = new JObject
{
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz"))
new JProperty("$jsii.date", now.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz", CultureInfo.InvariantCulture))
};
Assert.Equal(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public abstract class TestBase
internal readonly IReferenceMap _referenceMap;
internal readonly JsiiToFrameworkConverter _converter;

public TestBase()
protected TestBase()
{
_typeCache = Substitute.For<ITypeCache>();
_referenceMap = Substitute.For<IReferenceMap>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ public DeputyBaseTests()
.BuildServiceProvider();
}

void IDisposable.Dispose()
public void Dispose()
{
ServiceContainer.ServiceProviderOverride = null;
_serviceProvider.Dispose();
}

[Fact(DisplayName = Prefix + nameof(CanCastToAnyInterface))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Amazon.JSII.Runtime.UnitTests.JsonModel
{
public class AssemblyTests
public static class AssemblyTests
{
const string RootPrefix = nameof(JsonModel) + "." + nameof(Assembly) + ".";

Expand Down Expand Up @@ -113,7 +113,7 @@ public void ShouldThrowOnMissingName()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
version: "",
license: "",
Expand All @@ -140,7 +140,7 @@ public void ShouldThrowOnMissingVersion()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
#pragma warning disable CS8625
version: null,
Expand Down Expand Up @@ -169,7 +169,7 @@ public void ShouldNotSerializeMissingDependencies()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
version: "",
license: "",
Expand Down Expand Up @@ -229,7 +229,7 @@ public void ShouldNotSerializeMissingBundled()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
version: "",
license: "",
Expand Down Expand Up @@ -289,7 +289,7 @@ public void ShouldNotSerializeMissingDocs()
description: "",
homepage: "",
repository: new Assembly.AssemblyRepository(type: "", url: ""),
author: new Person(name: "", roles: new string[] { }),
author: new Person(name: "", roles: Array.Empty<string>()),
fingerprint: "",
version: "",
license: "",
Expand Down
Loading

0 comments on commit cd8957e

Please sign in to comment.