From fab5f7774520b897c182e15a314ec7c4a36c15b8 Mon Sep 17 00:00:00 2001 From: Marko Lahma Date: Tue, 23 Jul 2024 16:16:36 +0300 Subject: [PATCH] Enable latest-Recommended analysis rules (#142) --- ...ectory.Build.props => Directory.Build.props | 18 ++++++++++++++++++ Parlot.sln | 1 + src/Parlot/Compilation/CompilationContext.cs | 4 ++-- src/Parlot/Compilation/ExpressionHelper.cs | 6 +++--- src/Parlot/Fluent/CharLiteral.cs | 2 +- src/Parlot/Fluent/Deferred.cs | 6 +++--- src/Parlot/Fluent/NumberLiteral.cs | 2 +- src/Parlot/Fluent/OneOf.cs | 2 +- src/Parlot/Fluent/ParseContext.cs | 4 +++- src/Parlot/Fluent/Parser.TryParse.cs | 2 +- src/Parlot/Fluent/Parsers.cs | 2 ++ src/Parlot/Fluent/Sequence.cs | 14 +++++++------- src/Parlot/Fluent/SequenceAndSkip.cs | 16 ++++++++-------- src/Parlot/Fluent/SequenceSkipAnd.cs | 16 ++++++++-------- src/Parlot/Fluent/StringLiteral.cs | 2 +- src/Parlot/Fluent/TextLiteral.cs | 6 ++++-- src/Parlot/Fluent/Then.cs | 2 +- .../Parlot.Benchmarks/Parlot.Benchmarks.csproj | 1 + test/Parlot.Tests/Parlot.Tests.csproj | 2 +- 19 files changed, 67 insertions(+), 41 deletions(-) rename src/Directory.Build.props => Directory.Build.props (61%) diff --git a/src/Directory.Build.props b/Directory.Build.props similarity index 61% rename from src/Directory.Build.props rename to Directory.Build.props index 7f5dc43..3bcb120 100644 --- a/src/Directory.Build.props +++ b/Directory.Build.props @@ -21,4 +21,22 @@ + + + latest-Recommended + + + $(NoWarn);CA1715; + + + $(NoWarn);CA1051; + + + $(NoWarn);CA1720; + + + $(NoWarn);CA1716; + + + diff --git a/Parlot.sln b/Parlot.sln index df1f156..9dec130 100644 --- a/Parlot.sln +++ b/Parlot.sln @@ -21,6 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution NuGet.config = NuGet.config .github\workflows\publish.yml = .github\workflows\publish.yml README.md = README.md + Directory.Build.props = Directory.Build.props EndProjectSection EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Samples", "src\Samples\Samples.csproj", "{B9A796FE-4BEB-499A-B506-25F20C749527}" diff --git a/src/Parlot/Compilation/CompilationContext.cs b/src/Parlot/Compilation/CompilationContext.cs index ef44827..269f657 100644 --- a/src/Parlot/Compilation/CompilationContext.cs +++ b/src/Parlot/Compilation/CompilationContext.cs @@ -10,7 +10,7 @@ namespace Parlot.Compilation /// public class CompilationContext { - private int _number = 0; + private int _number; public CompilationContext() { @@ -51,7 +51,7 @@ public CompilationContext() /// When set to false, the compiled statements don't need to record and define the property. /// This is done to optimize compiled parser that are usually used for pattern matching only. /// - public bool DiscardResult { get; set; } = false; + public bool DiscardResult { get; set; } /// /// Creates a instance with a and diff --git a/src/Parlot/Compilation/ExpressionHelper.cs b/src/Parlot/Compilation/ExpressionHelper.cs index 6ccc165..0793a2e 100644 --- a/src/Parlot/Compilation/ExpressionHelper.cs +++ b/src/Parlot/Compilation/ExpressionHelper.cs @@ -30,14 +30,14 @@ public static class ExpressionHelper internal static readonly ConstructorInfo Exception_ToString = typeof(Exception).GetConstructor([typeof(string)])!; - internal static ConstructorInfo TextSpan_Constructor = typeof(TextSpan).GetConstructor([typeof(string), typeof(int), typeof(int)])!; + internal static readonly ConstructorInfo TextSpan_Constructor = typeof(TextSpan).GetConstructor([typeof(string), typeof(int), typeof(int)])!; - internal static MethodInfo MemoryExtensions_AsSpan = typeof(MemoryExtensions).GetMethod(nameof(MemoryExtensions.AsSpan), [typeof(string)])!; + internal static readonly MethodInfo MemoryExtensions_AsSpan = typeof(MemoryExtensions).GetMethod(nameof(MemoryExtensions.AsSpan), [typeof(string)])!; public static Expression ArrayEmpty() => ((Expression>)(() => Array.Empty())).Body; public static Expression New() where T : new() => ((Expression>)(() => new T())).Body; - public static Expression> CharacterIsInRange = (cursor,b,c) => Character.IsInRange(cursor.Current, b, c); + public static readonly Expression> CharacterIsInRange = (cursor,b,c) => Character.IsInRange(cursor.Current, b, c); //public static Expression NewOptionalResult(this CompilationContext _, Expression hasValue, Expression value) => Expression.New(GetOptionalResult_Constructor(), [hasValue, value]); public static Expression NewTextSpan(this CompilationContext _, Expression buffer, Expression offset, Expression count) => Expression.New(TextSpan_Constructor, [buffer, offset, count]); diff --git a/src/Parlot/Fluent/CharLiteral.cs b/src/Parlot/Fluent/CharLiteral.cs index 5f22ac8..4f05399 100644 --- a/src/Parlot/Fluent/CharLiteral.cs +++ b/src/Parlot/Fluent/CharLiteral.cs @@ -18,7 +18,7 @@ public CharLiteral(char c) public char[] ExpectedChars { get; } - public bool SkipWhitespace { get; } = false; + public bool SkipWhitespace { get; } public override bool Parse(ParseContext context, ref ParseResult result) { diff --git a/src/Parlot/Fluent/Deferred.cs b/src/Parlot/Fluent/Deferred.cs index ee6a7b7..b5e387b 100644 --- a/src/Parlot/Fluent/Deferred.cs +++ b/src/Parlot/Fluent/Deferred.cs @@ -22,16 +22,16 @@ public override bool Parse(ParseContext context, ref ParseResult result) { if (Parser is null) { - throw new ArgumentNullException(nameof(Parser)); + throw new InvalidOperationException("Parser has not been initialized"); } return Parser.Parse(context, ref result); } - private bool _initialized = false; + private bool _initialized; private readonly Closure _closure = new(); - private class Closure + private sealed class Closure { public object? Func; } diff --git a/src/Parlot/Fluent/NumberLiteral.cs b/src/Parlot/Fluent/NumberLiteral.cs index 9b21990..0bc9389 100644 --- a/src/Parlot/Fluent/NumberLiteral.cs +++ b/src/Parlot/Fluent/NumberLiteral.cs @@ -30,7 +30,7 @@ public sealed class NumberLiteral : Parser, ICompilable, ISeekable public char[] ExpectedChars { get; } - public bool SkipWhitespace { get; } = false; + public bool SkipWhitespace { get; } public NumberLiteral(NumberOptions numberOptions = NumberOptions.Number, char decimalSeparator = DefaultDecimalSeparator, char groupSeparator = DefaultGroupSeparator) { diff --git a/src/Parlot/Fluent/OneOf.cs b/src/Parlot/Fluent/OneOf.cs index a1040f9..e02bf3e 100644 --- a/src/Parlot/Fluent/OneOf.cs +++ b/src/Parlot/Fluent/OneOf.cs @@ -14,7 +14,7 @@ namespace Parlot.Fluent /// public sealed class OneOf : Parser, ICompilable, ISeekable { - internal readonly Parser[] _parsers; + private readonly Parser[] _parsers; internal readonly CharMap>>? _map; public OneOf(Parser[] parsers) diff --git a/src/Parlot/Fluent/ParseContext.cs b/src/Parlot/Fluent/ParseContext.cs index 00122fa..47de5c5 100644 --- a/src/Parlot/Fluent/ParseContext.cs +++ b/src/Parlot/Fluent/ParseContext.cs @@ -4,7 +4,9 @@ namespace Parlot.Fluent { public class ParseContext { - public static int DefaultCompilationThreshold = 0; +#pragma warning disable CA2211 // Non-constant fields should not be visible + public static int DefaultCompilationThreshold; +#pragma warning restore CA2211 /// /// The number of usages of the parser before it is compiled automatically. 0 to disable automatic compilation. Default is 0. diff --git a/src/Parlot/Fluent/Parser.TryParse.cs b/src/Parlot/Fluent/Parser.TryParse.cs index 502a294..9c76d60 100644 --- a/src/Parlot/Fluent/Parser.TryParse.cs +++ b/src/Parlot/Fluent/Parser.TryParse.cs @@ -4,7 +4,7 @@ public abstract partial class Parser { - private int _invocations = 0; + private int _invocations; private volatile Parser? _compiledParser; public T? Parse(string text) diff --git a/src/Parlot/Fluent/Parsers.cs b/src/Parlot/Fluent/Parsers.cs index cfe73ee..7d88440 100644 --- a/src/Parlot/Fluent/Parsers.cs +++ b/src/Parlot/Fluent/Parsers.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Numerics; +#pragma warning disable CA1822 // Mark members as static + namespace Parlot.Fluent { public static partial class Parsers diff --git a/src/Parlot/Fluent/Sequence.cs b/src/Parlot/Fluent/Sequence.cs index deca64e..c1dfc32 100644 --- a/src/Parlot/Fluent/Sequence.cs +++ b/src/Parlot/Fluent/Sequence.cs @@ -7,8 +7,8 @@ namespace Parlot.Fluent { public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { - internal readonly Parser _parser1; - internal readonly Parser _parser2; + private readonly Parser _parser1; + private readonly Parser _parser2; public Sequence(Parser parser1, Parser parser2) { _parser1 = parser1 ?? throw new ArgumentNullException(nameof(parser1)); @@ -70,7 +70,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public Sequence(Parser> parser, @@ -143,7 +143,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public Sequence(Parser> parser, Parser lastParser) { @@ -214,7 +214,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public Sequence(Parser> parser, Parser lastParser) { @@ -286,7 +286,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public Sequence(Parser> parser, Parser lastParser) { @@ -360,7 +360,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class Sequence : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public Sequence(Parser> parser, Parser lastParser) { diff --git a/src/Parlot/Fluent/SequenceAndSkip.cs b/src/Parlot/Fluent/SequenceAndSkip.cs index 55423d7..2238455 100644 --- a/src/Parlot/Fluent/SequenceAndSkip.cs +++ b/src/Parlot/Fluent/SequenceAndSkip.cs @@ -8,8 +8,8 @@ namespace Parlot.Fluent { public sealed class SequenceAndSkip : Parser, ICompilable, ISkippableSequenceParser, ISeekable { - internal readonly Parser _parser1; - internal readonly Parser _parser2; + private readonly Parser _parser1; + private readonly Parser _parser2; public SequenceAndSkip(Parser parser1, Parser parser2) { @@ -129,7 +129,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, @@ -201,7 +201,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, Parser lastParser) { @@ -271,7 +271,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, Parser lastParser) { @@ -342,7 +342,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, Parser lastParser) { @@ -416,7 +416,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, Parser lastParser) { @@ -491,7 +491,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceAndSkip : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceAndSkip(Parser> parser, Parser lastParser) { diff --git a/src/Parlot/Fluent/SequenceSkipAnd.cs b/src/Parlot/Fluent/SequenceSkipAnd.cs index f2d1135..a4e1de7 100644 --- a/src/Parlot/Fluent/SequenceSkipAnd.cs +++ b/src/Parlot/Fluent/SequenceSkipAnd.cs @@ -8,8 +8,8 @@ namespace Parlot.Fluent { public sealed class SequenceSkipAnd : Parser, ICompilable, ISkippableSequenceParser, ISeekable { - internal readonly Parser _parser1; - internal readonly Parser _parser2; + private readonly Parser _parser1; + private readonly Parser _parser2; public SequenceSkipAnd(Parser parser1, Parser parser2) { @@ -129,7 +129,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, @@ -204,7 +204,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, Parser lastParser) { @@ -277,7 +277,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, Parser lastParser) { @@ -351,7 +351,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, Parser lastParser) { @@ -427,7 +427,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, Parser lastParser) { @@ -504,7 +504,7 @@ public CompilationResult Compile(CompilationContext context) public sealed class SequenceSkipAnd : Parser>, ICompilable, ISkippableSequenceParser, ISeekable { private readonly Parser> _parser; - internal readonly Parser _lastParser; + private readonly Parser _lastParser; public SequenceSkipAnd(Parser> parser, Parser lastParser) { diff --git a/src/Parlot/Fluent/StringLiteral.cs b/src/Parlot/Fluent/StringLiteral.cs index 84e0522..4e4dcd2 100644 --- a/src/Parlot/Fluent/StringLiteral.cs +++ b/src/Parlot/Fluent/StringLiteral.cs @@ -40,7 +40,7 @@ public StringLiteral(StringLiteralQuotes quotes) public char[] ExpectedChars { get; } - public bool SkipWhitespace { get; } = false; + public bool SkipWhitespace { get; } public override bool Parse(ParseContext context, ref ParseResult result) { diff --git a/src/Parlot/Fluent/TextLiteral.cs b/src/Parlot/Fluent/TextLiteral.cs index b3236b4..dd4518a 100644 --- a/src/Parlot/Fluent/TextLiteral.cs +++ b/src/Parlot/Fluent/TextLiteral.cs @@ -6,6 +6,8 @@ namespace Parlot.Fluent { + using System.Globalization; + public sealed class TextLiteral : Parser, ICompilable, ISeekable { private readonly StringComparison _comparisonType; @@ -40,7 +42,7 @@ public TextLiteral(string text, StringComparison comparisonType) } else { - ExpectedChars = ignoreCase ? [Text.ToUpper()[0], Text.ToLower()[0]] : [Text[0]]; + ExpectedChars = ignoreCase ? [Text.ToUpper(CultureInfo.CurrentCulture)[0], Text.ToLower(CultureInfo.CurrentCulture)[0]] : [Text[0]]; } } } @@ -51,7 +53,7 @@ public TextLiteral(string text, StringComparison comparisonType) public char[] ExpectedChars { get; } = []; - public bool SkipWhitespace { get; } = false; + public bool SkipWhitespace { get; } public override bool Parse(ParseContext context, ref ParseResult result) { diff --git a/src/Parlot/Fluent/Then.cs b/src/Parlot/Fluent/Then.cs index 97bd94c..71a22d3 100644 --- a/src/Parlot/Fluent/Then.cs +++ b/src/Parlot/Fluent/Then.cs @@ -16,7 +16,7 @@ public sealed class Then : Parser, ICompilable, ISeekable { private readonly Func? _action1; private readonly Func? _action2; - private readonly U? _value = default; + private readonly U? _value; private readonly Parser _parser; private Then(Parser parser) diff --git a/test/Parlot.Benchmarks/Parlot.Benchmarks.csproj b/test/Parlot.Benchmarks/Parlot.Benchmarks.csproj index a6ac86a..9e96f75 100644 --- a/test/Parlot.Benchmarks/Parlot.Benchmarks.csproj +++ b/test/Parlot.Benchmarks/Parlot.Benchmarks.csproj @@ -4,6 +4,7 @@ Exe net6.0;net8.0 false + latest-Default diff --git a/test/Parlot.Tests/Parlot.Tests.csproj b/test/Parlot.Tests/Parlot.Tests.csproj index 6b39515..a4b1ff7 100644 --- a/test/Parlot.Tests/Parlot.Tests.csproj +++ b/test/Parlot.Tests/Parlot.Tests.csproj @@ -3,7 +3,7 @@ net6.0;net8.0 false - latest + latest-Default