Skip to content

Commit

Permalink
Merge pull request #971 from lahma/analyzer-fixes
Browse files Browse the repository at this point in the history
Fix analyzer warnings on NET 9 SDK
+semver:fix
  • Loading branch information
EdwardCooke authored Sep 11, 2024
2 parents 00026fb + eed5d69 commit 3d2e577
Show file tree
Hide file tree
Showing 29 changed files with 68 additions and 103 deletions.
1 change: 0 additions & 1 deletion YamlDotNet.Benchmark/YamlDotNet.Benchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
<TargetFrameworks>net8.0;net47</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/AnchorName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

namespace YamlDotNet.Core
{
public struct AnchorName : IEquatable<AnchorName>
public readonly struct AnchorName : IEquatable<AnchorName>
{
public static readonly AnchorName Empty;

Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Emitter : IEmitter
private static readonly Regex UriReplacer = new Regex(@"[^0-9A-Za-z_\-;?@=$~\\\)\]/:&+,\.\*\(\[!]",
StandardRegexOptions.Compiled | RegexOptions.Singleline);

private static readonly string[] newLineSeparators = new[] { "\r\n", "\r", "\n" };
private static readonly string[] NewLineSeparators = ["\r\n", "\r", "\n"];

private readonly TextWriter output;
private readonly bool outputUsesUnicodeEncoding;
Expand Down Expand Up @@ -653,7 +653,7 @@ private void EmitComment(Comment comment)
return;
}

var lines = comment.Value.Split(newLineSeparators, StringSplitOptions.None);
var lines = comment.Value.Split(NewLineSeparators, StringSplitOptions.None);

if (comment.IsInline)
{
Expand Down
2 changes: 2 additions & 0 deletions YamlDotNet/Core/InsertionQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ public sealed class InsertionQueue<T> : IEnumerable<T>
private int readPtr;
private int writePtr;
private int mask;
#pragma warning disable IDE0032
private int count;
#pragma warning restore IDE0032

public InsertionQueue(int initialCapacity = DefaultInitialCapacity)
{
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/MergingParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private IEnumerable<ParsingEvent> GetMappingEvents(AnchorName anchor)
return events.FromAnchor(anchor).Where(e => !this.events.IsDeleted(e))
.Select(e => e.Value)
.TakeWhile(e => (nesting += e.NestingIncrease) >= 0)
.Select(e => cloner.Clone(e));
.Select(cloner.Clone);
}

private sealed class ParsingEventCollection : IEnumerable<LinkedListNode<ParsingEvent>>
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/Core/ParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public static bool Accept<T>(this IParser parser) where T : ParsingEvent
/// otherwise returns false.</returns>
public static bool TryFindMappingEntry(this IParser parser, Func<Scalar, bool> selector, [MaybeNullWhen(false)] out Scalar? key, [MaybeNullWhen(false)] out ParsingEvent? value)
{
if (parser.TryConsume<MappingStart>(out var _start))
if (parser.TryConsume<MappingStart>(out _))
{
while (parser.Current != null)
{
Expand Down
4 changes: 1 addition & 3 deletions YamlDotNet/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public class Scanner : IScanner
private bool streamStartProduced;
private bool streamEndProduced;
private bool plainScalarFollowedByComment;
private long flowSequenceStartLine;
private bool flowCollectionFetched;
private bool startFlowCollectionFetched;
private long indent = -1;
Expand Down Expand Up @@ -933,7 +932,6 @@ private void FetchFlowCollectionStart(bool isSequenceToken)
if (isSequenceToken)
{
token = new FlowSequenceStart(start, start);
flowSequenceStartLine = token.Start.Line;
}
else
{
Expand Down Expand Up @@ -2520,7 +2518,7 @@ private string ScanTagUri(string? head, Mark start)
return result;
}

private static readonly byte[] EmptyBytes = Array.Empty<byte>();
private static readonly byte[] EmptyBytes = [];

/// <summary>
/// Decode an URI-escape sequence corresponding to a single UTF-8 character.
Expand Down
27 changes: 0 additions & 27 deletions YamlDotNet/GlobalSuppressions.cs

This file was deleted.

3 changes: 0 additions & 3 deletions YamlDotNet/Helpers/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if !NET20

using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
Expand Down Expand Up @@ -82,4 +80,3 @@ private static TMemberInfo TryGetMemberExpression<TMemberInfo>(LambdaExpression
}
}
}
#endif
2 changes: 2 additions & 0 deletions YamlDotNet/Helpers/Polyfills.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma warning disable IDE0130
namespace YamlDotNet
#pragma warning restore IDE0130
{
internal static class Polyfills
{
Expand Down
7 changes: 4 additions & 3 deletions YamlDotNet/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,12 +305,13 @@ public static Attribute[] GetAllCustomAttributes<TAttribute>(this PropertyInfo m
return Attribute.GetCustomAttributes(member, typeof(TAttribute), inherit: true);
}

private static readonly ConcurrentDictionary<Type, bool> typesHaveNullContext = new ConcurrentDictionary<Type, bool>();
private static readonly ConcurrentDictionary<Type, bool> TypesHaveNullContext = new();

public static bool AcceptsNull(this MemberInfo member)
{
var result = true; //default to allowing nulls, this will be set to false if there is a null context on the type
#if NET8_0_OR_GREATER
var typeHasNullContext = typesHaveNullContext.GetOrAdd(member.DeclaringType, (Type t) =>
var typeHasNullContext = TypesHaveNullContext.GetOrAdd(member.DeclaringType, (Type t) =>
{
var attributes = t.GetCustomAttributes(typeof(System.Runtime.CompilerServices.NullableContextAttribute), true);
return (attributes?.Length ?? 0) > 0;
Expand All @@ -325,7 +326,7 @@ public static bool AcceptsNull(this MemberInfo member)

return result;
#else
var typeHasNullContext = typesHaveNullContext.GetOrAdd(member.DeclaringType, (Type t) =>
var typeHasNullContext = TypesHaveNullContext.GetOrAdd(member.DeclaringType, (Type t) =>
{
var attributes = t.GetCustomAttributes(true);
return attributes.Any(x => x.GetType().FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
Expand Down
10 changes: 2 additions & 8 deletions YamlDotNet/RepresentationModel/YamlMappingNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,13 @@ internal override void ResolveAliases(DocumentLoadingState state)
{
if (entry.Key is YamlAliasNode)
{
if (keysToUpdate == null)
{
keysToUpdate = new Dictionary<YamlNode, YamlNode>();
}
keysToUpdate ??= new Dictionary<YamlNode, YamlNode>();
// 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)
{
if (valuesToUpdate == null)
{
valuesToUpdate = new Dictionary<YamlNode, YamlNode>();
}
valuesToUpdate ??= new Dictionary<YamlNode, YamlNode>();
// 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
18 changes: 9 additions & 9 deletions YamlDotNet/RepresentationModel/YamlScalarNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,28 @@ namespace YamlDotNet.RepresentationModel
[DebuggerDisplay("{Value}")]
public sealed class YamlScalarNode : YamlNode, IYamlConvertible
{
private bool _forceImplicitPlain;
private string? _value;
private bool forceImplicitPlain;
private string? value;

/// <summary>
/// Gets or sets the value of the node.
/// </summary>
/// <value>The value.</value>
public string? Value
{
get => _value;
get => value;
set
{
if (value == null)
{
_forceImplicitPlain = true;
forceImplicitPlain = true;
}
else
{
_forceImplicitPlain = false;
forceImplicitPlain = false;
}

_value = value;
this.value = value;
}
}

Expand Down Expand Up @@ -86,7 +86,7 @@ private void Load(IParser parser, DocumentLoadingState state)
var value = scalar.Value;
if (scalar.Style == ScalarStyle.Plain && Tag.IsEmpty)
{
_forceImplicitPlain = value.Length switch
forceImplicitPlain = value.Length switch
{
// we have an implicit null value without a tag stating it, fake it out
0 => true,
Expand All @@ -97,7 +97,7 @@ private void Load(IParser parser, DocumentLoadingState state)
};
}

_value = value;
this.value = value;
Style = scalar.Style;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ internal override void Emit(IEmitter emitter, EmitterState state)
var tag = Tag;
var implicitPlain = tag.IsEmpty;

if (_forceImplicitPlain &&
if (forceImplicitPlain &&
Style == ScalarStyle.Plain &&
(Value == null || Value == ""))
{
Expand Down
2 changes: 1 addition & 1 deletion YamlDotNet/RepresentationModel/YamlStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace YamlDotNet.RepresentationModel
/// </summary>
public class YamlStream : IEnumerable<YamlDocument>
{
private readonly IList<YamlDocument> documents = new List<YamlDocument>();
private readonly List<YamlDocument> documents = [];

/// <summary>
/// Gets the documents inside the stream.
Expand Down
2 changes: 0 additions & 2 deletions YamlDotNet/Serialization/Converters/DateTime8601Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ namespace YamlDotNet.Serialization.Converters
/// </summary>
public class DateTime8601Converter : IYamlTypeConverter
{
private readonly IFormatProvider provider;
private readonly ScalarStyle scalarStyle;

/// <summary>
Expand All @@ -47,7 +46,6 @@ public DateTime8601Converter()
/// </summary>
public DateTime8601Converter(ScalarStyle scalarStyle)
{
this.provider = CultureInfo.InvariantCulture;
this.scalarStyle = scalarStyle;
}

Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Serialization/Deserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ public T Deserialize<T>(IParser parser)

public object? Deserialize(string input)
{
return Deserialize(input, typeof(object));
return Deserialize<object>(input);
}

public object? Deserialize(TextReader input)
{
return Deserialize(input, typeof(object));
return Deserialize<object>(input);
}

public object? Deserialize(IParser parser)
{
return Deserialize(parser, typeof(object));
return Deserialize<object>(parser);
}

public object? Deserialize(string input, Type type)
Expand Down
4 changes: 2 additions & 2 deletions YamlDotNet/Serialization/EventEmitters/JsonEventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class JsonEventEmitter : ChainedEventEmitter
private readonly YamlFormatter formatter;
private readonly INamingConvention enumNamingConvention;
private readonly ITypeInspector typeInspector;
private static readonly Regex numericRegex = new Regex(@"^-?\d+\.?\d+$", RegexOptions.Compiled);
private static readonly Regex NumericRegex = new Regex(@"^-?\d+\.?\d+$", RegexOptions.Compiled);

public JsonEventEmitter(IEventEmitter nextEmitter, YamlFormatter formatter, INamingConvention enumNamingConvention, ITypeInspector typeInspector)
: base(nextEmitter)
Expand Down Expand Up @@ -90,7 +90,7 @@ public override void Emit(ScalarEventInfo eventInfo, IEmitter emitter)
case TypeCode.Decimal:
eventInfo.RenderedValue = formatter.FormatNumber(value);

if (!numericRegex.IsMatch(eventInfo.RenderedValue))
if (!NumericRegex.IsMatch(eventInfo.RenderedValue))
{
eventInfo.Style = ScalarStyle.DoubleQuoted;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
using System.Collections;
using System.Collections.Generic;
using YamlDotNet.Core;
using YamlDotNet.Core.Events;
using YamlDotNet.Helpers;
using YamlDotNet.Serialization.Utilities;

namespace YamlDotNet.Serialization.NodeDeserializers
{
Expand Down Expand Up @@ -53,11 +51,9 @@ public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, o
value = objectFactory.Create(expectedType);

dictionary = value as IDictionary;
if (dictionary == null)
{
// Uncommon case where a type implements IDictionary<TKey, TValue> but not IDictionary
dictionary = (IDictionary?)Activator.CreateInstance(typeof(GenericDictionaryToNonGenericAdapter<,>).MakeGenericType(keyType, valueType), value);
}

// Uncommon case where a type implements IDictionary<TKey, TValue> but not IDictionary
dictionary ??= (IDictionary?)Activator.CreateInstance(typeof(GenericDictionaryToNonGenericAdapter<,>).MakeGenericType(keyType, valueType), value);
}
else if (typeof(IDictionary).IsAssignableFrom(expectedType))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, o
return true;
}

private static object DeserializeBooleanHelper(string value)
private static bool DeserializeBooleanHelper(string value)
{
bool result;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,26 @@ namespace YamlDotNet.Serialization.NodeDeserializers
{
public class StaticDictionaryNodeDeserializer : DictionaryDeserializer, INodeDeserializer
{
private readonly ObjectFactories.StaticObjectFactory _objectFactory;
private readonly ObjectFactories.StaticObjectFactory objectFactory;

public StaticDictionaryNodeDeserializer(ObjectFactories.StaticObjectFactory objectFactory, bool duplicateKeyChecking)
: base(duplicateKeyChecking)
{
_objectFactory = objectFactory ?? throw new ArgumentNullException(nameof(objectFactory));
this.objectFactory = objectFactory ?? throw new ArgumentNullException(nameof(objectFactory));
}

public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object?> nestedObjectDeserializer, out object? value, ObjectDeserializer rootDeserializer)
{
if (_objectFactory.IsDictionary(expectedType))
if (objectFactory.IsDictionary(expectedType))
{
var result = _objectFactory.Create(expectedType) as IDictionary;
var result = objectFactory.Create(expectedType) as IDictionary;
if (result == null)
{
value = null;
return false;
}
var keyType = _objectFactory.GetKeyType(expectedType);
var valueType = _objectFactory.GetValueType(expectedType);
var keyType = objectFactory.GetKeyType(expectedType);
var valueType = objectFactory.GetValueType(expectedType);

value = result;
base.Deserialize(keyType, valueType, reader, nestedObjectDeserializer, result, rootDeserializer);
Expand Down
Loading

0 comments on commit 3d2e577

Please sign in to comment.