Skip to content

Commit

Permalink
Use compound assignment (#5046)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 2ab9847 commit 79ca06b
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 76 deletions.
4 changes: 1 addition & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
[IDE0011] Add braces to 'if' statement
[IDE0008] Use explicit type instead of 'var'
[IDE0017] Object initialization can be simplified
[IDE0019] Use pattern matching
[IDE0021] Use block body for constructor
[IDE0022] Use block body for method
[IDE0025] Use expression body for property
Expand All @@ -68,7 +67,6 @@
[IDE0059] Unnecessary assignment of a value
[IDE0060] Remove unused parameter
[IDE0061] Use block body for local function
[IDE0074] Use compound assignment
[IDE0090] 'new' expression can be simplified
[IDE0130] Namespace does not match folder structure
[IDE0160] Convert to block scoped namespace
Expand Down Expand Up @@ -103,7 +101,7 @@
[SYSLIB0012] 'Assembly.CodeBase' is obsolete
-->
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0074;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);IDE0005;IDE0008;IDE0011;IDE0017;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0056;IDE0057;IDE0059;IDE0060;IDE0061;IDE0090;IDE0130;IDE0160;IDE0200;IDE0260;IDE0270;IDE0290;IDE0330</NoWarn>
<NoWarn>$(NoWarn);CA1200;CA1304;CA1305;CA1310;CA1311;CA1507;CA1510;CA1514;CA1710;CA1716;CA1720;CA1725;CA1805;CA1834;CA1845;CA1847;CA1861;CA1862;CA1865;CA1866;CA1870;CA2249;CA2263;SYSLIB0012</NoWarn>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ public IEnumerable<string> ExceptionClassNames
{
get
{
var settings = _settings as CSharpClientGeneratorSettings;
if (settings != null)
if (_settings is CSharpClientGeneratorSettings settings)
{
if (settings.OperationNameGenerator.SupportsMultipleClients)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public string MethodAccessModifier
get
{
var controllerName = _settings.GenerateControllerName(ControllerName);
var settings = _settings as CSharpClientGeneratorSettings;
if (settings != null && settings.ProtectedMethods?.Contains(controllerName + "." + ConversionUtilities.ConvertToUpperCamelCase(OperationName, false) + "Async") == true)
if (_settings is CSharpClientGeneratorSettings settings && settings.ProtectedMethods?.Contains(controllerName + "." + ConversionUtilities.ConvertToUpperCamelCase(OperationName, false) + "Async") == true)
{
return "protected";
}
Expand Down Expand Up @@ -201,8 +200,7 @@ public string RouteName
{
get
{
var settings = _settings as CSharpControllerGeneratorSettings;
if (settings != null)
if (_settings is CSharpControllerGeneratorSettings settings)
{
return settings.GetRouteName(_operation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,7 @@ public static async Task<ProjectMetadata> GetProjectMetadata(

var args = CreateMsBuildArguments(file, framework, configuration, runtime, noBuild, outputPath);

var metadata = await TryReadingUsingGetProperties(args, file, noBuild);
if (metadata == null)
{
metadata = await ReadUsingMsBuildTargets(args, file, buildExtensionsDir, console);
}
var metadata = await TryReadingUsingGetProperties(args, file, noBuild) ?? await ReadUsingMsBuildTargets(args, file, buildExtensionsDir, console);

var platformTarget = metadata[nameof(PlatformTarget)];
if (platformTarget.Length == 0)
Expand Down Expand Up @@ -125,11 +121,7 @@ private static async Task<Dictionary<string, string>> ReadUsingMsBuildTargets(
string buildExtensionsDir,
IConsoleHost console)
{
if (buildExtensionsDir == null)
{
// fallback
buildExtensionsDir = Path.Combine(Path.GetDirectoryName(file), "obj");
}
buildExtensionsDir ??= Path.Combine(Path.GetDirectoryName(file), "obj");

Directory.CreateDirectory(buildExtensionsDir);

Expand Down
5 changes: 1 addition & 4 deletions src/NSwag.Commands/HostApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ public static IServiceProvider GetServiceProvider(Assembly assembly)
}
}

if (serviceProvider == null)
{
serviceProvider = GetServiceProviderWithHostFactoryResolver(assembly);
}
serviceProvider ??= GetServiceProviderWithHostFactoryResolver(assembly);

if (serviceProvider == null)
{
Expand Down
6 changes: 1 addition & 5 deletions src/NSwag.Core/OpenApiOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ internal void UpdateRequestBody(OpenApiParameter parameter)

if (parameter.Kind == OpenApiParameterKind.Body)
{
if (RequestBody == null)
{
RequestBody = new OpenApiRequestBody();
}

RequestBody ??= new OpenApiRequestBody();
RequestBody.Name = parameter.Name;
RequestBody.Position = parameter.Position;
RequestBody.Description = parameter.Description;
Expand Down
6 changes: 1 addition & 5 deletions src/NSwag.Core/OpenApiPathItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,7 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
}
else if (propertyName.StartsWith("x-", StringComparison.OrdinalIgnoreCase))
{
if (operations.ExtensionData == null)
{
operations.ExtensionData = new Dictionary<string, object>();
}

operations.ExtensionData ??= new Dictionary<string, object>();
operations.ExtensionData[propertyName] = serializer.Deserialize(reader);
}
else if (propertyName.Contains("$ref"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,9 @@ private List<Type> GenerateApiGroups(
path = "/" + path;
}

var httpMethod = apiDescription.HttpMethod?.ToLowerInvariant();
if (httpMethod == null)
{
httpMethod = apiDescription.ParameterDescriptions.Any(p => p.Source == BindingSource.Body)
? OpenApiOperationMethod.Post
: OpenApiOperationMethod.Get;
}
var httpMethod = apiDescription.HttpMethod?.ToLowerInvariant() ?? (apiDescription.ParameterDescriptions.Any(p => p.Source == BindingSource.Body)
? OpenApiOperationMethod.Post
: OpenApiOperationMethod.Get);

var operation = new OpenApiOperation();
#if NETCOREAPP3_1_OR_GREATER
Expand Down Expand Up @@ -406,10 +402,7 @@ await OpenApiDocument.FromJsonAsync(Settings.DocumentTemplate).ConfigureAwait(fa
document.Generator = $"NSwag{version}";
document.SchemaType = Settings.SchemaSettings.SchemaType;

if (document.Info == null)
{
document.Info = new OpenApiInfo();
}
document.Info ??= new OpenApiInfo();

if (string.IsNullOrEmpty(Settings.DocumentTemplate))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,12 +338,7 @@ private static JsonSchema CreateOrGetFormDataSchema(OperationProcessorContext co
requestBody.Content[MultipartFormData] = value;
}

if (value.Schema == null)
{
value.Schema = new JsonSchema();
}

return value.Schema;
return value.Schema ??= new JsonSchema();
}

private static JsonSchemaProperty CreateFormDataProperty(OperationProcessorContext context, ExtendedApiParameterDescription extendedApiParameter, JsonSchema schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ public bool Process(OperationProcessorContext context)
.GetCustomAttributes()
.SingleOrDefault(a => a.GetType().Name == "ConsumesAttribute");

if (consumesAttribute == null)
{
// If the action method does not have a Consumes Attribute we'll try with its containing class
consumesAttribute = context.MethodInfo.DeclaringType
.GetTypeInfo()
.GetCustomAttributes()
.SingleOrDefault(a => a.GetType().Name == "ConsumesAttribute");
}
// If the action method does not have a Consumes Attribute we'll try with its containing class
consumesAttribute ??= context.MethodInfo.DeclaringType
.GetTypeInfo()
.GetCustomAttributes()
.SingleOrDefault(a => a.GetType().Name == "ConsumesAttribute");

if (consumesAttribute != null && consumesAttribute.ContentTypes != null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ await OpenApiDocument.FromJsonAsync(Settings.DocumentTemplate).ConfigureAwait(fa
document.Consumes = new List<string> { "application/json" };
document.Produces = new List<string> { "application/json" };

if (document.Info == null)
{
document.Info = new OpenApiInfo();
}
document.Info ??= new OpenApiInfo();

if (string.IsNullOrEmpty(Settings.DocumentTemplate))
{
Expand Down
6 changes: 1 addition & 5 deletions src/NSwag.Generation/Processors/DocumentTagsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ private static void ProcessTagsAttribute(OpenApiDocument document, Type controll

if (tags.Count > 0)
{
if (document.Tags == null)
{
document.Tags = new List<OpenApiTag>();
}

document.Tags ??= new List<OpenApiTag>();
foreach (var tag in tags)
{
if (document.Tags.All(t => t.Name != tag.Name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public class OperationExtensionDataProcessor : IOperationProcessor
public bool Process(OperationProcessorContext context)
{
var operation = context.OperationDescription.Operation;
if (operation.ExtensionData == null)
{
operation.ExtensionData = new Dictionary<string, object>();
}
operation.ExtensionData ??= new Dictionary<string, object>();

if (context.MethodInfo != null)
{
Expand Down
6 changes: 1 addition & 5 deletions src/NSwag.Generation/Processors/OperationTagsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ private static void ProcessSwaggerTagsAttribute(OpenApiDocument document, OpenAp

if (ObjectExtensions.HasProperty(tagsAttribute, "AddToDocument") && tagsAttribute.AddToDocument)
{
if (document.Tags == null)
{
document.Tags = new List<OpenApiTag>();
}

document.Tags ??= new List<OpenApiTag>();
if (document.Tags.All(t => t.Name != tag))
{
document.Tags.Add(new OpenApiTag { Name = tag });
Expand Down

0 comments on commit 79ca06b

Please sign in to comment.