Skip to content

Commit c1e2d59

Browse files
committed
Enable .NET analyzers
* fix issues
1 parent 8808c6f commit c1e2d59

40 files changed

+150
-92
lines changed

YamlDotNet/Core/AnchorName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace YamlDotNet.Core
2626
{
2727
public struct AnchorName : IEquatable<AnchorName>
2828
{
29-
public static readonly AnchorName Empty = default;
29+
public static readonly AnchorName Empty;
3030

3131
// https://yaml.org/spec/1.2/spec.html#id2785586
3232
private static readonly Regex AnchorPattern = new Regex(@"^[^\[\]\{\},]+$", StandardRegexOptions.Compiled);

YamlDotNet/Core/CharacterAnalyzer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public bool Check(string expectedCharacters, int offset = 0)
161161
Debug.Assert(expectedCharacters.Length > 1, "Use Check(char, int) instead.");
162162

163163
var character = Buffer.Peek(offset);
164-
return expectedCharacters.IndexOf(character) != -1;
164+
return expectedCharacters.Contains(character);
165165
}
166166
}
167167
}

YamlDotNet/Core/Emitter.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private bool ValueIsRepresentableInOutputEncoding(string value)
537537
}
538538
}
539539

540-
private bool IsUnicode(Encoding encoding)
540+
private static bool IsUnicode(Encoding encoding)
541541
{
542542
return encoding is UTF8Encoding ||
543543
encoding is UnicodeEncoding ||
@@ -726,7 +726,7 @@ private void EmitDocumentStart(ParsingEvent evt, bool isFirst)
726726

727727
if (documentStart.Version != null)
728728
{
729-
AnalyzeVersionDirective(documentStart.Version);
729+
Emitter.AnalyzeVersionDirective(documentStart.Version);
730730

731731
var documentVersion = documentStart.Version.Version;
732732
isImplicit = false;
@@ -792,7 +792,7 @@ private void EmitDocumentStart(ParsingEvent evt, bool isFirst)
792792
}
793793
}
794794

795-
private TagDirectiveCollection NonDefaultTagsAmong(IEnumerable<TagDirective>? tagCollection)
795+
private static TagDirectiveCollection NonDefaultTagsAmong(IEnumerable<TagDirective>? tagCollection)
796796
{
797797
var directives = new TagDirectiveCollection();
798798
if (tagCollection == null)
@@ -811,7 +811,7 @@ private TagDirectiveCollection NonDefaultTagsAmong(IEnumerable<TagDirective>? ta
811811
return directives;
812812
}
813813

814-
private void AnalyzeVersionDirective(VersionDirective versionDirective)
814+
private static void AnalyzeVersionDirective(VersionDirective versionDirective)
815815
{
816816
if (versionDirective.Version.Major != Constants.MajorVersion || versionDirective.Version.Minor > Constants.MinorVersion)
817817
{
@@ -1714,7 +1714,7 @@ private bool CheckSimpleKey()
17141714
switch (events.Peek().Type)
17151715
{
17161716
case EventType.Alias:
1717-
length = AnchorNameLength(anchorData.Anchor);
1717+
length = Emitter.AnchorNameLength(anchorData.Anchor);
17181718
break;
17191719

17201720
case EventType.Scalar:
@@ -1724,7 +1724,7 @@ private bool CheckSimpleKey()
17241724
}
17251725

17261726
length =
1727-
AnchorNameLength(anchorData.Anchor) +
1727+
Emitter.AnchorNameLength(anchorData.Anchor) +
17281728
SafeStringLength(tagData.Handle) +
17291729
SafeStringLength(tagData.Suffix) +
17301730
SafeStringLength(scalarData.Value);
@@ -1736,7 +1736,7 @@ private bool CheckSimpleKey()
17361736
return false;
17371737
}
17381738
length =
1739-
AnchorNameLength(anchorData.Anchor) +
1739+
Emitter.AnchorNameLength(anchorData.Anchor) +
17401740
SafeStringLength(tagData.Handle) +
17411741
SafeStringLength(tagData.Suffix);
17421742
break;
@@ -1747,7 +1747,7 @@ private bool CheckSimpleKey()
17471747
return false;
17481748
}
17491749
length =
1750-
AnchorNameLength(anchorData.Anchor) +
1750+
Emitter.AnchorNameLength(anchorData.Anchor) +
17511751
SafeStringLength(tagData.Handle) +
17521752
SafeStringLength(tagData.Suffix);
17531753
break;
@@ -1759,14 +1759,14 @@ private bool CheckSimpleKey()
17591759
return length <= maxSimpleKeyLength;
17601760
}
17611761

