Skip to content

Commit

Permalink
Updates to build properly with .NET 9 SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Nov 13, 2024
1 parent ffa519b commit 3c727df
Show file tree
Hide file tree
Showing 76 changed files with 297 additions and 360 deletions.
9 changes: 8 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_prefer_auto_properties = true
dotnet_style_prefer_compound_assignment = true
dotnet_style_prefer_conditional_expression_over_assignment = true
dotnet_style_prefer_conditional_expression_over_return = true
dotnet_style_prefer_conditional_expression_over_return = false
dotnet_style_prefer_foreach_explicit_cast_in_source = when_strongly_typed
dotnet_style_prefer_inferred_anonymous_type_member_names = true
dotnet_style_prefer_inferred_tuple_names = true
Expand Down Expand Up @@ -227,6 +227,7 @@ dotnet_diagnostic.CA1002.severity = none # Do not expose generic lists
dotnet_diagnostic.CA1014.severity = none # Mark assemblies with CLSCompliantAttribute
dotnet_diagnostic.CA1034.severity = none # Do not nest types
dotnet_diagnostic.CA1050.severity = none # Declare types in namespaces
dotnet_diagnostic.CA1200.severity = none # Avoid using cref tags with a prefix
dotnet_diagnostic.CA1303.severity = none # Do not pass literals as localized parameters
dotnet_diagnostic.CA1707.severity = none # Remove the underscores from type name
dotnet_diagnostic.CA1720.severity = none # Identifier contains type name
Expand All @@ -235,5 +236,11 @@ dotnet_diagnostic.CA1859.severity = none # Use concrete types when possible fo
dotnet_diagnostic.CA1861.severity = none # Avoid constant arrays as arguments
dotnet_diagnostic.CA2211.severity = none # Non-constant fields should not be visible
dotnet_diagnostic.CA2241.severity = error # Provide correct arguments to formatting methods/The format argument is not a valid string
dotnet_diagnostic.CS1591.severity = none # Missing XML comment
dotnet_diagnostic.IDE0010.severity = none # Populate switch
dotnet_diagnostic.IDE0021.severity = none # Use block body for method
dotnet_diagnostic.IDE0022.severity = none # Use block body for method
dotnet_diagnostic.IDE0040.severity = none # Add accessibility modifiers
dotnet_diagnostic.IDE0058.severity = none # Remove unnecessary expression value
dotnet_diagnostic.IDE0072.severity = none # Populate switch
dotnet_diagnostic.IDE1006.severity = none # Naming rule violation
1 change: 1 addition & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>12.0</LangVersion>
<MicrosoftCodeAnalysisVersion Condition=" '$(MicrosoftCodeAnalysisVersion)' == '' ">3.11</MicrosoftCodeAnalysisVersion>
<MSBuildCopyContentTransitively>false</MSBuildCopyContentTransitively>
Expand Down
10 changes: 3 additions & 7 deletions src/common/CallerArgumentExpressionAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,10 @@
namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
internal sealed class CallerArgumentExpressionAttribute : Attribute
internal sealed class CallerArgumentExpressionAttribute(string parameterName) :
Attribute
{
public CallerArgumentExpressionAttribute(string parameterName)
{
ParameterName = parameterName;
}

public string ParameterName { get; }
public string ParameterName { get; } = parameterName;
}

