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

Migrate from FxCop to .NET Analyzers #2174

Merged
merged 2 commits into from
Apr 8, 2023
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
24 changes: 24 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,31 @@ dotnet_diagnostic.CA1062.severity = suggestion
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
dotnet_code_quality.exclude_extension_method_this_parameter = true
dotnet_code_quality.null_check_validation_methods = ThrowIfArgumentIsNull
# CA1031: Do not catch general exception types
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
dotnet_diagnostic.CA1031.severity = none
# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none
# CA1304: Specify CultureInfo
dotnet_diagnostic.CA1304.severity = error
# CA1307: Specify StringComparison for clarity
dotnet_diagnostic.CA1307.severity = error
# CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1308.severity = error
# CA1309: Use ordinal StringComparison
dotnet_diagnostic.CA1309.severity = error
# CA1724: Type names should not match namespaces
dotnet_diagnostic.CA1724.severity = none
# CA1819: Properties should not return arrays
dotnet_diagnostic.CA1819.severity = none
# CA1851: Possible multiple enumerations of IEnumerable collection. Related to GH-issue #2000
dotnet_diagnostic.CA1851.severity = suggestion
# CA2007: Do not directly await a Task
dotnet_diagnostic.CA2007.severity = none
# CA2225: Operator overloads have named alternates
dotnet_diagnostic.CA2225.severity = none
# CA3075: Insecure DTD Processing
dotnet_diagnostic.CA3075.severity = none
# CA5369: Use XmlReader for Deserialize
dotnet_diagnostic.CA5369.severity = none

# Banned API Analyzers
Expand Down Expand Up @@ -317,6 +337,10 @@ dotnet_diagnostic.RCS1124.severity = suggestion
# Add exception to documentation comment. Nice suggestion, but we don't want to document exceptions for internal code.
dotnet_diagnostic.RCS1140.severity = suggestion

# Missing documentation
dotnet_diagnostic.RCS1141.severity = suggestion
dotnet_diagnostic.RCS1142.severity = suggestion

# Use conditional access. Suggestion because it doesn't always improve readability
dotnet_diagnostic.RCS1146.severity = suggestion

Expand Down
12 changes: 10 additions & 2 deletions Build/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_indexers = true:warning
csharp_style_expression_bodied_accessors = true:warning

dotnet_diagnostic.ide0044.severity = none
dotnet_diagnostic.ide0051.severity = none
dotnet_diagnostic.IDE0044.severity = none
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.RCS1110.severity = none
dotnet_diagnostic.RCS1169.severity = none
dotnet_diagnostic.S2365.severity = none
dotnet_diagnostic.S3903.severity = none
dotnet_diagnostic.SA1009.severity = none
dotnet_diagnostic.SA1111.severity = none
dotnet_diagnostic.SA1306.severity = none
dotnet_diagnostic.SA1400.severity = none

# ReSharper properties
resharper_place_field_attribute_on_same_line = false
4 changes: 4 additions & 0 deletions Build/Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ class Build : NukeBuild
GitHubActions GitHubActions => GitHubActions.Instance;

string BranchSpec => GitHubActions?.Ref;

string BuildNumber => GitHubActions?.RunNumber.ToString();

string PullRequestBase => GitHubActions?.BaseRef;

