Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some NET 9 SDK analysis warnings #986

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion YamlDotNet/Core/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ private void EmitBlockSequenceItem(ParsingEvent evt, bool isFirst)
{
if (isFirst)
{
IncreaseIndent(false, (isMappingContext && !isIndentation));
IncreaseIndent(false, isMappingContext && !isIndentation);
}

if (evt is SequenceEnd)
Expand Down
10 changes: 3 additions & 7 deletions YamlDotNet/Core/ParserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public static T Require<T>(this IParser parser) where T : ParsingEvent
{
if (!parser.Accept<T>(out var required))
{
var @event = parser.Current;
if (@event == null)
{
throw new YamlException($"Expected '{typeof(T).Name}', got nothing.");
}
var @event = parser.Current ?? throw new YamlException($"Expected '{typeof(T).Name}', got nothing.");
throw new YamlException(@event.Start, @event.End, $"Expected '{typeof(T).Name}', got '{@event.GetType().Name}' (at {@event.Start}).");
}
return required;
Expand Down Expand Up @@ -197,8 +193,8 @@ public static bool TryFindMappingEntry(this IParser parser, Func<Scalar, bool> s
parser.SkipThisAndNestedEvents();

break;
case MappingStart _:
case SequenceStart _:
case MappingStart:
case SequenceStart:
parser.SkipThisAndNestedEvents();
break;
default:
Expand Down
6 changes: 3 additions & 3 deletions YamlDotNet/Core/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1943,7 +1943,7 @@ private Scalar ScanFlowScalar(bool isSingleQuoted)
{
throw new SyntaxErrorException(start, cursor.Mark(), "While scanning a quoted scalar, did not find expected hexadecimal number.");
}
character = ((character << 4) + analyzer.AsHex(k));
character = (character << 4) + analyzer.AsHex(k);
}

// Check the value and write the character.
Expand Down Expand Up @@ -1979,7 +1979,7 @@ private Scalar ScanFlowScalar(bool isSingleQuoted)
{
throw new SyntaxErrorException(start, cursor.Mark(), "While scanning a quoted scalar, did not find expected hexadecimal number.");
}
lowSurrogate = ((lowSurrogate << 4) + analyzer.AsHex(k));
lowSurrogate = (lowSurrogate << 4) + analyzer.AsHex(k);
}

for (var k = 0; k < codeLength; ++k)
Expand Down Expand Up @@ -2693,7 +2693,7 @@ private void SaveSimpleKey()
// level.


var isRequired = (flowLevel == 0 && indent == cursor.LineOffset);
var isRequired = flowLevel == 0 && indent == cursor.LineOffset;


// A simple key is required only when it is the first token in the current
Expand Down
10 changes: 2 additions & 8 deletions YamlDotNet/Helpers/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,11 @@ public static class ExpressionExtensions
/// </summary>
public static PropertyInfo AsProperty(this LambdaExpression propertyAccessor)
{
var property = TryGetMemberExpression<PropertyInfo>(propertyAccessor);
if (property == null)
{
throw new ArgumentException("Expected a lambda expression in the form: x => x.SomeProperty", nameof(propertyAccessor));
}

var property = TryGetMemberExpression<PropertyInfo>(propertyAccessor) ?? throw new ArgumentException("Expected a lambda expression in the form: x => x.SomeProperty", nameof(propertyAccessor));
return property;
}

[return: MaybeNull]
private static TMemberInfo TryGetMemberExpression<TMemberInfo>(LambdaExpression lambdaExpression)
private static TMemberInfo? TryGetMemberExpression<TMemberInfo>(LambdaExpression lambdaExpression)
where TMemberInfo : MemberInfo
{
if (lambdaExpression.Parameters.Count != 1)
Expand Down
8 changes: 4 additions & 4 deletions YamlDotNet/RepresentationModel/LibYamlEventStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void WriteTo(TextWriter textWriter)
textWriter.Write(" ---");
}
break;
case MappingEnd _:
case MappingEnd:
textWriter.Write("-MAP");
break;
case MappingStart mappingStart:
Expand Down Expand Up @@ -99,17 +99,17 @@ public void WriteTo(TextWriter textWriter)
}
}
break;
case SequenceEnd _:
case SequenceEnd:
textWriter.Write("-SEQ");
break;
case SequenceStart sequenceStart:
textWriter.Write("+SEQ");
WriteAnchorAndTag(textWriter, sequenceStart);
break;
case StreamEnd _:
case StreamEnd:
textWriter.Write("-STR");
break;
case StreamStart _:
case StreamStart:
textWriter.Write("+STR");
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class YamlNodeIdentityEqualityComparer : IEqualityComparer<YamlNod
#region IEqualityComparer<YamlNode> Members

/// <summary />
public bool Equals([AllowNull] YamlNode x, [AllowNull] YamlNode y)
public bool Equals(YamlNode? x, YamlNode? y)
{
return ReferenceEquals(x, y);
}
Expand Down
5 changes: 1 addition & 4 deletions YamlDotNet/RepresentationModel/YamlVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ protected virtual void VisitChildren(YamlStream stream)
/// </param>
protected virtual void VisitChildren(YamlDocument document)
{
if (document.RootNode != null)
{
document.RootNode.Accept(this);
}
document.RootNode?.Accept(this);
}

/// <summary>
Expand Down
5 changes: 1 addition & 4 deletions YamlDotNet/RepresentationModel/YamlVisitorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,7 @@ protected virtual void VisitChildren(YamlStream stream)
/// </param>
protected virtual void VisitChildren(YamlDocument document)
{
if (document.RootNode != null)
{
document.RootNode.Accept(this);
}
document.RootNode?.Accept(this);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public bool Deserialize(IParser parser, Type expectedType, Func<IParser, Type, o
IList? list;
var canUpdate = true;
Type itemType;
var genericCollectionType = expectedType.GetImplementationOfOpenGenericInterface((typeof(ICollection<>)));
var genericCollectionType = expectedType.GetImplementationOfOpenGenericInterface(typeof(ICollection<>));
if (genericCollectionType != null)
{
var genericArguments = genericCollectionType.GetGenericArguments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, o
{
if (objectFactory.IsDictionary(expectedType))
{
var result = objectFactory.Create(expectedType) as IDictionary;
if (result == null)
if (objectFactory.Create(expectedType) is not IDictionary result)
{
value = null;
return false;
Expand Down
3 changes: 1 addition & 2 deletions YamlDotNet/Serialization/YamlAttributeOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public int Matches(Type matchType)

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

[return: MaybeNull]
public T GetAttribute<T>(Type type, string member) where T : Attribute
public T? GetAttribute<T>(Type type, string member) where T : Attribute
{
if (overrides.TryGetValue(new AttributeKey(typeof(T), member), out var mappings))
{
Expand Down
3 changes: 1 addition & 2 deletions YamlDotNet/YamlDotNet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,12 @@
<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);IDE0007</NoWarn> <!-- use 'var' instead of explicit type -->
<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);IDE0290</NoWarn> <!-- Use primary constructor -->
</PropertyGroup>

Expand Down