Skip to content

Commit 4160214

Browse files
committed
Add VS suggestions
Dispose StringWriter
1 parent e54df2c commit 4160214

9 files changed

Lines changed: 21 additions & 28 deletions

File tree

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
158158
{ "dnfin|doNotForceIsNullable", "do not force generator to emit IsNullable = true in XmlElement annotation for nillable elements when element is nullable (minOccurs < 1 or parent element is choice) (default is false)", v => doNotForceIsNullable = v != null },
159159
{ "cn|compactTypeNames", "use type names without namespace qualifier for types in the using list (default is false)", v => compactTypeNames = v != null },
160160
{ "cl|commentLanguages=", $"comment languages to use (default is {string.Join(", ", commentLanguages)}; supported are {string.Join(", ", supportedCommentLanguages)})",
161-
v => commentLanguages = v.Split(',').Select(l => l.Trim()).ToArray() },
161+
v => commentLanguages = [.. v.Split(',').Select(l => l.Trim())] },
162162
{ "un|uniqueTypeNames", "generate type names that are unique across namespaces (default is false)", v => uniqueTypeNamesAcrossNamespaces = v != null },
163163
{ "gc|generatedCodeAttribute", "add version information to GeneratedCodeAttribute (default is true)", v => createGeneratedCodeAttributeVersion = v != null },
164164
{ "nc|netCore", "generate .NET Core specific code that might not work with .NET Framework (default is false)", v => netCoreSpecificCode = v != null },

XmlSchemaClassGenerator.Tests/Compiler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public static Assembly GenerateFiles(string name, IEnumerable<string> files, Gen
134134

135135
public static Assembly CompileFiles(string name, IEnumerable<string> files)
136136
{
137-
return Compile(name, files.Select(f => File.ReadAllText(f)).ToArray());
137+
return Compile(name, [.. files.Select(f => File.ReadAllText(f))]);
138138
}
139139

140140
private static readonly LanguageVersion MaxLanguageVersion = Enum

XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<CoverletOutputFormat>opencover</CoverletOutputFormat>
1717
<CoverletOutput>../coverage.xml</CoverletOutput>
1818
<Include>[XmlSchemaClassGenerator]*</Include>
19-
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045</NoWarn>
19+
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;CS8981</NoWarn>
2020
</PropertyGroup>
2121
<ItemGroup>
2222
<ProjectReference Include="..\XmlSampleGenerator\XmlSampleGenerator.csproj" />

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ public static string GenerateNamespace(string xmlns, string namespacePrefix = nu
496496
var splitSegments = ((IEnumerable<string>)segments[0].Split('.')).Reverse().Where(s => s != "www").ToList();
497497
if (segments.Count > 1)
498498
splitSegments.AddRange(segments.Skip(1));
499-
segments = splitSegments.SelectMany(s => s.Split('-', '.', '_')).ToList();
499+
segments = [.. splitSegments.SelectMany(s => s.Split('-', '.', '_'))];
500500
}
501501
}
502502

XmlSchemaClassGenerator/FileOutputWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private void WriteSeparateFiles(CodeNamespace cn)
8282

8383
Directory.CreateDirectory(dirPath);
8484

85-
cns.Imports.AddRange(cn.Imports.Cast<CodeNamespaceImport>().ToArray());
85+
cns.Imports.AddRange([.. cn.Imports.Cast<CodeNamespaceImport>()]);
8686
cns.Comments.AddRange(cn.Comments);
8787
ccu.Namespaces.Add(cns);
8888