[Parameter("Use this parameter if you encounter build problems in any way, " +
Expand Down Expand Up @@ -375,7 +377,9 @@ void ReportTestOutcome(params string[] globFilters)
.ToArray();

Repository Repository => new(GitRepository.LocalDirectory);

Tree TargetBranch => Repository.Branches[PullRequestBase].Tip.Tree;

Tree SourceBranch => Repository.Branches[Repository.Head.FriendlyName].Tip.Tree;

bool RunAllTargets => string.IsNullOrWhiteSpace(PullRequestBase) || Changes.Any(x => x.StartsWith("Build"));
Expand Down
8 changes: 5 additions & 3 deletions Build/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
[TypeConverter(typeof(TypeConverter<Configuration>))]
public class Configuration : Enumeration
{
public static Configuration Debug = new() { Value = nameof(Debug) };
public static Configuration Release = new() { Value = nameof(Release) };
public static Configuration CI = new() { Value = nameof(CI) };
public static Configuration Debug { get; } = new() { Value = nameof(Debug) };

public static Configuration Release { get; } = new() { Value = nameof(Release) };

public static Configuration CI { get; } = new() { Value = nameof(CI) };

public static implicit operator string(Configuration configuration)
{
Expand Down
35 changes: 35 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,39 @@
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>

<!-- To reduce build times, we only enable analyzers for the newest TFM -->
jnyrup marked this conversation as resolved.
Show resolved Hide resolved
<PropertyGroup Condition="'$(TargetFramework)' != 'net6.0'">
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzers>false</RunAnalyzers>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<EnableNETAnalyzers>true</EnableNETAnalyzers>
<AnalysisLevel>latest</AnalysisLevel>
<AnalysisMode>All</AnalysisMode>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CSharpGuidelinesAnalyzer" Version="3.8.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.26">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions FluentAssertions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "_build", "Build\_build.csproj", "{364DD16C-D759-49DC-A04A-64D40205295B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Specs", "Specs", "{963262D0-9FD5-4741-8C0E-E2F34F110EF3}"
ProjectSection(SolutionItems) = preProject
Tests\.editorconfig = Tests\.editorconfig
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AssemblyA", "Tests\AssemblyA\AssemblyA.csproj", "{7144BD9D-2A5F-45B6-AC5B-E35578D03350}"
EndProject
Expand Down
63 changes: 0 additions & 63 deletions Rules.ruleset

This file was deleted.

2 changes: 1 addition & 1 deletion Src/FluentAssertions/CallerIdentifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static bool StartsWithNewKeyword(string candidate)

private static bool IsStringLiteral(string candidate)
{
return candidate.StartsWith("\"", StringComparison.Ordinal);
return candidate.StartsWith('\"');
}

private static bool IsNumeric(string candidate)
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Common/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static string Combine(this string @this, string other, string separator =
return @this;
}

if (other.StartsWith("[", StringComparison.Ordinal))
if (other.StartsWith('['))
{
separator = string.Empty;
}
Expand Down
1 change: 1 addition & 0 deletions Src/FluentAssertions/CustomAssertionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace FluentAssertions;
/// internally, or directly uses the <c>Execute.Assertion</c>.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
#pragma warning disable CA1813 // Avoid unsealed attributes. This type has shipped.
public class CustomAssertionAttribute : Attribute
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace FluentAssertions.Equivalency;

#pragma warning disable CA1033 //An unsealed externally visible type provides an explicit method implementation of a public interface and does not provide an alternative externally visible method that has the same name.

/// <summary>
/// Represents the run-time behavior of a structural equivalency assertion.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Execution/LateBoundTestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public void Throw(string message)

if (exceptionType is null)
{
throw new Exception(
throw new NotSupportedException(
$"Failed to create the assertion exception for the current test framework: \"{ExceptionFullName}, {assembly.FullName}\"");
}

Expand Down
8 changes: 2 additions & 6 deletions Src/FluentAssertions/Execution/NSpecFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ public bool IsAvailable
[DoesNotReturn]
public void Throw(string message)
{
Type exceptionType = assembly.GetType("NSpec.Domain.AssertionException");

if (exceptionType is null)
{
throw new Exception("Failed to create the NSpec assertion type");
}
Type exceptionType = assembly.GetType("NSpec.Domain.AssertionException")
?? throw new NotSupportedException("Failed to create the NSpec assertion type");

throw (Exception)Activator.CreateInstance(exceptionType, message);
}
Expand Down
8 changes: 2 additions & 6 deletions Src/FluentAssertions/Execution/XUnit2TestFramework.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,8 @@ public bool IsAvailable
[DoesNotReturn]
public void Throw(string message)
{
Type exceptionType = assembly.GetType("Xunit.Sdk.XunitException");

if (exceptionType is null)
{
throw new Exception("Failed to create the XUnit assertion type");
}
Type exceptionType = assembly.GetType("Xunit.Sdk.XunitException")
?? throw new NotSupportedException("Failed to create the XUnit assertion type");

throw (Exception)Activator.CreateInstance(exceptionType, message);
}
Expand Down
32 changes: 0 additions & 32 deletions Src/FluentAssertions/FluentAssertions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>1591;1573</NoWarn>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<CodeAnalysisRuleSet>..\..\Rules.ruleset</CodeAnalysisRuleSet>
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
Expand All @@ -30,11 +29,6 @@
<PackageReleaseNotes>See https://fluentassertions.com/releases/</PackageReleaseNotes>
<Copyright>Copyright Dennis Doomen 2010-$([System.DateTime]::Now.ToString(yyyy))</Copyright>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' != 'net6.0'">
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<RunAnalyzersDuringLiveAnalysis>false</RunAnalyzersDuringLiveAnalysis>
<RunAnalyzers>false</RunAnalyzers>
</PropertyGroup>
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
<_Parameter1>FluentAssertions.Specs, PublicKey=00240000048000009400000006020000002400005253413100040000010001002d25ff515c85b13ba08f61d466cff5d80a7f28ba197bbf8796085213e7a3406f970d2a4874932fed35db546e89af2da88c194bf1b7f7ac70de7988c78406f7629c547283061282a825616eb7eb48a9514a7570942936020a9bb37dca9ff60b778309900851575614491c6d25018fadb75828f4c7a17bf2d7dc86e7b6eafc5d8f</_Parameter1>
Expand Down Expand Up @@ -100,30 +94,4 @@
<Reference Include="System.Xml" />
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0'">
<PackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="CSharpGuidelinesAnalyzer" Version="3.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Roslynator.Analyzers" Version="4.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Meziantou.Analyzer" Version="2.0.26">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Src/FluentAssertions/Formatting/ValueFormatterAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace FluentAssertions.Formatting;
/// Marks a static method as a kind of <see cref="IValueFormatter"/> for a particular type.
/// </summary>
[AttributeUsage(AttributeTargets.Method)]
#pragma warning disable CA1813 // Avoid unsealed attributes. This type has shipped.
public class ValueFormatterAttribute : Attribute
{
}
4 changes: 2 additions & 2 deletions Src/FluentAssertions/ObjectAssertionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ private static object CreateCloneUsingBinarySerializer(object subject)
Binder = new SimpleBinder(subject.GetType())
};

#pragma warning disable SYSLIB0011 // BinaryFormatter is obsoleted, GH-issue 1779 tracks the upcoming removal in .NET 8.0
#pragma warning disable SYSLIB0011, CA2300 // BinaryFormatter is obsoleted, GH-issue 1779 tracks the upcoming removal in .NET 8.0
binaryFormatter.Serialize(stream, subject);
stream.Position = 0;
return binaryFormatter.Deserialize(stream);
#pragma warning restore SYSLIB0011
#pragma warning restore SYSLIB0011, CA2300
}

private sealed class SimpleBinder : SerializationBinder
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/ReferenceTypeAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace FluentAssertions.Primitives;

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Contains a number of methods to assert that a reference type object is in the expected state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public SimpleTimeSpanAssertions(TimeSpan? value)
}
}

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Contains a number of methods to assert that a nullable <see cref="TimeSpan"/> is in the expected state.
Expand Down
2 changes: 1 addition & 1 deletion Src/FluentAssertions/Primitives/TimeOnlyAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TimeOnlyAssertions(TimeOnly? value)
}
}

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Contains a number of methods to assert that a <see cref="TimeOnly"/> is in the expected state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace FluentAssertions.Specialized;

#pragma warning disable CS0659 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CS0659, S1206 // Ignore not overriding Object.GetHashCode()
#pragma warning disable CA1065 // Ignore throwing NotSupportedException from Equals
/// <summary>
/// Provides methods for asserting that the execution time of an <see cref="Action"/> satisfies certain conditions.
Expand Down
Loading