From 9f3764658618319a083bfcd8c969cdc12a148418 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Wed, 20 Nov 2024 21:03:23 +0200 Subject: [PATCH] Use compound assignment and simplify delegate invocation --- Directory.Build.props | 4 +- .../Collections/ObservableDictionary.cs | 25 ++------ .../Generation/JsonSchemaGenerator.cs | 16 +---- src/NJsonSchema/JsonSchema.cs | 59 ++++--------------- 4 files changed, 19 insertions(+), 85 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 270ddee19..317e5ece6 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -51,7 +51,6 @@ [IDE0057] Substring can be simplified [IDE0059] Unnecessary assignment of a value [IDE0060] Remove unused parameter - [IDE0074] Use compound assignment [IDE0078] Use pattern matching [IDE0083] Use pattern matching [IDE0090] 'new' expression can be simplified @@ -60,7 +59,6 @@ [IDE0160] Convert to block scoped namespace [IDE0260] Use pattern matching [IDE0290] Use primary constructor - [IDE1005] Delegate invocation can be simplified [CA1200] Avoid using cref tags with a prefix [CA1510] Use 'ArgumentNullException.ThrowIfNull' instead of explicitly throwing a new exception instance [CA1716] rename parameter property so that it no longer conflicts with the reserved language keyword @@ -68,7 +66,7 @@ [CA1870] Use a cached 'SearchValues' instance for improved searching performance [CA2263] Prefer the generic overload 'System.Enum.GetValues()' --> - $(NoWarn);IDE0008;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0074;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0260;IDE0290;IDE1005;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263 + $(NoWarn);IDE0008;IDE0019;IDE0021;IDE0022;IDE0025;IDE0027;IDE0028;IDE0029;IDE0032;IDE0039;IDE0045;IDE0046;IDE0055;IDE0057;IDE0059;IDE0060;IDE0078;IDE0083;IDE0090;IDE0100;IDE0130;IDE0160;IDE0260;IDE0290;CA1200;CA1510;CA1716;CA1720;CA1870;CA2263 diff --git a/src/NJsonSchema/Collections/ObservableDictionary.cs b/src/NJsonSchema/Collections/ObservableDictionary.cs index c936891a4..14f6b5957 100644 --- a/src/NJsonSchema/Collections/ObservableDictionary.cs +++ b/src/NJsonSchema/Collections/ObservableDictionary.cs @@ -130,30 +130,21 @@ private void Insert(TKey key, TValue? value, bool add) private void OnPropertyChanged(string propertyName) { var copy = PropertyChanged; - if (copy != null) - { - copy(this, new PropertyChangedEventArgs(propertyName)); - } + copy?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private void OnCollectionChanged() { OnPropertyChanged(); var copy = CollectionChanged; - if (copy != null) - { - copy(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); - } + copy?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset)); } private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair changedItem) { OnPropertyChanged(); var copy = CollectionChanged; - if (copy != null) - { - copy(this, new NotifyCollectionChangedEventArgs(action, changedItem, 0)); - } + copy?.Invoke(this, new NotifyCollectionChangedEventArgs(action, changedItem, 0)); } private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValuePair newItem, @@ -161,20 +152,14 @@ private void OnCollectionChanged(NotifyCollectionChangedAction action, KeyValueP { OnPropertyChanged(); var copy = CollectionChanged; - if (copy != null) - { - copy(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem, 0)); - } + copy?.Invoke(this, new NotifyCollectionChangedEventArgs(action, newItem, oldItem, 0)); } private void OnCollectionChanged(NotifyCollectionChangedAction action, IList newItems) { OnPropertyChanged(); var copy = CollectionChanged; - if (copy != null) - { - copy(this, new NotifyCollectionChangedEventArgs(action, newItems, 0)); - } + copy?.Invoke(this, new NotifyCollectionChangedEventArgs(action, newItems, 0)); } private void OnPropertyChanged() diff --git a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs index 691dd9d28..86fc18abf 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -366,10 +366,7 @@ public virtual void ApplyDataAnnotations(JsonSchema schema, JsonTypeDescription { if (typeDescription.IsDictionary) { - if (schema.AdditionalPropertiesSchema == null) - { - schema.AdditionalPropertiesSchema = new JsonSchema(); - } + schema.AdditionalPropertiesSchema ??= new JsonSchema(); schema.AdditionalPropertiesSchema.Pattern = regexAttribute.Pattern; } @@ -1202,15 +1199,8 @@ public void AddProperty( propertySchema.IsReadOnly = readOnlyAttribute.IsReadOnly; } - if (propertySchema.Description == null) - { - propertySchema.Description = property.GetDescription(Settings); - } - - if (propertySchema.Example == null) - { - propertySchema.Example = GenerateExample(property); - } + propertySchema.Description ??= property.GetDescription(Settings); + propertySchema.Example ??= GenerateExample(property); dynamic? obsoleteAttribute = property.GetAttributes(true).FirstAssignableToTypeNameOrDefault("System.ObsoleteAttribute"); if (obsoleteAttribute != null) diff --git a/src/NJsonSchema/JsonSchema.cs b/src/NJsonSchema/JsonSchema.cs index a67ea4d33..ccd2d4bf6 100644 --- a/src/NJsonSchema/JsonSchema.cs +++ b/src/NJsonSchema/JsonSchema.cs @@ -1022,55 +1022,16 @@ private static JsonObjectType ConvertStringToJsonObjectType(string? value) private void Initialize() #pragma warning disable CS8774 { - if (Items == null) - { - Items = new ObservableCollection(); - } - - if (Properties == null) - { - Properties = new ObservableDictionary(); - } - - if (PatternProperties == null) - { - PatternProperties = new ObservableDictionary(); - } - - if (Definitions == null) - { - Definitions = new ObservableDictionary(); - } - - if (RequiredProperties == null) - { - RequiredProperties = new Collection(); - } - - if (AllOf == null) - { - AllOf = new ObservableCollection(); - } - - if (AnyOf == null) - { - AnyOf = new ObservableCollection(); - } - - if (OneOf == null) - { - OneOf = new ObservableCollection(); - } - - if (Enumeration == null) - { - Enumeration = new Collection(); - } - - if (EnumerationNames == null) - { - EnumerationNames = []; - } + Items ??= new ObservableCollection(); + Properties ??= new ObservableDictionary(); + PatternProperties ??= new ObservableDictionary(); + Definitions ??= new ObservableDictionary(); + RequiredProperties ??= new Collection(); + AllOf ??= new ObservableCollection(); + AnyOf ??= new ObservableCollection(); + OneOf ??= new ObservableCollection(); + Enumeration ??= new Collection(); + EnumerationNames ??= []; } #pragma warning restore CS8774