From 2a4d8c1c0506160054b5f7218cf0eadf7f174608 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Mon, 17 Jul 2023 16:24:55 +0200 Subject: [PATCH] Fix sonarcloud issues --- XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs | 2 +- XmlSchemaClassGenerator/CodeUtilities.cs | 8 ++++---- XmlSchemaClassGenerator/ModelBuilder.cs | 10 +++++----- XmlSchemaClassGenerator/TypeModel.cs | 12 +++++++----- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs b/XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs index d0b04024..e97b8796 100644 --- a/XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs +++ b/XmlSchemaClassGenerator.Tests/SharedTestFunctions.cs @@ -70,7 +70,7 @@ private static void DeserializeSampleXml(ITestOutputHelper output, string patter foreach (var rootElement in set.GlobalElements.Values.Cast().Where(e => !e.IsAbstract - && !(e.ElementSchemaType is XmlSchemaSimpleType) + && e.ElementSchemaType is not XmlSchemaSimpleType && e.ElementSchemaType.QualifiedName != AnyType)) { var type = FindType(assembly, rootElement.QualifiedName); diff --git a/XmlSchemaClassGenerator/CodeUtilities.cs b/XmlSchemaClassGenerator/CodeUtilities.cs index fbe10e79..06fd8acd 100644 --- a/XmlSchemaClassGenerator/CodeUtilities.cs +++ b/XmlSchemaClassGenerator/CodeUtilities.cs @@ -150,18 +150,18 @@ private static Type GetUnionType(GeneratorConfiguration configuration, XmlSchema if (baseMemberEffectiveTypes.Distinct().Count() == 1) return baseMemberEffectiveTypes[0]; // all member types are integer types - if (baseMemberEffectiveTypes.All(t => intTypes.Contains(t))) + if (baseMemberEffectiveTypes.TrueForAll(t => intTypes.Contains(t))) { var maxTypeIndex = baseMemberEffectiveTypes.Max(t => Array.IndexOf(intTypes, t)); var maxType = intTypes[maxTypeIndex]; // if the max type is signed and the corresponding unsigned type is also in the set we have to use the next higher type - if (maxTypeIndex % 2 == 1 && baseMemberEffectiveTypes.Any(t => Array.IndexOf(intTypes, t) == maxTypeIndex - 1)) + if (maxTypeIndex % 2 == 1 && baseMemberEffectiveTypes.Exists(t => Array.IndexOf(intTypes, t) == maxTypeIndex - 1)) return intTypes[maxTypeIndex + 1]; return maxType; } // all member types are float/double/decimal - if (baseMemberEffectiveTypes.All(t => decimalTypes.Contains(t))) + if (baseMemberEffectiveTypes.TrueForAll(t => decimalTypes.Contains(t))) { var maxTypeIndex = baseMemberEffectiveTypes.Max(t => Array.IndexOf(decimalTypes, t)); var maxType = decimalTypes[maxTypeIndex]; @@ -342,7 +342,7 @@ public static KeyValuePair ParseNamespace(string nsArg, st ); public static bool IsUsingNamespace(string namespaceName, GeneratorConfiguration conf) - => UsingNamespaces.Any(n => n.Namespace == namespaceName && n.Condition(conf)); + => UsingNamespaces.Exists(n => n.Namespace == namespaceName && n.Condition(conf)); public static CodeTypeReference CreateTypeReference(Type type, GeneratorConfiguration conf) { diff --git a/XmlSchemaClassGenerator/ModelBuilder.cs b/XmlSchemaClassGenerator/ModelBuilder.cs index 547d3816..c2f664ba 100644 --- a/XmlSchemaClassGenerator/ModelBuilder.cs +++ b/XmlSchemaClassGenerator/ModelBuilder.cs @@ -225,7 +225,7 @@ private void PromoteInterfacePropertiesToCollection() var derivedProperties = interfaceModel.AllDerivedReferenceTypes().SelectMany(t => t.Properties) .Where(p => p.Name == interfaceProperty.Name || p.OriginalPropertyName == interfaceProperty.Name).ToList(); - if (derivedProperties.Any(p => p.IsCollection)) + if (derivedProperties.Exists(p => p.IsCollection)) { foreach (var derivedProperty in derivedProperties.Where(p => !p.IsCollection)) derivedProperty.IsCollection = true; @@ -636,7 +636,7 @@ XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out base // If a union has enum restrictions, there must be an enum restriction in all parts of the union // If there are other restrictions mixed into the enumeration values, we'll generate a string to play it safe. - if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.All(fs => fs.OfType().Any())) && !_configuration.EnumAsString) + if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.TrueForAll(fs => fs.OfType().Any())) && !_configuration.EnumAsString) return CreateEnumModel(simpleType, enumFacets); restrictions = CodeUtilities.GetRestrictions(facets, simpleType, _configuration).Where(r => r != null).Sanitize().ToList(); @@ -698,7 +698,7 @@ private EnumModel CreateEnumModel(XmlSchemaSimpleType simpleType, List() - .Any(a => a.Markup.Any(m => m.Name == "annox:annotate" && m.HasChildNodes && m.FirstChild.Name == "jl:Deprecated")) == true; + .Any(a => Array.Exists(a.Markup, m => m.Name == "annox:annotate" && m.HasChildNodes && m.FirstChild.Name == "jl:Deprecated")) == true; enumModel.Values.Add(value); } @@ -898,7 +898,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM // Discard duplicate property names. This is most likely due to: // - Choice or // - Element and attribute with the same name - if (property != null && !properties.Any(p => p.Name == property.Name)) + if (property != null && !properties.Exists(p => p.Name == property.Name)) { var itemDocs = GetDocumentation(item.XmlParticle); property.Documentation.AddRange(itemDocs); @@ -906,7 +906,7 @@ private IEnumerable CreatePropertiesForElements(Uri source, TypeM if (_configuration.EmitOrder) property.Order = order++; - property.IsDeprecated = itemDocs.Any(d => d.Text.StartsWith("DEPRECATED")); + property.IsDeprecated = itemDocs.Exists(d => d.Text.StartsWith("DEPRECATED")); properties.Add(property); } diff --git a/XmlSchemaClassGenerator/TypeModel.cs b/XmlSchemaClassGenerator/TypeModel.cs index e0b88072..a33b26f8 100644 --- a/XmlSchemaClassGenerator/TypeModel.cs +++ b/XmlSchemaClassGenerator/TypeModel.cs @@ -211,7 +211,7 @@ public IEnumerable AllDerivedReferenceTypes(List DerivedTypes.Any(d => d.XmlSchemaType?.Parent is XmlSchemaRedefine); + public override bool IsRedefined => DerivedTypes.Exists(d => d.XmlSchemaType?.Parent is XmlSchemaRedefine); public bool IsAbstract { get; set; } public bool IsMixed { get; set; } public bool IsSubstitution { get; set; } @@ -391,7 +391,7 @@ public override CodeTypeDeclaration Generate() var propName = "Text"; // To not collide with any existing members - for (var propertyIndex = 1; Properties.Any(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++) + for (var propertyIndex = 1; Properties.Exists(x => x.Name.Equals(propName, StringComparison.Ordinal)) || propName.Equals(classDeclaration.Name, StringComparison.Ordinal); propertyIndex++) { propName = $"Text_{propertyIndex}"; } @@ -748,7 +748,9 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi else { var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue, IsAttribute); - backingField.InitExpression = defaultValueExpression; + + if (backingField != null) + backingField.InitExpression = defaultValueExpression; member.Type = IsNillableValueType ? NullableTypeRef(typeReference) : typeReference; @@ -1384,7 +1386,7 @@ protected IEnumerable GetComments(IList", true); foreach (var doc in docs - .Where(d => string.IsNullOrEmpty(d.Language) || Configuration.CommentLanguages.Any(l => d.Language.StartsWith(l, StringComparison.OrdinalIgnoreCase))) + .Where(d => string.IsNullOrEmpty(d.Language) || Configuration.CommentLanguages.Exists(l => d.Language.StartsWith(l, StringComparison.OrdinalIgnoreCase))) .OrderBy(d => d.Language)) { var text = doc.Text; @@ -1399,7 +1401,7 @@ protected void AddDescription(CodeAttributeDeclarationCollection attributes, IEn { if (!Configuration.GenerateDescriptionAttribute || DisableComments || !docs.Any()) return; - var doc = GetSingleDoc(docs.Where(d => string.IsNullOrEmpty(d.Language) || Configuration.CommentLanguages.Any(l => d.Language.StartsWith(l, StringComparison.OrdinalIgnoreCase)))); + var doc = GetSingleDoc(docs.Where(d => string.IsNullOrEmpty(d.Language) || Configuration.CommentLanguages.Exists(l => d.Language.StartsWith(l, StringComparison.OrdinalIgnoreCase)))); if (doc != null) {