Skip to content

Commit

Permalink
Merge pull request #980 from lahma/collection-expressions
Browse files Browse the repository at this point in the history
Use collection expressions

+semver:fix
  • Loading branch information
EdwardCooke authored Sep 26, 2024
2 parents 485daaa + b6749af commit 875a4cd
Show file tree
Hide file tree
Showing 29 changed files with 42 additions and 45 deletions.
4 changes: 2 additions & 2 deletions YamlDotNet/Core/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace YamlDotNet.Core
public static class Constants
{
public static readonly TagDirective[] DefaultTagDirectives =
{
[
new TagDirective("!", "!"),
new TagDirective("!!", "tag:yaml.org,2002:")
};
];

public const int MajorVersion = 1;
public const int MinorVersion = 3;
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class Emitter : IEmitter
private readonly Stack<EmitterState> states = new Stack<EmitterState>();
private readonly Queue<ParsingEvent> events = new Queue<ParsingEvent>();
private readonly Stack<int> indents = new Stack<int>();
private readonly TagDirectiveCollection tagDirectives = new TagDirectiveCollection();
private readonly TagDirectiveCollection tagDirectives = [];
private int indent;
private int flowLevel;
private bool isMappingContext;
Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Core/MergingParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ private sealed class ParsingEventCollection : IEnumerable<LinkedListNode<Parsing

public ParsingEventCollection()
{
events = new LinkedList<ParsingEvent>();
deleted = new HashSet<LinkedListNode<ParsingEvent>>();
references = new Dictionary<AnchorName, LinkedListNode<ParsingEvent>>();
events = [];
deleted = [];
references = [];
}

public void AddAfter(LinkedListNode<ParsingEvent> node, IEnumerable<ParsingEvent> items)
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace YamlDotNet.Core
public class Parser : IParser
{
private readonly Stack<ParserState> states = new Stack<ParserState>();
private readonly TagDirectiveCollection tagDirectives = new TagDirectiveCollection();
private readonly TagDirectiveCollection tagDirectives = [];
private ParserState state;

private readonly IScanner scanner;
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Helpers/FsharpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static bool IsFsharpListType(Type t)
.GetType("Microsoft.FSharp.Collections.ListModule")
.GetMethod("OfArray")
.MakeGenericMethod(itemsType)
.Invoke(null, new[] { arr });
.Invoke(null, [arr]);

return fsharpList;
}
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Helpers/OrderedDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public OrderedDictionary() : this(EqualityComparer<TKey>.Default)

public OrderedDictionary(IEqualityComparer<TKey> comparer)
{
list = new List<KeyValuePair<TKey, TValue>>();
list = [];
dictionary = new Dictionary<TKey, TValue>(comparer);
this.comparer = comparer;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public bool TryGetValue(TKey key, [MaybeNullWhen(false)] out TValue value) =>
internal void OnDeserializedMethod(StreamingContext context)
{
// Reconstruct the dictionary from the serialized list
dictionary = new Dictionary<TKey, TValue>();
dictionary = [];
foreach (var kvp in list)
{
dictionary[kvp.Key] = kvp.Value;
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/RepresentationModel/DocumentLoadingState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
internal class DocumentLoadingState
{
private readonly Dictionary<AnchorName, YamlNode> anchors = new Dictionary<AnchorName, YamlNode>();
private readonly List<YamlNode> nodesWithUnresolvedAliases = new List<YamlNode>();
private readonly Dictionary<AnchorName, YamlNode> anchors = [];
private readonly List<YamlNode> nodesWithUnresolvedAliases = [];

/// <summary>
/// Adds the specified node to the anchor list.
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/RepresentationModel/EmitterState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ internal class EmitterState
/// Gets the already emitted anchors.
/// </summary>
/// <value>The emitted anchors.</value>
public HashSet<AnchorName> EmittedAnchors { get; } = new HashSet<AnchorName>();
public HashSet<AnchorName> EmittedAnchors { get; } = [];
}
}
4 changes: 2 additions & 2 deletions YamlDotNet/RepresentationModel/YamlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ internal YamlDocument(IParser parser)
/// </summary>
private class AnchorAssigningVisitor : YamlVisitorBase
{
private readonly HashSet<AnchorName> existingAnchors = new HashSet<AnchorName>();
private readonly HashSet<AnchorName> existingAnchors = [];
/// <summary>
/// Key: Node, Value: IsDuplicate
/// </summary>
private readonly Dictionary<YamlNode, bool> visitedNodes = new Dictionary<YamlNode, bool>();
private readonly Dictionary<YamlNode, bool> visitedNodes = [];

public void AssignAnchors(YamlDocument document)
{
Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/RepresentationModel/YamlMappingNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
public sealed class YamlMappingNode : YamlNode, IEnumerable<KeyValuePair<YamlNode, YamlNode>>, IYamlConvertible
{
private readonly OrderedDictionary<YamlNode, YamlNode> children = new OrderedDictionary<YamlNode, YamlNode>();
private readonly OrderedDictionary<YamlNode, YamlNode> children = [];

/// <summary>
/// Gets the children of the current node.
Expand Down Expand Up @@ -196,13 +196,13 @@ internal override void ResolveAliases(DocumentLoadingState state)
{
if (entry.Key is YamlAliasNode)
{
keysToUpdate ??= new Dictionary<YamlNode, YamlNode>();
keysToUpdate ??= [];
// TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML
keysToUpdate.Add(entry.Key, state.GetNode(entry.Key.Anchor!, entry.Key.Start, entry.Key.End));
}
if (entry.Value is YamlAliasNode)
{
valuesToUpdate ??= new Dictionary<YamlNode, YamlNode>();
valuesToUpdate ??= [];
// TODO: The representation model should be redesigned, because here the anchor could be null but that would be invalid YAML
valuesToUpdate.Add(entry.Key, state.GetNode(entry.Value.Anchor!, entry.Value.Start, entry.Value.End));
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/RepresentationModel/YamlSequenceNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace YamlDotNet.RepresentationModel
[DebuggerDisplay("Count = {children.Count}")]
public sealed class YamlSequenceNode : YamlNode, IEnumerable<YamlNode>, IYamlConvertible
{
private readonly List<YamlNode> children = new List<YamlNode>();
private readonly List<YamlNode> children = [];

/// <summary>
/// Gets the collection of child nodes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace YamlDotNet.Serialization.BufferedDeserialization
{
public class TypeDiscriminatingNodeDeserializerOptions : ITypeDiscriminatingNodeDeserializerOptions
{
internal readonly List<ITypeDiscriminator> discriminators = new List<ITypeDiscriminator>();
internal readonly List<ITypeDiscriminator> discriminators = [];

/// <summary>
/// Adds an <see cref="ITypeDiscriminator" /> to be checked by the TypeDiscriminatingNodeDeserializer.
Expand Down Expand Up @@ -64,4 +64,4 @@ public void AddUniqueKeyTypeDiscriminator<T>(IDictionary<string, Type> uniqueKey
this.discriminators.Add(new UniqueKeyTypeDiscriminator(typeof(T), uniqueKeyTypeMapping));
}
}
}
}
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/BuilderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ internal BuilderSkeleton(ITypeResolver typeResolver)
{ typeof(SystemTypeConverter), _ => new SystemTypeConverter() }
};

typeInspectorFactories = new LazyComponentRegistrationList<ITypeInspector, ITypeInspector>();
typeInspectorFactories = [];
this.typeResolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver));
settings = new Settings();
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/DeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public sealed class DeserializerBuilder : BuilderSkeleton<DeserializerBuilder>
public DeserializerBuilder()
: base(new StaticTypeResolver())
{
typeMappings = new Dictionary<Type, Type>();
typeMappings = [];
objectFactory = new Lazy<IObjectFactory>(() => new DefaultObjectFactory(typeMappings, settings), true);

tagMappings = new Dictionary<TagName, Type>
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/LazyComponentRegistrationList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace YamlDotNet.Serialization
{
internal sealed class LazyComponentRegistrationList<TArgument, TComponent> : IEnumerable<Func<TArgument, TComponent>>
{
private readonly List<LazyComponentRegistration> entries = new List<LazyComponentRegistration>();
private readonly List<LazyComponentRegistration> entries = [];

public LazyComponentRegistrationList<TArgument, TComponent> Clone()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private class AnchorAssignment
public AnchorName Anchor;
}

private readonly Dictionary<object, AnchorAssignment> assignments = new Dictionary<object, AnchorAssignment>();
private readonly Dictionary<object, AnchorAssignment> assignments = [];
private uint nextId;

public AnchorAssigner(IEnumerable<IYamlTypeConverter> typeConverters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public sealed class AnchorAssigningObjectGraphVisitor : ChainedObjectGraphVisito
{
private readonly IEventEmitter eventEmitter;
private readonly IAliasProvider aliasProvider;
private readonly HashSet<AnchorName> emittedAliases = new HashSet<AnchorName>();
private readonly HashSet<AnchorName> emittedAliases = [];

public AnchorAssigningObjectGraphVisitor(IObjectGraphVisitor<IEmitter> nextVisitor, IEventEmitter eventEmitter, IAliasProvider aliasProvider)
: base(nextVisitor)
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/SerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public sealed class SerializerBuilder : BuilderSkeleton<SerializerBuilder>
private readonly LazyComponentRegistrationList<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>> preProcessingPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
private readonly Dictionary<Type, TagName> tagMappings = new Dictionary<Type, TagName>();
private readonly Dictionary<Type, TagName> tagMappings = [];
private readonly IObjectFactory objectFactory;
private int maximumRecursion = 50;
private EmitterSettings emitterSettings = EmitterSettings.Default;
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/StaticBuilderSkeleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ internal StaticBuilderSkeleton(ITypeResolver typeResolver)
{ typeof(GuidConverter), _ => new GuidConverter(false) },
};

typeInspectorFactories = new LazyComponentRegistrationList<ITypeInspector, ITypeInspector>();
typeInspectorFactories = [];
this.typeResolver = typeResolver ?? throw new ArgumentNullException(nameof(typeResolver));
settings = new Settings();
}
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/StaticDeserializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public StaticDeserializerBuilder(StaticContext context)
{
this.context = context;
factory = context.GetFactory();
typeMappings = new Dictionary<Type, Type>();
typeMappings = [];

tagMappings = new Dictionary<TagName, Type>
{
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/StaticSerializerBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public sealed class StaticSerializerBuilder : StaticBuilderSkeleton<StaticSerial
private readonly LazyComponentRegistrationList<IEnumerable<IYamlTypeConverter>, IObjectGraphVisitor<Nothing>> preProcessingPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<EmissionPhaseObjectGraphVisitorArgs, IObjectGraphVisitor<IEmitter>> emissionPhaseObjectGraphVisitorFactories;
private readonly LazyComponentRegistrationList<IEventEmitter, IEventEmitter> eventEmitterFactories;
private readonly Dictionary<Type, TagName> tagMappings = new Dictionary<Type, TagName>();
private readonly Dictionary<Type, TagName> tagMappings = [];
private int maximumRecursion = 50;
private EmitterSettings emitterSettings = EmitterSettings.Default;
private DefaultValuesHandling defaultValuesHandlingConfiguration = DefaultValuesHandling.Preserve;
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/StreamFragment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace YamlDotNet.Serialization
/// </summary>
public sealed class StreamFragment : IYamlConvertible
{
private readonly List<ParsingEvent> events = new List<ParsingEvent>();
private readonly List<ParsingEvent> events = [];

/// <summary>
/// Gets or sets the events.
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/TagMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public sealed class TagMappings
/// </summary>
public TagMappings()
{
mappings = new Dictionary<string, Type>();
mappings = [];
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Serialization/Utilities/ObjectAnchorCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ namespace YamlDotNet.Serialization.Utilities
{
internal sealed class ObjectAnchorCollection
{
private readonly Dictionary<string, object> objectsByAnchor = new Dictionary<string, object>();
private readonly Dictionary<object, string> anchorsByObject = new Dictionary<object, string>();
private readonly Dictionary<string, object> objectsByAnchor = [];
private readonly Dictionary<object, string> anchorsByObject = [];

/// <summary>
/// Adds the specified anchor.
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Serialization/Utilities/SerializerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace YamlDotNet.Serialization.Utilities
/// </summary>
public sealed class SerializerState : IDisposable
{
private readonly Dictionary<Type, object> items = new Dictionary<Type, object>();
private readonly Dictionary<Type, object> items = [];

public T Get<T>()
where T : class, new()
Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Serialization/Utilities/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static T ChangeType<T>(object? value, INamingConvention enumNamingConvent
{
try
{
return method.Invoke(null, new[] { value });
return method.Invoke(null, [value]);
}
catch (TargetInvocationException ex)
{
Expand All @@ -200,14 +200,14 @@ public static T ChangeType<T>(object? value, INamingConvention enumNamingConvent
var parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string), typeof(IFormatProvider));
if (parseMethod != null)
{
return parseMethod.Invoke(null, new object[] { value, culture });
return parseMethod.Invoke(null, [value, culture]);
}

// Try with - public static T Parse(string)
parseMethod = destinationType.GetPublicStaticMethod("Parse", typeof(string));
if (parseMethod != null)
{
return parseMethod.Invoke(null, new object[] { value });
return parseMethod.Invoke(null, [value]);
}
}
catch (TargetInvocationException ex)
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Serialization/YamlAttributeOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public int Matches(Type matchType)
}
}

private readonly Dictionary<AttributeKey, List<AttributeMapping>> overrides = new Dictionary<AttributeKey, List<AttributeMapping>>();
private readonly Dictionary<AttributeKey, List<AttributeMapping>> overrides = [];

[return: MaybeNull]
public T GetAttribute<T>(Type type, string member) where T : Attribute
Expand Down Expand Up @@ -150,7 +150,7 @@ public void Add(Type type, string member, Attribute attribute)
var attributeKey = new AttributeKey(attribute.GetType(), member);
if (!overrides.TryGetValue(attributeKey, out var mappings))
{
mappings = new List<AttributeMapping>();
mappings = [];
overrides.Add(attributeKey, mappings);
}
else if (mappings.Contains(mapping))
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Serialization/YamlFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class YamlFormatter
{
CurrencyDecimalSeparator = ".",
CurrencyGroupSeparator = "_",
CurrencyGroupSizes = new[] { 3 },
CurrencyGroupSizes = [3],
CurrencySymbol = string.Empty,
CurrencyDecimalDigits = 99,
NumberDecimalSeparator = ".",
NumberGroupSeparator = "_",
NumberGroupSizes = new[] { 3 },
NumberGroupSizes = [3],
NumberDecimalDigits = 99,
NaNSymbol = ".nan",
PositiveInfinitySymbol = ".inf",
Expand Down
3 changes: 0 additions & 3 deletions YamlDotNet/YamlDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,14 @@
<NoWarn>$(NoWarn);CA1725</NoWarn> <!-- Parameter names should match base declaration and other partial definitions -->
<NoWarn>$(NoWarn);CS1061;CA1016</NoWarn> <!-- Mark assemblies with assembly version -->

<NoWarn>$(NoWarn);IDE0028</NoWarn> <!-- Collection initialization can be simplified -->
<NoWarn>$(NoWarn);IDE0045</NoWarn> <!-- 'if' statement can be simplified -->
<NoWarn>$(NoWarn);IDE0046</NoWarn> <!-- 'if' statement can be simplified -->
<NoWarn>$(NoWarn);IDE0047</NoWarn> <!-- Parentheses can be removed -->
<NoWarn>$(NoWarn);IDE0078</NoWarn> <!-- Use pattern matching -->
<NoWarn>$(NoWarn);IDE0083</NoWarn> <!-- Use pattern matching -->
<NoWarn>$(NoWarn);IDE0090</NoWarn> <!-- 'new' expression can be simplified -->
<NoWarn>$(NoWarn);IDE0110</NoWarn> <!-- Discard can be removed -->
<NoWarn>$(NoWarn);IDE0251</NoWarn> <!-- Member can be made 'readonly' -->
<NoWarn>$(NoWarn);IDE0290</NoWarn> <!-- Use primary constructor -->
<NoWarn>$(NoWarn);IDE0300</NoWarn> <!-- Collection initialization can be simplified -->
</PropertyGroup>

</Project>

0 comments on commit 875a4cd

Please sign in to comment.