XmlSchemaClassGenerator/ModelBuilder.cs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -475,8 +475,7 @@ private InterfaceModel CreateInterfaceModel(XmlSchemaAnnotated group, string nam
475475

476476
interfaceModel.Documentation.AddRange(docs);
477477

478-
if (namespaceModel != null)
479-
namespaceModel.Types[name] = interfaceModel;
478+
namespaceModel?.Types[name] = interfaceModel;
480479

481480
if (!qualifiedName.IsEmpty)
482481
builder.SetType(group, qualifiedName, interfaceModel);
@@ -533,8 +532,7 @@ private TypeModel CreateTypeModel(XmlSchemaComplexType complexType)
533532

534533
classModel.Documentation.AddRange(docs);
535534

536-
if (namespaceModel != null)
537-
namespaceModel.Types[classModel.Name] = classModel;
535+
namespaceModel?.Types[classModel.Name] = classModel;
538536

539537
if (!qualifiedName.IsEmpty)
540538
builder.SetType(complexType, qualifiedName, classModel);
@@ -669,8 +667,8 @@ private TypeModel CreateTypeModel(XmlSchemaSimpleType simpleType)
669667

670668
var facets = simpleType.Content switch
671669
{
672-
XmlSchemaSimpleTypeRestriction typeRestriction when !_configuration.MergeRestrictionsWithBase => typeRestriction.Facets.Cast<XmlSchemaFacet>().ToList(),
673-
XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out baseFacets) => baseFacets.SelectMany(f => f).ToList(),
670+
XmlSchemaSimpleTypeRestriction typeRestriction when !_configuration.MergeRestrictionsWithBase => [.. typeRestriction.Facets.Cast<XmlSchemaFacet>()],
671+
XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out baseFacets) => [.. baseFacets.SelectMany(f => f)],
674672
_ => MergeRestrictions(simpleType)
675673
};
676674

@@ -683,7 +681,7 @@ XmlSchemaSimpleTypeUnion typeUnion when AllMembersHaveFacets(typeUnion, out base
683681
if (enumFacets.Count > 0 && (baseFacets is null || baseFacets.TrueForAll(fs => fs.OfType<XmlSchemaEnumerationFacet>().Any())) && !_configuration.EnumAsString)
684682
return CreateEnumModel(simpleType, enumFacets);
685683

686-
restrictions = CodeUtilities.GetRestrictions(facets, simpleType, _configuration).Where(r => r != null).Sanitize().ToList();
684+
restrictions = [.. CodeUtilities.GetRestrictions(facets, simpleType, _configuration).Where(r => r != null).Sanitize()];
687685
}
688686

689687
return CreateSimpleModel(simpleType, restrictions ?? []);
@@ -692,7 +690,7 @@ static bool AllMembersHaveFacets(XmlSchemaSimpleTypeUnion typeUnion, out List<IE
692690
{
693691
var members = typeUnion.BaseMemberTypes.Select(b => b.Content as XmlSchemaSimpleTypeRestriction);
694692
var retval = members.All(r => r?.Facets.Count > 0);
695-
baseFacets = !retval ? null : members.Select(r => r.Facets.Cast<XmlSchemaFacet>()).ToList();
693+
baseFacets = !retval ? null : [.. members.Select(r => r.Facets.Cast<XmlSchemaFacet>())];
696694
return retval;
697695
}
698696

@@ -769,8 +767,7 @@ private EnumModel CreateSimpleContentEnumModel(ClassModel classModel, List<XmlSc
769767
}
770768

771769
enumModel.Values = EnsureEnumValuesUnique(enumModel.Values);
772-
if (namespaceModel != null)
773-
namespaceModel.Types[enumModel.Name] = enumModel;
770+
namespaceModel?.Types[enumModel.Name] = enumModel;
774771

775772
return enumModel;
776773
}
@@ -811,8 +808,7 @@ private EnumModel CreateEnumModel(XmlSchemaSimpleType simpleType, List<XmlSchema
811808
}
812809

813810
enumModel.Values = EnsureEnumValuesUnique(enumModel.Values);
814-
if (namespaceModel != null)
815-
namespaceModel.Types[enumModel.Name] = enumModel;
811+
namespaceModel?.Types[enumModel.Name] = enumModel;
816812

