diff --git a/Funcky.Analyzers/Funcky.Analyzers.CodeFixes/ReplaceParameterReferenceRewriter.cs b/Funcky.Analyzers/Funcky.Analyzers.CodeFixes/ReplaceParameterReferenceRewriter.cs index 1ec6329b..d7854f1c 100644 --- a/Funcky.Analyzers/Funcky.Analyzers.CodeFixes/ReplaceParameterReferenceRewriter.cs +++ b/Funcky.Analyzers/Funcky.Analyzers.CodeFixes/ReplaceParameterReferenceRewriter.cs @@ -11,15 +11,10 @@ internal sealed class ReplaceParameterReferenceRewriter( ExpressionSyntax replacement) : CSharpSyntaxRewriter(visitIntoStructuredTrivia: false) { - public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node) - { - if (semanticModel.GetOperation(node) is IParameterReferenceOperation { Parameter.Name: var name } && name == parameterName) - { - return replacement.WithTriviaFrom(node); - } - - return node; - } + public override SyntaxNode VisitIdentifierName(IdentifierNameSyntax node) + => semanticModel.GetOperation(node) is IParameterReferenceOperation { Parameter.Name: var name } && name == parameterName + ? replacement.WithTriviaFrom(node) + : node; } internal static partial class SyntaxNodeExtensions diff --git a/Funcky.Analyzers/Funcky.Analyzers/AlternativeMonadAnalyzer/AlternativeMonadAnalyzer.cs b/Funcky.Analyzers/Funcky.Analyzers/AlternativeMonadAnalyzer/AlternativeMonadAnalyzer.cs index f78682ed..a3d5af13 100644 --- a/Funcky.Analyzers/Funcky.Analyzers/AlternativeMonadAnalyzer/AlternativeMonadAnalyzer.cs +++ b/Funcky.Analyzers/Funcky.Analyzers/AlternativeMonadAnalyzer/AlternativeMonadAnalyzer.cs @@ -54,11 +54,9 @@ private static bool IsMatchInvocation( { matchReceiverType = null; alternativeMonadType = null; - return invocation.TargetMethod.ReceiverType is INamedTypeSymbol receiverType - && invocation.TargetMethod.Name == MatchMethodName - && invocation.Arguments.Length == 2 - && alternativeMonadTypes.Value.TryGetValue(receiverType.ConstructedFrom, out alternativeMonadType) - && (matchReceiverType = receiverType) is var _; + return invocation is { TargetMethod: { ReceiverType: INamedTypeSymbol receiverType, Name: MatchMethodName }, Arguments: [_, _] } + && alternativeMonadTypes.Value.TryGetValue(receiverType.ConstructedFrom, out alternativeMonadType) + && (matchReceiverType = receiverType) is var _; } private static Diagnostic? AnalyzeMatchInvocation( diff --git a/Funcky.Analyzers/Funcky.Analyzers/AnonymousFunctionMatching.cs b/Funcky.Analyzers/Funcky.Analyzers/AnonymousFunctionMatching.cs index cab39b56..6c1531b8 100644 --- a/Funcky.Analyzers/Funcky.Analyzers/AnonymousFunctionMatching.cs +++ b/Funcky.Analyzers/Funcky.Analyzers/AnonymousFunctionMatching.cs @@ -10,7 +10,7 @@ public static bool MatchAnonymousUnaryFunctionWithSingleReturn( IAnonymousFunctionOperation anonymousFunction, [NotNullWhen(true)] out IReturnOperation? returnOperation) => MatchAnonymousFunctionWithSingleReturn(anonymousFunction, out returnOperation) - && anonymousFunction.Symbol.Parameters.Length == 1; + && anonymousFunction.Symbol.Parameters is [_]; /// Matches an anonymous function of the shape (...) => y. public static bool MatchAnonymousFunctionWithSingleReturn( diff --git a/Funcky.Analyzers/Funcky.Analyzers/MonadReturnMatching.cs b/Funcky.Analyzers/Funcky.Analyzers/MonadReturnMatching.cs index 8a12e734..87302cee 100644 --- a/Funcky.Analyzers/Funcky.Analyzers/MonadReturnMatching.cs +++ b/Funcky.Analyzers/Funcky.Analyzers/MonadReturnMatching.cs @@ -24,9 +24,8 @@ private static bool IsReturn(AlternativeMonadType alternativeMonadType, IMethodS private static bool IsReturnFunction(AlternativeMonadType alternativeMonadType, IAnonymousFunctionOperation anonymousFunction) => MatchAnonymousUnaryFunctionWithSingleReturn(anonymousFunction, out var returnOperation) - && returnOperation is { ReturnedValue: IInvocationOperation returnedValue } + && returnOperation is { ReturnedValue: IInvocationOperation { Arguments: [{ Value: IParameterReferenceOperation { Parameter.ContainingSymbol: var parameterContainingSymbol } }] } returnedValue } && IsReturn(alternativeMonadType, returnedValue.TargetMethod) - && returnedValue.Arguments is [{ Value: IParameterReferenceOperation { Parameter.ContainingSymbol: var parameterContainingSymbol } }] && SymbolEqualityComparer.Default.Equals(parameterContainingSymbol, anonymousFunction.Symbol); private static bool IsImplicitReturn(AlternativeMonadType alternativeMonadType, IAnonymousFunctionOperation anonymousFunction) diff --git a/Funcky.Analyzers/Funcky.Analyzers/OperationMatching.cs b/Funcky.Analyzers/Funcky.Analyzers/OperationMatching.cs index 3b5df291..7d02bbd5 100644 --- a/Funcky.Analyzers/Funcky.Analyzers/OperationMatching.cs +++ b/Funcky.Analyzers/Funcky.Analyzers/OperationMatching.cs @@ -22,7 +22,7 @@ public static bool MatchArguments( { firstArgument = null; secondArgument = null; - return operation.Arguments.Length is 2 + return operation.Arguments is [_, _] && (firstArgument = operation.GetArgumentForParameterAtIndex(0)) is var _ && (secondArgument = operation.GetArgumentForParameterAtIndex(1)) is var _ && matchFirstArgument(firstArgument) diff --git a/Funcky.SourceGenerator/OrNoneFromTryPatternGenerator.cs b/Funcky.SourceGenerator/OrNoneFromTryPatternGenerator.cs index f48f3427..b5155b8e 100644 --- a/Funcky.SourceGenerator/OrNoneFromTryPatternGenerator.cs +++ b/Funcky.SourceGenerator/OrNoneFromTryPatternGenerator.cs @@ -42,7 +42,7 @@ private static IncrementalValueProvider> GetOrNone .Collect(); private static bool IsSyntaxTargetForGeneration(SyntaxNode node, CancellationToken cancellationToken) - => node is ClassDeclarationSyntax { AttributeLists.Count: > 0 }; + => node is ClassDeclarationSyntax { AttributeLists: [_, ..] }; private static SemanticTarget? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken) => context.Node is ClassDeclarationSyntax classDeclarationSyntax @@ -51,7 +51,7 @@ private static bool IsSyntaxTargetForGeneration(SyntaxNode node, CancellationTok .Where(a => a.AttributeClass?.ToDisplayString() == AttributeFullName) .Where(AttributeBelongsToPartialPart(classDeclarationSyntax)) .Select(ParseAttribute) - .ToImmutableArray() is { Length: >=1 } attributes + .ToImmutableArray() is [_, ..] attributes ? new SemanticTarget(classDeclarationSyntax, attributes) : null; @@ -59,15 +59,9 @@ private static Func AttributeBelongsToPartialPart(ClassDecl => attribute => attribute.ApplicationSyntaxReference?.GetSyntax().Ancestors().OfType().FirstOrDefault() == partialPart; private static ParsedAttribute ParseAttribute(AttributeData attribute) - { - const int typeNameIndex = 0; - const int methodNameIndex = 1; - return attribute.ConstructorArguments.Length >= 2 - && attribute.ConstructorArguments[typeNameIndex].Value is INamedTypeSymbol type - && attribute.ConstructorArguments[methodNameIndex].Value is string methodName + => attribute.ConstructorArguments is [{ Value: INamedTypeSymbol type }, { Value: string methodName }, ..] ? new ParsedAttribute(type, methodName) : throw new InvalidOperationException("Invalid attribute: expected a named type and a method name"); - } private static MethodPartial ToMethodPartial(SemanticTarget semanticTarget, Compilation compilation) => new( @@ -132,7 +126,7 @@ private static ParameterSyntax GenerateParameter(IParameterSymbol parameter, int .WithAttributeLists(GenerateParameterAttributeLists(parameter)); private static SyntaxList GenerateParameterAttributeLists(IParameterSymbol parameter) - => GenerateParameterAttributes(parameter).ToImmutableArray() is { Length: >0 } attributes + => GenerateParameterAttributes(parameter).ToImmutableArray() is [_, ..] attributes ? SingletonList(AttributeList(SeparatedList(attributes))) : List(); diff --git a/Funcky/Extensions/StringExtensions/Chunk.cs b/Funcky/Extensions/StringExtensions/Chunk.cs index f3404f61..ee787a23 100644 --- a/Funcky/Extensions/StringExtensions/Chunk.cs +++ b/Funcky/Extensions/StringExtensions/Chunk.cs @@ -29,7 +29,7 @@ private static IEnumerable ChunkString(string source, int size) } // If there is anything left to emit, we will emit the last chunk. - if (source.Length > 0) + if (source is not "") { yield return source.Substring(index * size); } diff --git a/Funcky/Extensions/StringExtensions/SplitLazy.cs b/Funcky/Extensions/StringExtensions/SplitLazy.cs index aa981a9f..fa92b907 100644 --- a/Funcky/Extensions/StringExtensions/SplitLazy.cs +++ b/Funcky/Extensions/StringExtensions/SplitLazy.cs @@ -61,7 +61,7 @@ private static FindNextIndex IndexOfCharSeparators(char[] separator) private static FindNextIndex IndexOfStringSeparator(string separator) => (text, startIndex) - => separator.Length == 0 + => separator is "" ? Option<(int Index, int SeparatorLength)>.None : text .IndexOfOrNone(separator, startIndex, StringComparison.Ordinal) @@ -91,7 +91,7 @@ private static FindNextIndex IndexOfStringSeparators(string[] separators) private static Func> IndexOfAnyOrNone(string text, int startIndex) => separator - => separator.Length == 0 + => separator is "" ? Option<(int Index, int SeparatorLength)>.None : text.IndexOfOrNone(separator, startIndex, StringComparison.Ordinal).AndThen(index => (index, separator.Length)); diff --git a/Funcky/Monads/Option/OptionJsonConverter.cs b/Funcky/Monads/Option/OptionJsonConverter.cs index 974e9c37..ff1f4eb1 100644 --- a/Funcky/Monads/Option/OptionJsonConverter.cs +++ b/Funcky/Monads/Option/OptionJsonConverter.cs @@ -8,8 +8,8 @@ namespace Funcky.Monads; public sealed class OptionJsonConverter : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) - => typeToConvert.IsGenericType && - typeToConvert.GetGenericTypeDefinition() == typeof(Option<>); + => typeToConvert.IsGenericType + && typeToConvert.GetGenericTypeDefinition() == typeof(Option<>); [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")] [UnconditionalSuppressMessage("Trimming", "IL2046:\'RequiresUnreferencedCodeAttribute\' annotations must match across all interface implementations or overrides.")] @@ -26,7 +26,7 @@ internal sealed class OptionJsonConverter(JsonConverter itemConver where TItem : notnull { public override Option Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - => reader.TokenType == JsonTokenType.Null + => reader.TokenType is JsonTokenType.Null ? Option.None : itemConverter.Read(ref reader, typeof(TItem), options)!;