From c242bddf79535552640ad850a83cee300075b4bf 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 96db16ed5..e370a61f2 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -50,7 +50,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 @@ -59,7 +58,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 @@ -67,7 +65,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 b93e82ebf..ca92e5f26 100644 --- a/src/NJsonSchema/Collections/ObservableDictionary.cs +++ b/src/NJsonSchema/Collections/ObservableDictionary.cs @@ -133,30 +133,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, @@ -164,20 +155,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 44a43742b..07f116042 100644 --- a/src/NJsonSchema/Generation/JsonSchemaGenerator.cs +++ b/src/NJsonSchema/Generation/JsonSchemaGenerator.cs @@ -369,10 +369,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; } @@ -1205,15 +1202,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 962271bdc..83ac16e0a 100644 --- a/src/NJsonSchema/JsonSchema.cs +++ b/src/NJsonSchema/JsonSchema.cs @@ -1028,55 +1028,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