1762-
private int AnchorNameLength(AnchorName value)
1762+
private static int AnchorNameLength(AnchorName value)
17631763
{
17641764
return value.IsEmpty ? 0 : value.Value.Length;
17651765
}
17661766

1767-
private int SafeStringLength(string? value)
1767+
private static int SafeStringLength(string? value)
17681768
{
1769-
return value == null ? 0 : value.Length;
1769+
return value?.Length ?? 0;
17701770
}
17711771

17721772
private bool CheckEmptySequence() => CheckEmptyStructure<SequenceStart, SequenceEnd>();
@@ -1894,7 +1894,7 @@ private static string UrlEncode(string text)
18941894
var buffer = bufferBuilder.Builder;
18951895
foreach (var toEncode in Encoding.UTF8.GetBytes(match.Value))
18961896
{
1897-
buffer.AppendFormat("%{0:X02}", toEncode);
1897+
buffer.AppendFormat(CultureInfo.InvariantCulture, "%{0:X02}", toEncode);
18981898
}
18991899
return buffer.ToString();
19001900
});

YamlDotNet/Core/EmitterSettings.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public sealed class EmitterSettings
4343
/// <summary>
4444
/// If true, write the output in canonical form.
4545
/// </summary>
46-
public bool IsCanonical { get; } = false;
46+
public bool IsCanonical { get; }
4747

4848
/// <summary>
4949
/// If true, write output without anchor names.
@@ -61,7 +61,7 @@ public sealed class EmitterSettings
6161
/// <summary>
6262
/// Indent sequences. The default is to not indent.
6363
/// </summary>
64-
public bool IndentSequences { get; } = false;
64+
public bool IndentSequences { get; }
6565

6666
public static readonly EmitterSettings Default = new EmitterSettings();
6767

YamlDotNet/Core/InsertionQueue.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public sealed class InsertionQueue<T> : IEnumerable<T>
3939
private int readPtr;
4040
private int writePtr;
4141
private int mask;
42-
private int count = 0;
42+
private int count;
4343

4444
public InsertionQueue(int initialCapacity = DefaultInitialCapacity)
4545
{

YamlDotNet/Core/Mark.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,35 @@ public int CompareTo(Mark other)
122122
}
123123
return cmp;
124124
}
125+
126+
public static bool operator ==(Mark left, Mark right)
127+
{
128+
return left.Equals(right);
129+
}
130+
131+
public static bool operator !=(Mark left, Mark right)
132+
{
133+
return !(left == right);
134+
}
135+
136+
public static bool operator <(Mark left, Mark right)
137+
{
138+
return left.CompareTo(right) < 0;
139+
}
140+
141+
public static bool operator <=(Mark left, Mark right)
142+
{
143+
return left.CompareTo(right) <= 0;
144+
}
145+
146+
public static bool operator >(Mark left, Mark right)
147+
{
148+
return left.CompareTo(right) > 0;
149+
}
150+
151+
public static bool operator >=(Mark left, Mark right)
152+
{
153+
return left.CompareTo(right) >= 0;
154+
}
125155
}
126156
}

YamlDotNet/Core/MergingParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private bool HandleMergeSequence(LinkedListNode<ParsingEvent> sequenceStart, Lin
117117
return false;
118118
}
119119