#endif
4 changes: 2 additions & 2 deletions src/common/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static class Guard
/// <exception cref="ArgumentNullException">Thrown when the argument is null</exception>
public static T ArgumentNotNull<T>(
[NotNull] T? argValue,
[CallerArgumentExpression("argValue")] string? argName = null)
[CallerArgumentExpression(nameof(argValue))] string? argName = null)
where T : class
{
if (argValue is null)
Expand All @@ -39,7 +39,7 @@ public static T ArgumentNotNull<T>(
/// <exception cref="ArgumentException">Thrown when the argument is null or empty</exception>
public static T ArgumentNotNullOrEmpty<T>(
[NotNull] T? argValue,
[CallerArgumentExpression("argValue")] string? argName = null)
[CallerArgumentExpression(nameof(argValue))] string? argName = null)
where T : class, IEnumerable
{
ArgumentNotNull(argValue, argName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

<PropertyGroup>
<AssemblyName>xunit.analyzers.latest.tests.$(TargetFramework)</AssemblyName>
<!--
TODO: Temporarily disable security vulnerability detection because Microsoft.CodeAnalysis.CSharp.CodeFix.Testing
1.1.1 is out of date and 1.1.2 is broken.
-->
<NoWarn>$(NoWarn);NU1902;NU1903;NU1904</NoWarn>
<OutputType>Exe</OutputType>
<PackageId>xunit.analyzers.latest.tests</PackageId>
<RootNamespace>Xunit.Analyzers</RootNamespace>
Expand Down
4 changes: 2 additions & 2 deletions src/xunit.analyzers.fixes/Utility/AsyncHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static class AsyncHelper

var constructedFunctionTypeSymbol =
unboundFunctionTypeSymbol
.Construct(typeArguments.ToArray())
.Construct([.. typeArguments])
.WithNullableAnnotation(declarationTypeSymbol.NullableAnnotation);

return editor.Generator.TypeExpression(constructedFunctionTypeSymbol) as TypeSyntax;
Expand Down Expand Up @@ -188,7 +188,7 @@ static bool IsSystemActionType(
if (arity == 0)
return SymbolEqualityComparer.Default.Equals(typeSymbol.ConstructedFrom, TypeSymbolFactory.Action(compilation));

if (arity >= 1 && arity <= 16)
if (arity is >= 1 and <= 16)
return SymbolEqualityComparer.Default.Equals(typeSymbol.ConstructedFrom, TypeSymbolFactory.Action(compilation, arity));
}

Expand Down
10 changes: 5 additions & 5 deletions src/xunit.analyzers.fixes/Utility/BatchedCodeFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Xunit.Analyzers.Fixes;

public abstract class BatchedCodeFixProvider : CodeFixProvider
public abstract class BatchedCodeFixProvider(params string[] diagnostics) :
CodeFixProvider
{
protected BatchedCodeFixProvider(params string[] diagnostics) =>
FixableDiagnosticIds = diagnostics.ToImmutableArray();

public sealed override ImmutableArray<string> FixableDiagnosticIds { get; }
#pragma warning disable IDE0305 // Cannot convert this due to Roslyn 3.11 vs. 4.11 dependencies
public sealed override ImmutableArray<string> FixableDiagnosticIds { get; } = diagnostics.ToImmutableArray();
#pragma warning restore IDE0305

public sealed override FixAllProvider GetFixAllProvider() =>
WellKnownFixAllProviders.BatchFixer;
Expand Down
7 changes: 2 additions & 5 deletions src/xunit.analyzers.fixes/Utility/BatchedMemberFixProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

namespace Xunit.Analyzers.Fixes;

public abstract class BatchedMemberFixProvider : BatchedCodeFixProvider
public abstract class BatchedMemberFixProvider(params string[] diagnostics) :
BatchedCodeFixProvider(diagnostics)
{
protected BatchedMemberFixProvider(params string[] diagnostics) :
base(diagnostics)
{ }

public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
{
var semanticModel = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
Expand Down
15 changes: 7 additions & 8 deletions src/xunit.analyzers.fixes/Utility/CodeAnalysisExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public static async Task<Document> AddConstructor(
#pragma warning disable CA1308 // These are display names, not normalizations for comparison

// TODO: Make this respect the user's preferences on identifier name style
var fieldName = "_" + typeName.Substring(0, 1).ToLowerInvariant() + typeName.Substring(1, typeName.Length - 1);
var constructorArgName = typeName.Substring(0, 1).ToLowerInvariant() + typeName.Substring(1, typeName.Length - 1);
var fieldName = "_" + typeName.Substring(0, 1).ToLowerInvariant() + typeName.Substring(1);
var constructorArgName = typeName.Substring(0, 1).ToLowerInvariant() + typeName.Substring(1);
var editor = await DocumentEditor.CreateAsync(document, cancellationToken).ConfigureAwait(false);

#pragma warning restore CA1308
Expand Down Expand Up @@ -65,7 +65,7 @@ public static async Task<Document> AddConstructor(
)
);

editor.InsertMembers(declaration, 0, new SyntaxNode[] { fieldDeclaration, constructor });
editor.InsertMembers(declaration, 0, [fieldDeclaration, constructor]);

return editor.GetChangedDocument();
}
Expand Down Expand Up @@ -218,11 +218,10 @@ public static async Task<Document> SetBaseClass(
var baseTypeNode = generator.TypeExpression(baseTypeMetadata);
var baseTypes = generator.GetBaseAndInterfaceTypes(declaration);

SyntaxNode updatedDeclaration;
if (baseTypes is null || baseTypes.Count == 0 || semanticModel.GetTypeInfo(baseTypes[0], cancellationToken).Type?.TypeKind != TypeKind.Class)
updatedDeclaration = generator.AddBaseType(declaration, baseTypeNode);
else
updatedDeclaration = generator.ReplaceNode(declaration, baseTypes[0], baseTypeNode);
var updatedDeclaration =
baseTypes is null || baseTypes.Count == 0 || semanticModel.GetTypeInfo(baseTypes[0], cancellationToken).Type?.TypeKind != TypeKind.Class
? generator.AddBaseType(declaration, baseTypeNode)
: generator.ReplaceNode(declaration, baseTypes[0], baseTypeNode);

editor.ReplaceNode(declaration, updatedDeclaration);
}
Expand Down
37 changes: 13 additions & 24 deletions src/xunit.analyzers.fixes/Utility/ConvertAttributeCodeAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,22 @@

namespace Xunit.Analyzers.Fixes;

public class ConvertAttributeCodeAction : CodeAction
public class ConvertAttributeCodeAction(
string title,
string equivalenceKey,
Document document,
SyntaxList<AttributeListSyntax> attributeLists,
string fromTypeName,
string toTypeName) :
CodeAction
{
readonly SyntaxList<AttributeListSyntax> attributeLists;
readonly Document document;
readonly string fromTypeName;
readonly string toTypeName;

public ConvertAttributeCodeAction(
string title,
string equivalenceKey,
Document document,
SyntaxList<AttributeListSyntax> attributeLists,
string fromTypeName,
string toTypeName)
{
Title = Guard.ArgumentNotNull(title);
EquivalenceKey = Guard.ArgumentNotNull(equivalenceKey);

this.toTypeName = Guard.ArgumentNotNull(toTypeName);
this.fromTypeName = Guard.ArgumentNotNull(fromTypeName);
this.attributeLists = attributeLists;
this.document = Guard.ArgumentNotNull(document);
}
readonly Document document = Guard.ArgumentNotNull(document);
readonly string fromTypeName = Guard.ArgumentNotNull(fromTypeName);
readonly string toTypeName = Guard.ArgumentNotNull(toTypeName);

public override string EquivalenceKey { get; }
public override string EquivalenceKey { get; } = Guard.ArgumentNotNull(equivalenceKey);

public override string Title { get; }
public override string Title { get; } = Guard.ArgumentNotNull(title);

protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,21 @@

namespace Xunit.Analyzers.Fixes;

public class RemoveAttributesOfTypeCodeAction : CodeAction
public class RemoveAttributesOfTypeCodeAction(
string title,
string equivalenceKey,
Document document,
SyntaxList<AttributeListSyntax> attributeLists,
string attributeType,
bool exactMatch = false) :
CodeAction
{
readonly SyntaxList<AttributeListSyntax> attributeLists;
readonly string attributeType;
readonly Document document;
readonly bool exactMatch;

public RemoveAttributesOfTypeCodeAction(
string title,
string equivalenceKey,
Document document,
SyntaxList<AttributeListSyntax> attributeLists,
string attributeType,
bool exactMatch = false)
{
Title = Guard.ArgumentNotNull(title);
EquivalenceKey = Guard.ArgumentNotNull(equivalenceKey);

this.attributeLists = attributeLists;
this.attributeType = Guard.ArgumentNotNull(attributeType);
this.document = Guard.ArgumentNotNull(document);
this.exactMatch = exactMatch;
}
readonly string attributeType = Guard.ArgumentNotNull(attributeType);
readonly Document document = Guard.ArgumentNotNull(document);

public override string EquivalenceKey { get; }
public override string EquivalenceKey { get; } = Guard.ArgumentNotNull(equivalenceKey);

public override string Title { get; }
public override string Title { get; } = Guard.ArgumentNotNull(title);

protected override async Task<Document> GetChangedDocumentAsync(CancellationToken cancellationToken)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ static async Task<Document> UseExactMatchFalse(
.WithNameColon(NameColon("exactMatch"));

var argumentList = invocation.ArgumentList;
if (argumentList.Arguments.Count == 2)
argumentList = argumentList.ReplaceNode(argumentList.Arguments[1], falseArgument);
else
argumentList = argumentList.AddArguments(falseArgument);
argumentList =
argumentList.Arguments.Count == 2
? argumentList.ReplaceNode(argumentList.Arguments[1], falseArgument)
: argumentList.AddArguments(falseArgument);

editor.ReplaceNode(invocation.ArgumentList, argumentList);
return editor.GetChangedDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static async Task<Document> UseRegexCheckAsync(
editor.ReplaceNode(
invocation,
invocation
.WithArgumentList(ArgumentList(SeparatedList(new[] { Argument(regexMember), regexIsMatchInvocation.ArgumentList.Arguments[0] })))
.WithArgumentList(ArgumentList(SeparatedList([Argument(regexMember), regexIsMatchInvocation.ArgumentList.Arguments[0]])))
.WithExpression(memberAccess.WithName(IdentifierName(replacement)))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ static async Task<Document> UseSingleMethod(
var localSymbols = semanticModel.LookupSymbols(startLocation).OfType<ILocalSymbol>().Select(s => s.Name).ToImmutableHashSet();
var replacementNode =
invocation
.WithArgumentList(ArgumentList(SeparatedList(new[] { Argument(collectionVariable) })))
.WithArgumentList(ArgumentList(SeparatedList([Argument(collectionVariable)])))
.WithExpression(memberAccess.WithName(IdentifierName(replacementMethod)));

if (invocation.ArgumentList.Arguments[1].Expression is SimpleLambdaExpressionSyntax lambdaExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,24 +171,14 @@ interface IFunctionFixer
bool ShouldFixParentFunction(SyntaxNode parentFunction, CancellationToken cancellationToken);
}

sealed class AnonymousFunctionFixer : IFunctionFixer
sealed class AnonymousFunctionFixer(
AnonymousFunctionExpressionSyntax anonymousFunction,
SemanticModel semanticModel,
DocumentEditor editor) :
IFunctionFixer
{
public SyntaxNode Function => anonymousFunction;

readonly AnonymousFunctionExpressionSyntax anonymousFunction;
readonly SemanticModel semanticModel;
readonly DocumentEditor editor;

public AnonymousFunctionFixer(
AnonymousFunctionExpressionSyntax anonymousFunction,
SemanticModel semanticModel,
DocumentEditor editor)
{
this.anonymousFunction = anonymousFunction;
this.semanticModel = semanticModel;
this.editor = editor;
}

public async Task Fix(CancellationToken cancellationToken)
{
var modifiers = AsyncHelper.GetModifiersWithAsyncKeywordAdded(anonymousFunction.Modifiers);
Expand Down Expand Up @@ -336,24 +326,14 @@ public bool ShouldFixParentFunction(
}
}

sealed class LocalFunctionFixer : IFunctionFixer
sealed class LocalFunctionFixer(
LocalFunctionStatementSyntax localFunction,
SemanticModel semanticModel,
DocumentEditor editor) :
IFunctionFixer
{
public SyntaxNode Function => localFunction;

readonly LocalFunctionStatementSyntax localFunction;
readonly SemanticModel semanticModel;
readonly DocumentEditor editor;

public LocalFunctionFixer(
LocalFunctionStatementSyntax localFunction,
SemanticModel semanticModel,
DocumentEditor editor)
{
this.localFunction = localFunction;
this.semanticModel = semanticModel;
this.editor = editor;
}

public async Task Fix(CancellationToken cancellationToken)
{
var returnType = await AsyncHelper.GetAsyncReturnType(localFunction.ReturnType, editor, cancellationToken).ConfigureAwait(false);
Expand Down Expand Up @@ -387,30 +367,22 @@ public bool ShouldFixParentFunction(
if (symbol is null)
return false;

return parentFunction
.DescendantNodes()
.Where(node => node is InvocationExpressionSyntax)
.Select(node => semanticModel.GetOperation((InvocationExpressionSyntax)node, cancellationToken) as IInvocationOperation)
.Where(invocation => SymbolEqualityComparer.Default.Equals(invocation?.TargetMethod, symbol))
.Any();
return
parentFunction
.DescendantNodes()
.Where(node => node is InvocationExpressionSyntax)
.Select(node => semanticModel.GetOperation((InvocationExpressionSyntax)node, cancellationToken) as IInvocationOperation)
.Any(invocation => SymbolEqualityComparer.Default.Equals(invocation?.TargetMethod, symbol));
}
}

sealed class MethodFixer : IFunctionFixer
sealed class MethodFixer(
MethodDeclarationSyntax method,
DocumentEditor editor) :
IFunctionFixer
{
public SyntaxNode Function => method;

readonly MethodDeclarationSyntax method;
readonly DocumentEditor editor;

public MethodFixer(
MethodDeclarationSyntax method,
DocumentEditor editor)
{
this.method = method;
this.editor = editor;
}

public async Task Fix(CancellationToken cancellationToken)
{
var returnType = await AsyncHelper.GetAsyncReturnType(method.ReturnType, editor, cancellationToken).ConfigureAwait(false);
Expand Down
Loading

0 comments on commit 3c727df

Please sign in to comment.