817813
if (!qualifiedName.IsEmpty)
818814
builder.SetType(simpleType, qualifiedName, enumModel);
@@ -838,8 +834,7 @@ private SimpleModel CreateSimpleModel(XmlSchemaSimpleType simpleType, List<Restr
838834
simpleModel.Documentation.AddRange(docs);
839835
simpleModel.Restrictions.AddRange(restrictions);
840836

841-
if (namespaceModel != null)
842-
namespaceModel.Types[simpleModel.Name] = simpleModel;
837+
namespaceModel?.Types[simpleModel.Name] = simpleModel;
843838

844839
if (!qualifiedName.IsEmpty)
845840
builder.SetType(simpleType, qualifiedName, simpleModel);
@@ -1162,12 +1157,11 @@ public IEnumerable<Particle> GetElements(XmlSchemaObject item, XmlSchemaObject p
11621157
public static List<DocumentationModel> GetDocumentation(XmlSchemaAnnotated annotated)
11631158
{
11641159
return annotated.Annotation == null ? []
1165-
: annotated.Annotation.Items.OfType<XmlSchemaDocumentation>()
1160+
: [.. annotated.Annotation.Items.OfType<XmlSchemaDocumentation>()
11661161
.Where(d => d.Markup?.Length > 0)
11671162
.Select(d => d.Markup.Select(m => new DocumentationModel { Language = d.Language, Text = m.OuterXml }))
11681163
.SelectMany(d => d)
1169-
.Where(d => !string.IsNullOrEmpty(d.Text))
1170-
.ToList();
1164+
.Where(d => !string.IsNullOrEmpty(d.Text))];
11711165
}
11721166

11731167
public IEnumerable<CodeNamespace> GenerateCode()

XmlSchemaClassGenerator/Models/PropertyModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,7 @@ public void AddMembersTo(CodeTypeDeclaration typeDeclaration, bool withDataBindi
269269
{
270270
var defaultValueExpression = propertyType.GetDefaultValueFor(DefaultValue, IsAttribute);
271271

272-
if (backingField != null)
273-
backingField.InitExpression = defaultValueExpression;
272+
backingField?.InitExpression = defaultValueExpression;
274273

275274
member.Type = IsNillableValueType ? NullableTypeRef(typeReference) : typeReference;
276275

@@ -518,7 +517,7 @@ [new CodeMethodReturnStatement(valueExpression)],
518517
foreach (var propertyAttribute in arrayItemProperty.GetAttributes(false, OwningType).ToList())
519518
{
520519
var arrayItemAttribute = AttributeDecl<XmlArrayItemAttribute>(
521-
propertyAttribute.Arguments.Cast<CodeAttributeArgument>().Where(x => !string.Equals(x.Name, nameof(Order), StringComparison.Ordinal)).ToArray());
520+
[.. propertyAttribute.Arguments.Cast<CodeAttributeArgument>().Where(x => !string.Equals(x.Name, nameof(Order), StringComparison.Ordinal))]);
522521
var namespacePresent = arrayItemAttribute.Arguments.OfType<CodeAttributeArgument>().Any(a => a.Name == Namespace);
523522
if (!namespacePresent && !arrayItemProperty.XmlSchemaName.IsEmpty && !string.IsNullOrEmpty(arrayItemProperty.XmlSchemaName.Namespace))
524523
arrayItemAttribute.Arguments.Add(new(Namespace, new CodePrimitiveExpression(arrayItemProperty.XmlSchemaName.Namespace)));

XmlSchemaClassGenerator/Models/SimpleModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ private static string GetFullTypeName(string typeName, CodeTypeReference typeRef
4545
{
4646
Type = { Options = CodeTypeReferenceOptions.GenericTypeParameter }
4747
};
48-
var writer = new System.IO.StringWriter();
48+
using var writer = new System.IO.StringWriter();
4949
CSharpProvider.GenerateCodeFromExpression(typeOfExpr, writer, new CodeGeneratorOptions());
5050
var fullTypeName = writer.ToString();
51-
Debug.Assert(fullTypeName.StartsWith("typeof(") && fullTypeName.EndsWith(")"));
51+
Debug.Assert(fullTypeName.StartsWith("typeof(") && fullTypeName.EndsWith(")"), $"Expected typeof expression, got: {fullTypeName}");
5252
return fullTypeName.Substring(7, fullTypeName.Length - 8);
5353
}
5454

XmlSchemaClassGenerator/NamingExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static class NamingExtensions
8080

8181
private static Regex CreateInvalidCharsRegex()
8282
{
83-
var r = string.Join("", InvalidChars.Keys.Select(c => string.Format(@"\x{0:x2}", (int)c)).ToArray());
83+
var r = string.Join("", [.. InvalidChars.Keys.Select(c => string.Format(@"\x{0:x2}", (int)c))]);
8484
return new Regex("[" + r + "]", RegexOptions.Compiled);
8585
}
8686

0 commit comments

Comments
 (0)