Skip to content

Commit

Permalink
Remove redundant equality checks (#1753)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Nov 22, 2024
1 parent 9bbf46b commit e907b20
Show file tree
Hide file tree
Showing 18 changed files with 29 additions and 34 deletions.
3 changes: 1 addition & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
[IDE0059] Unnecessary assignment of a value
[IDE0060] Remove unused parameter
[IDE0090] 'new' expression can be simplified
[IDE0100] Remove redundant equality
[IDE0130] Namespace does not match folder structure
[IDE0160] Convert to block scoped namespace
[IDE0290] Use primary constructor
Expand All @@ -59,7 +58,7 @@
[CA1870] Use a cached 'SearchValues' instance for improved searching performance
[CA2263] Prefer the generic overload 'System.Enum.GetValues<TEnum>()'
-->
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0100;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
<NoWarn>$(NoWarn);IDE0008;IDE0021;IDE0022;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0090;IDE0130;IDE0160;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263</NoWarn>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public CSharpValueGenerator(CSharpGeneratorSettings settings)
var isOptional = schema is JsonSchemaProperty { IsRequired: false };

schema = schema.ActualSchema;
if (schema != null && allowsNull == false && isOptional == false)
if (schema != null && !allowsNull && !isOptional)
{
if (schema.Type.IsArray() ||
schema.Type.IsObject())
Expand Down
6 changes: 2 additions & 4 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,8 @@ public PropertyModel(

/// <summary>Gets a value indicating whether this is an array property which cannot be null.</summary>
public bool HasSetter =>
(_property.IsNullable(_settings.SchemaType) == false && (
(_property.ActualTypeSchema.IsArray && _settings.GenerateImmutableArrayProperties) ||
(_property.ActualTypeSchema.IsDictionary && _settings.GenerateImmutableDictionaryProperties)
)) == false;
_property.IsNullable(_settings.SchemaType) || (!_property.ActualTypeSchema.IsArray || !_settings.GenerateImmutableArrayProperties) &&
(!_property.ActualTypeSchema.IsDictionary || !_settings.GenerateImmutableDictionaryProperties);

/// <summary>Gets the json property required.</summary>
public string JsonPropertyRequiredCode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ public ClassTemplateModel(string typeName, string discriminatorName,

ClassName = typeName;
Properties = _schema.ActualProperties.Values
.Where(v => settings.TypeStyle == TypeScriptTypeStyle.Interface ||
v.IsInheritanceDiscriminator == false)
.Where(v => settings.TypeStyle == TypeScriptTypeStyle.Interface || !v.IsInheritanceDiscriminator)
.Select(property => new PropertyModel(this, property, ClassName, _resolver, _settings))
.ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,8 @@ private string ResolveArrayOrTuple(JsonSchema schema, string? typeNameHint, bool
{
if (schema.Item != null)
{
var isObject = schema.Item.ActualSchema.Type.IsObject() == true;
var isDictionary = schema.Item.ActualSchema.IsDictionary == true;
var isObject = schema.Item.ActualSchema.Type.IsObject();
var isDictionary = schema.Item.ActualSchema.IsDictionary;
var prefix = addInterfacePrefix && SupportsConstructorConversion(schema.Item) && isObject && !isDictionary ? "I" : "";

if (Settings.UseLeafType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ schema.Format is not null &&
}
}

if (!allowsNull && schema is JsonSchemaProperty { IsRequired: false } == false)
if (!allowsNull && schema is not JsonSchemaProperty { IsRequired: false })
{
if (typeResolver.GeneratesType(schema) &&
!schema.ActualTypeSchema.IsEnumeration &&
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration/Models/PropertyModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected PropertyModelBase(
protected string GetTypeNameHint()
{
var propertyName = PropertyName;
if (_property.IsEnumeration == false)
if (!_property.IsEnumeration)
{
return propertyName;
}
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration/TypeResolverBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ protected string ResolveDictionaryValueType(JsonSchema schema, string fallbackTy
return Resolve(schema.AdditionalPropertiesSchema, schema.AdditionalPropertiesSchema.ActualSchema.IsNullable(_settings.SchemaType), null);
}

if (schema.AllowAdditionalProperties == false && schema.PatternProperties.Any())
if (!schema.AllowAdditionalProperties && schema.PatternProperties.Any())
{
var valueTypes = new HashSet<string>(schema.PatternProperties
.Select(p => Resolve(p.Value, p.Value.IsNullable(_settings.SchemaType), null))
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration/ValueGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected ValueGeneratorBase(CodeGeneratorSettingsBase settings)
return GetEnumDefaultValue(schema, actualSchema, typeNameHint, typeResolver);
}

if (schema.Type.IsString() && (schema.Format == null || _unsupportedFormatStrings.Contains(schema.Format) == false))
if (schema.Type.IsString() && (schema.Format == null || !_unsupportedFormatStrings.Contains(schema.Format)))
{
return GetDefaultAsStringLiteral(schema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public override bool CanConvert(Type objectType)
if (jObject.TryGetValue("discriminator", StringComparison.OrdinalIgnoreCase, out JToken? token))
{
var discriminator = token.Value<string>();
if (objectType.Name.Equals(discriminator, StringComparison.Ordinal) == false)
if (!objectType.Name.Equals(discriminator, StringComparison.Ordinal))
{
var exceptionType = Type.GetType("System." + discriminator, false);
if (exceptionType != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public override string GetPropertyName(ContextualAccessorInfo accessorInfo, Json
private void LoadPropertyOrField(JsonProperty jsonProperty, ContextualAccessorInfo accessorInfo, Type parentType, JsonSchema parentSchema, NewtonsoftJsonSchemaGeneratorSettings settings, JsonSchemaGenerator schemaGenerator, JsonSchemaResolver schemaResolver)
{
var propertyTypeDescription = ((IReflectionService)this).GetDescription(accessorInfo.AccessorType, settings);
if (jsonProperty.Ignored == false && schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo) == false)
if (!jsonProperty.Ignored && !schemaGenerator.IsPropertyIgnoredBySettings(accessorInfo))
{
var propertyName = GetPropertyName(jsonProperty, accessorInfo, settings);
var propertyAlreadyExists = parentSchema.Properties.ContainsKey(propertyName);
Expand Down
10 changes: 5 additions & 5 deletions src/NJsonSchema/Generation/JsonSchemaGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -811,8 +811,8 @@ private bool TryHandleSpecialTypes<TSchemaType>(TSchemaType schema, ContextualTy
return true;
}

if (contextualType.OriginalType.IsAssignableToTypeName(nameof(JArray), TypeNameStyle.Name) == false &&
(contextualType.OriginalType.IsAssignableToTypeName(nameof(JToken), TypeNameStyle.Name) == true ||
if (!contextualType.OriginalType.IsAssignableToTypeName(nameof(JArray), TypeNameStyle.Name) &&
(contextualType.OriginalType.IsAssignableToTypeName(nameof(JToken), TypeNameStyle.Name) ||
contextualType.OriginalType == typeof(object)))
{
if (Settings.SchemaType == SchemaType.Swagger2)
Expand Down Expand Up @@ -1176,9 +1176,9 @@ public void AddProperty(
}

if (hasRequiredAttribute &&
propertyTypeDescription.IsEnum == false &&
!propertyTypeDescription.IsEnum &&
propertyTypeDescription.Type == JsonObjectType.String &&
requiredAttribute.TryGetPropertyValue("AllowEmptyStrings", false) == false)
!requiredAttribute.TryGetPropertyValue("AllowEmptyStrings", false))
{
propertySchema.MinLength = 1;
}
Expand Down Expand Up @@ -1230,7 +1230,7 @@ public virtual bool IsPropertyIgnored(ContextualAccessorInfo accessorInfo, Type
return true;
}

if (accessorInfo.IsAttributeDefined<JsonPropertyAttribute>(true) == false &&
if (!accessorInfo.IsAttributeDefined<JsonPropertyAttribute>(true) &&
HasDataContractAttribute(parentType) &&
GetDataMemberAttribute(accessorInfo, parentType) == null)
{
Expand Down
3 changes: 1 addition & 2 deletions src/NJsonSchema/Generation/ReflectionServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,7 @@ public virtual bool IsNullable(ContextualType contextualType, ReferenceTypeNullH
var isValueType = contextualType.Type != typeof(string) &&
contextualType.TypeInfo.IsValueType;

return isValueType == false &&
defaultReferenceTypeNullHandling != ReferenceTypeNullHandling.NotNull;
return !isValueType && defaultReferenceTypeNullHandling != ReferenceTypeNullHandling.NotNull;
}

/// <summary>Checks whether the give type is a string enum.</summary>
Expand Down
6 changes: 3 additions & 3 deletions src/NJsonSchema/Generation/SystemTextJsonReflectionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
}

if (accessorInfo.MemberInfo is PropertyInfo propertyInfo &&
(propertyInfo.GetMethod == null || propertyInfo.GetMethod.IsPrivate == true || propertyInfo.GetMethod.IsStatic == true) &&
(propertyInfo.SetMethod == null || propertyInfo.SetMethod.IsPrivate == true || propertyInfo.SetMethod.IsStatic == true) &&
(propertyInfo.GetMethod == null || propertyInfo.GetMethod.IsPrivate || propertyInfo.GetMethod.IsStatic) &&
(propertyInfo.SetMethod == null || propertyInfo.SetMethod.IsPrivate || propertyInfo.SetMethod.IsStatic) &&
!propertyInfo.IsDefined(typeof(DataMemberAttribute)))
{
continue;
Expand Down Expand Up @@ -99,7 +99,7 @@ public override void GenerateProperties(JsonSchema schema, ContextualType contex
schema.RequiredProperties.Add(propertyName);
}

var isNullable = propertyTypeDescription.IsNullable && hasRequiredAttribute == false;
var isNullable = propertyTypeDescription.IsNullable && !hasRequiredAttribute;

// TODO: Add default value
schemaGenerator.AddProperty(schema, accessorInfo, propertyTypeDescription, propertyName, requiredAttribute, hasRequiredAttribute, isNullable, null, schemaResolver);
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonReferenceResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ private async Task<IJsonReference> ResolveUrlReferenceWithAlreadyResolvedCheckAs
foreach (var member in obj
.GetType()
.GetContextualAccessors()
.Where(p => p.IsAttributeDefined<JsonIgnoreAttribute>(true) == false))
.Where(p => !p.IsAttributeDefined<JsonIgnoreAttribute>(true)))
{
var pathSegment = member.GetName();
if (pathSegment == firstSegment)
Expand Down
8 changes: 4 additions & 4 deletions src/NJsonSchema/JsonSchema.Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public virtual JsonSchema ActualTypeSchema
_patternProperties.Count == 0 &&
AdditionalPropertiesSchema == null &&
MultipleOf == null &&
IsEnumeration == false &&
!IsEnumeration &&
_allOf.Count == 1 &&
_allOf.Any(s => s.HasReference);

Expand All @@ -64,7 +64,7 @@ public virtual JsonSchema ActualTypeSchema
_patternProperties.Count == 0 &&
AdditionalPropertiesSchema == null &&
MultipleOf == null &&
IsEnumeration == false &&
!IsEnumeration &&
_oneOf.Count == 1 &&
_oneOf.Any(s => s.HasReference);

Expand All @@ -77,7 +77,7 @@ public virtual JsonSchema ActualTypeSchema
_patternProperties.Count == 0 &&
AdditionalPropertiesSchema == null &&
MultipleOf == null &&
IsEnumeration == false &&
!IsEnumeration &&
_anyOf.Count == 1 &&
_anyOf.Any(s => s.HasReference);

Expand All @@ -91,7 +91,7 @@ static void ThrowInvalidOperationException(string message)
throw new InvalidOperationException(message);
}

if (checkedSchemas.Contains(this) == true)
if (checkedSchemas.Contains(this))
{
ThrowInvalidOperationException("Cyclic references detected.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ public JsonSchema? AdditionalPropertiesSchema
_patternProperties.Count == 0 &&
AdditionalPropertiesSchema == null &&
MultipleOf == null &&
IsEnumeration == false;
!IsEnumeration;

#endregion

Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema/JsonSchemaProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public bool IsRequired
/// <returns>true if the type can be null.</returns>
public override bool IsNullable(SchemaType schemaType)
{
if (schemaType == SchemaType.Swagger2 && IsRequired == false)
if (schemaType == SchemaType.Swagger2 && !IsRequired)
{
return true;
}
Expand Down

0 comments on commit e907b20

Please sign in to comment.