Skip to content

Commit

Permalink
Use Count instead of using Any() (#5039)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 21, 2024
1 parent 84c2470 commit f54e2c8
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 33 deletions.
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
[CA1845] Use span-based 'string.Concat' and 'AsSpan' instead of 'Substring'
[CA1847] Use 'string.Contains(char)' instead of 'string.Contains(string)' - needs polyfill
[CA1854] Prefer a 'TryGetValue' call over a Dictionary indexer access
[CA1860] Prefer comparing 'Count' to 0 rather than using 'Any()'
[CA1861] Prefer 'static readonly' fields over constant array arguments
[CA1862] Prefer using 'string.Equals(string, StringComparison)' to perform a case-insensitive comparison
[CA1865] Use 'string.StartsWith(char)' instead of 'string.StartsWith(string)' - needs polyfill
Expand All @@ -116,7 +115,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0040;IDE0044;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0063;IDE0074;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0300;IDE0305;IDE0301;IDE0330;IDE1005</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1827;CA1834;CA1845;CA1847;CA1854;CA1860;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1827;CA1834;CA1845;CA1847;CA1854;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
</PropertyGroup>

</Project>
4 changes: 2 additions & 2 deletions src/NSwag.AspNetCore/SwaggerUiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public bool WithCredentials
public Func<HttpRequest, CancellationToken, Task<IEnumerable<SwaggerUiRoute>>> SwaggerRoutesFactory { get; set; }
#endif

internal override string ActualSwaggerDocumentPath => SwaggerRoutes.Any() ? "" : base.ActualSwaggerDocumentPath;
internal override string ActualSwaggerDocumentPath => SwaggerRoutes.Count > 0 ? "" : base.ActualSwaggerDocumentPath;

#if AspNetOwin
internal override async Task<string> TransformHtmlAsync(string html, IOwinRequest request, CancellationToken cancellationToken)
Expand Down Expand Up @@ -165,7 +165,7 @@ internal override async Task<string> TransformHtmlAsync(string html, HttpRequest
(await SwaggerRoutesFactory(request, cancellationToken)).ToList() :
SwaggerRoutes;

htmlBuilder.Replace("{Urls}", !swaggerRoutes.Any()
htmlBuilder.Replace("{Urls}", swaggerRoutes.Count == 0
? "undefined"
: JsonConvert.SerializeObject(
#pragma warning disable 618
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public IEnumerable<CSharpExceptionDescriptionModel> ExceptionDescriptions
.Where(r => r.ThrowsException)
.SelectMany(r =>
{
if (r.ExpectedSchemas?.Any() == true)
if (r.ExpectedSchemas?.Count > 0)
{
return r.ExpectedSchemas
.Where(s => s.Schema.ActualSchema?.InheritsSchema(_resolver.ExceptionSchema) == true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public async Task<object> RunAsync(CommandLineProcessor processor, IConsoleHost

var currentDirectory = Directory.GetCurrentDirectory();
var files = Directory.GetFiles(currentDirectory, "*.nswag");
if (files.Any())
if (files.Length > 0)
{
foreach (var file in files)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.Commands/Commands/InputOutputCommandBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected async Task<OpenApiDocument> GetInputSwaggerDocument()
document.Host = ServiceHost;
}

if (ServiceSchemes != null && ServiceSchemes.Any())
if (ServiceSchemes != null && ServiceSchemes.Length > 0)
{
document.Schemes = ServiceSchemes.Select(s => (OpenApiSchema)Enum.Parse(typeof(OpenApiSchema), s, true)).ToList();
}
Expand Down
12 changes: 6 additions & 6 deletions src/NSwag.Core/OpenApiParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ public bool IsXmlBodyParameter
}

var parent = Parent as OpenApiOperation;
var consumes = parent?.ActualConsumes?.Any() == true ?
var consumes = parent?.ActualConsumes?.Count > 0 ?
parent.ActualConsumes :
parent?.ActualRequestBody?.Content.Keys;

return consumes?.Any() == true &&
return consumes?.Count > 0 &&
consumes.Any(p => p.Contains("application/xml")) &&
consumes.Any(p => AppJsonRegex.IsMatch(p)) == false;
}
Expand All @@ -245,10 +245,10 @@ public bool IsBinaryBodyParameter
}

var parent = Parent as OpenApiOperation;
if (parent?.ActualConsumes?.Any() == true)
if (parent?.ActualConsumes?.Count > 0 == true)
{
var consumes = parent.ActualConsumes;
return consumes?.Any() == true &&
return consumes?.Count > 0 &&
(Schema?.IsBinary != false ||
consumes.Contains("multipart/form-data")) &&
consumes?.Any(p => p.Contains("*/*")) == false &&
Expand Down Expand Up @@ -277,10 +277,10 @@ public bool HasBinaryBodyWithMultipleMimeTypes
}

var parent = Parent as OpenApiOperation;
if (parent?.ActualConsumes?.Any() == true)
if (parent?.ActualConsumes?.Count > 0)
{
var consumes = parent.ActualConsumes;
return consumes?.Any() == true &&
return consumes?.Count > 0 &&
(consumes.Count > 1 ||
consumes.Any(p => p.Contains("*")));
}
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Core/OpenApiPathItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
}
}

if (operations.Parameters != null && operations.Parameters.Any())
if (operations.Parameters != null && operations.Parameters.Count > 0)
{
writer.WritePropertyName("parameters");
serializer.Serialize(writer, operations.Parameters);
}

if (operations.Servers != null && operations.Servers.Any())
if (operations.Servers != null && operations.Servers.Count > 0)
{
writer.WritePropertyName("servers");
serializer.Serialize(writer, operations.Servers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public async Task When_parameter_has_default_and_schema_type_is_OpenApi3_then_sc
var operation = document.Operations.First(o => o.Path.Contains(nameof(DefaultParametersController.WithDefaultEnum))).Operation;

Assert.Equal((int)MyEnum.Def, operation.Parameters.First().Schema.Default);
Assert.True(operation.Parameters.First().Schema.OneOf.Any());
Assert.True(operation.Parameters[0].Schema.OneOf.Count > 0);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ private List<Type> GenerateApiGroups(
}

var addedOperations = AddOperationDescriptionsToDocument(document, controllerType, operations, generator, schemaResolver);
if (addedOperations.Any() && apiGroup.Key != null)
if (addedOperations.Count > 0 && apiGroup.Key != null)
{
usedControllerTypes.Add(apiGroup.Key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public bool Process(OperationProcessorContext context)
}

var authorizeAttributes = endpointMetadata.OfType<AuthorizeAttribute>().ToList();
if (!authorizeAttributes.Any())
if (authorizeAttributes.Count == 0)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private bool IsFileArray(Type type, JsonTypeDescription typeInfo)
return true;
}

if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Any())
if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Length > 0)
{
var description = _settings.SchemaSettings.ReflectionService.GetDescription(type.GenericTypeArguments[0].ToContextualType(), _settings.SchemaSettings);
if (description.Type == JsonObjectType.File || description.Format == JsonFormatStrings.Binary)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ private bool IsFileArray(Type type, JsonTypeDescription typeInfo)
return true;
}

if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Any())
if (typeInfo.Type == JsonObjectType.Array && type.GenericTypeArguments.Length > 0)
{
var description = _settings.SchemaSettings.ReflectionService.GetDescription(type.GenericTypeArguments[0].ToContextualType(), _settings.SchemaSettings);
if (description.Type == JsonObjectType.File ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ private List<string> GetHttpPaths(Type controllerType, MethodInfo method)
var routeAttributesOnClass = GetAllRouteAttributes(controllerType);
var routePrefixAttribute = GetRoutePrefixAttribute(controllerType);

if (routeAttributes.Any())
if (routeAttributes.Count > 0)
{
foreach (var attribute in routeAttributes)
{
Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Generation/Processors/ApiVersionProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public bool Process(OperationProcessorContext context)
else
{
var versions = GetVersions(context, "ApiVersionAttribute");
if (versions.Any())
if (versions.Length > 0)
{
if (versions.Any(v => IncludedVersions == null || IncludedVersions.Length == 0 || IncludedVersions.Contains(v)))
{
Expand All @@ -54,7 +54,7 @@ public bool Process(OperationProcessorContext context)
var version = mappedVersions.FirstOrDefault(v => IncludedVersions == null || IncludedVersions.Length == 0 || IncludedVersions.Contains(v));
if (version == null && mappedVersions.Length == 0)
{
version = IncludedVersions != null && IncludedVersions.Any() ? IncludedVersions[0] : versions[0];
version = IncludedVersions != null && IncludedVersions.Length > 0 ? IncludedVersions[0] : versions[0];
}

if (version != null)
Expand Down
9 changes: 3 additions & 6 deletions src/NSwag.Generation/Processors/DocumentTagsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static void ProcessTagsAttribute(OpenApiDocument document, Type controll
.Select(t => new OpenApiTag { Name = t })
.ToList();

if (tags.Any())
if (tags.Count > 0)
{
if (document.Tags == null)
{
Expand All @@ -69,12 +69,9 @@ private static void ProcessTagAttributes(OpenApiDocument document, Type controll
.Select(a => (dynamic)a)
.ToArray();

if (tagAttributes.Any())
foreach (var tagAttribute in tagAttributes)
{
foreach (var tagAttribute in tagAttributes)
{
ProcessTagAttribute(document, tagAttribute);
}
ProcessTagAttribute(document, tagAttribute);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Generation/Processors/OperationTagsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ public virtual bool Process(OperationProcessorContext context)

if (context.ControllerType != null)
{
if (!context.OperationDescription.Operation.Tags.Any())
if (context.OperationDescription.Operation.Tags.Count == 0)
{
var typeInfo = context.ControllerType.GetTypeInfo();

ProcessControllerSwaggerTagsAttribute(context.OperationDescription, typeInfo);
ProcessControllerSwaggerTagAttributes(context.OperationDescription, typeInfo);
}

if (!context.OperationDescription.Operation.Tags.Any())
if (context.OperationDescription.Operation.Tags.Count == 0)
{
AddControllerNameTag(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ protected virtual IEnumerable<string> GetScopes(OpenApiOperationDescription oper
methodInfo.DeclaringType.GetTypeInfo().GetCustomAttributes());

var authorizeAttributes = allAttributes.Where(a => a.GetType().Name == "AuthorizeAttribute").ToList();
if (!authorizeAttributes.Any())
if (authorizeAttributes.Count == 0)
{
return Enumerable.Empty<string>();
return [];
}

return authorizeAttributes
Expand Down

0 comments on commit f54e2c8

Please sign in to comment.