120-
private bool IsMergeToken(LinkedListNode<ParsingEvent> node)
120+
private static bool IsMergeToken(LinkedListNode<ParsingEvent> node)
121121
{
122122
return node.Value is Scalar merge && merge.Value == "<<";
123123
}
@@ -220,7 +220,7 @@ public IEnumerable<LinkedListNode<ParsingEvent>> FromAnchor(AnchorName anchor)
220220

221221
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
222222

223-
private IEnumerable<LinkedListNode<ParsingEvent>> Enumerate(LinkedListNode<ParsingEvent>? node)
223+
private static IEnumerable<LinkedListNode<ParsingEvent>> Enumerate(LinkedListNode<ParsingEvent>? node)
224224
{
225225
while (node != null)
226226
{

YamlDotNet/Core/Parser.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private void Skip()
204204
/// stream ::= STREAM-START implicit_document? explicit_document* STREAM-END
205205
/// ************
206206
/// </summary>
207-
private ParsingEvent ParseStreamStart()
207+
private Events.StreamStart ParseStreamStart()
208208
{
209209
var current = GetCurrentToken();
210210

@@ -419,7 +419,7 @@ private ParsingEvent ParseDocumentContent()
419419
/// <summary>
420420
/// Generate an empty scalar event.
421421
/// </summary>
422-
private static ParsingEvent ProcessEmptyScalar(in Mark position)
422+
private static Events.Scalar ProcessEmptyScalar(in Mark position)
423423
{
424424
return new Events.Scalar(AnchorName.Empty, TagName.Empty, string.Empty, ScalarStyle.Plain, true, false, position, position);
425425
}
@@ -634,7 +634,7 @@ private ParsingEvent ParseNode(bool isBlock, bool isIndentlessSequence)
634634
/// *************
635635
/// </summary>
636636

637-
private ParsingEvent ParseDocumentEnd()
637+
private Events.DocumentEnd ParseDocumentEnd()
638638
{
639639
var current = GetCurrentToken() ?? throw new SemanticErrorException("Reached the end of the stream while parsing a document end");
640640

@@ -956,7 +956,7 @@ private ParsingEvent ParseFlowSequenceEntryMappingValue()
956956
/// flow_sequence_entry ::= flow_node | KEY flow_node? (VALUE flow_node?)?
957957
/// *
958958
/// </summary>
959-
private ParsingEvent ParseFlowSequenceEntryMappingEnd()
959+
private Events.MappingEnd ParseFlowSequenceEntryMappingEnd()
960960
{
961961
state = ParserState.FlowSequenceEntry;
962962
var current = GetCurrentToken();

YamlDotNet/Core/Scanner.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public class Scanner : IScanner
6868
private bool streamEndProduced;
6969
private bool plainScalarFollowedByComment;
7070
private long flowSequenceStartLine;
71-
private bool flowCollectionFetched = false;
72-
private bool startFlowCollectionFetched = false;
71+
private bool flowCollectionFetched;
72+
private bool startFlowCollectionFetched;
7373
private long indent = -1;
7474
private bool flowScalarFetched;
7575
private bool simpleKeyAllowed;
@@ -78,7 +78,7 @@ public class Scanner : IScanner
7878
private bool tokenAvailable;
7979
private Token? previous;
8080
private Anchor? previousAnchor;
81-
private Scalar? lastScalar = null;
81+
private Scalar? lastScalar;
8282

8383
private bool IsDocumentStart() =>
8484
!analyzer.EndOfInput &&
@@ -1282,7 +1282,9 @@ private void FetchAnchor(bool isAlias)
12821282
tokens.Enqueue(ScanAnchor(isAlias));
12831283
}
12841284

1285+
#pragma warning disable CA1859
12851286
private Token ScanAnchor(bool isAlias)
1287+
#pragma warning restore CA1859
12861288
{
12871289
// Eat the indicator character.
12881290

@@ -1369,8 +1371,7 @@ private void FetchTag()
13691371
/// <summary>
13701372
/// Scan a TAG token.
13711373
/// </summary>
1372-
1373-
Token ScanTag()
1374+
private Tag ScanTag()
13741375
{
13751376
var start = cursor.Mark();
13761377

@@ -1478,8 +1479,7 @@ private void FetchBlockScalar(bool isLiteral)
14781479
/// <summary>
14791480
/// Scan a block scalar.
14801481
/// </summary>
1481-
1482-
Token ScanBlockScalar(bool isLiteral)
1482+
private Scalar ScanBlockScalar(bool isLiteral)
14831483
{
14841484
using var valueBuilder = StringBuilderPool.Rent();
14851485
var value = valueBuilder.Builder;
@@ -1629,13 +1629,13 @@ Token ScanBlockScalar(bool isLiteral)
16291629
}
16301630
else
16311631
{
1632-
value.Append(leadingBreak.ToString());
1632+
value.Append(leadingBreak);
16331633
leadingBreak.Length = 0;
16341634
}
16351635

16361636
// Append the remaining line breaks.
16371637

1638-
value.Append(trailingBreaks.ToString());
1638+
value.Append(trailingBreaks);
16391639
trailingBreaks.Length = 0;
16401640

16411641
// Is it a leading whitespace?
@@ -2064,20 +2064,20 @@ private Scalar ScanFlowScalar(bool isSingleQuoted)
20642064
}
20652065
else
20662066
{
2067-
value.Append(trailingBreaks.ToString());
2067+
value.Append(trailingBreaks);
20682068
}
20692069
}
20702070
else
20712071
{
2072-
value.Append(leadingBreak.ToString());
2073-
value.Append(trailingBreaks.ToString());
2072+
value.Append(leadingBreak);
2073+
value.Append(trailingBreaks);
20742074
}
20752075
leadingBreak.Length = 0;
20762076
trailingBreaks.Length = 0;
20772077
}
20782078
else
20792079
{
2080-
value.Append(whitespaces.ToString());
2080+
value.Append(whitespaces);
20812081
whitespaces.Length = 0;
20822082
}
20832083
}
@@ -2220,7 +2220,7 @@ private Scalar ScanPlainScalar(ref bool isMultiline)
22202220
}
22212221
if (flowLevel > 0 && cursor.LineOffset < currentIndent)
22222222
{
2223-
throw new Exception();
2223+
throw new InvalidOperationException();
22242224
}
22252225
// Copy the character.
22262226

@@ -2382,7 +2382,7 @@ private void SkipWhitespaces()
23822382
/// %YAML 1.1 # a comment \n
23832383
/// ^^^^^^
23842384
/// </summary>
2385-
private Token ScanVersionDirectiveValue(in Mark start)
2385+
private VersionDirective ScanVersionDirectiveValue(in Mark start)
23862386
{
23872387
SkipWhitespaces();
23882388

@@ -2413,7 +2413,7 @@ private Token ScanVersionDirectiveValue(in Mark start)
24132413
/// %TAG !yaml! tag:yaml.org,2002: \n
24142414
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24152415
/// </summary>
2416-
private Token ScanTagDirectiveValue(in Mark start)
2416+
private TagDirective ScanTagDirectiveValue(in Mark start)
24172417
{
24182418
SkipWhitespaces();
24192419

@@ -2499,15 +2499,15 @@ private string ScanTagUri(string? head, Mark start)
24992499
}
25002500

25012501
var result = tag.ToString();
2502-
if (result.EndsWith(","))
2502+
if (result.EndsWith(','))
25032503
{
25042504
throw new SyntaxErrorException(cursor.Mark(), cursor.Mark(), "Unexpected comma at end of tag");
25052505
}
25062506

25072507
return result;
25082508
}
25092509

2510-
private static readonly byte[] EmptyBytes = new byte[0];
2510+
private static readonly byte[] EmptyBytes = Array.Empty<byte>();
25112511

25122512
/// <summary>
25132513
/// Decode an URI-escape sequence corresponding to a single UTF-8 character.

YamlDotNet/Core/TagName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace YamlDotNet.Core
2525
{
2626
public readonly struct TagName : IEquatable<TagName>
2727
{
28-
public static readonly TagName Empty = default;
28+
public static readonly TagName Empty;
2929

3030
private readonly string? value;
3131

0 commit comments

Comments
 (0)