Skip to content

Commit

Permalink
Enable latest-Recommended analysis rules (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored Jul 23, 2024
1 parent 902b805 commit fab5f77
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 41 deletions.
18 changes: 18 additions & 0 deletions src/Directory.Build.props → Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,22 @@

</PropertyGroup>

<PropertyGroup Label="Analysis rules">

<AnalysisLevel>latest-Recommended</AnalysisLevel>

<!-- Prefix generic type parameter name with 'T' -->
<NoWarn>$(NoWarn);CA1715;</NoWarn>

<!-- Do not declare visible instance fields -->
<NoWarn>$(NoWarn);CA1051;</NoWarn>

<!-- Identifier contains type name -->
<NoWarn>$(NoWarn);CA1720;</NoWarn>

<!-- Identifiers should not match keywords -->
<NoWarn>$(NoWarn);CA1716;</NoWarn>

</PropertyGroup>

</Project>
1 change: 1 addition & 0 deletions Parlot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
4 changes: 2 additions & 2 deletions src/Parlot/Compilation/CompilationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Parlot.Compilation
/// </summary>
public class CompilationContext
{
private int _number = 0;
private int _number;

public CompilationContext()
{
Expand Down Expand Up @@ -51,7 +51,7 @@ public CompilationContext()
/// When set to false, the compiled statements don't need to record and define the <see cref="CompilationResult.Value"/> property.
/// This is done to optimize compiled parser that are usually used for pattern matching only.
/// </remarks>
public bool DiscardResult { get; set; } = false;
public bool DiscardResult { get; set; }

/// <summary>
/// Creates a <see cref="CompilationResult"/> instance with a <see cref="CompilationResult.Value"/> and <see cref="CompilationResult.Success"/>
Expand Down
6 changes: 3 additions & 3 deletions src/Parlot/Compilation/ExpressionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>() => ((Expression<Func<object>>)(() => Array.Empty<T>())).Body;
public static Expression New<T>() where T : new() => ((Expression<Func<T>>)(() => new T())).Body;

public static Expression<Func<Cursor, char, char, bool>> CharacterIsInRange = (cursor,b,c) => Character.IsInRange(cursor.Current, b, c);
public static readonly Expression<Func<Cursor, char, char, bool>> CharacterIsInRange = (cursor,b,c) => Character.IsInRange(cursor.Current, b, c);

//public static Expression NewOptionalResult<T>(this CompilationContext _, Expression hasValue, Expression value) => Expression.New(GetOptionalResult_Constructor<T>(), [hasValue, value]);
public static Expression NewTextSpan(this CompilationContext _, Expression buffer, Expression offset, Expression count) => Expression.New(TextSpan_Constructor, [buffer, offset, count]);
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/CharLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> result)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Parlot/Fluent/Deferred.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ public override bool Parse(ParseContext context, ref ParseResult<T> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/NumberLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public sealed class NumberLiteral<T> : Parser<T>, 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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/OneOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Parlot.Fluent
/// <typeparam name="T"></typeparam>
public sealed class OneOf<T> : Parser<T>, ICompilable, ISeekable
{
internal readonly Parser<T>[] _parsers;
private readonly Parser<T>[] _parsers;
internal readonly CharMap<List<Parser<T>>>? _map;

public OneOf(Parser<T>[] parsers)
Expand Down
4 changes: 3 additions & 1 deletion src/Parlot/Fluent/ParseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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

/// <summary>
/// The number of usages of the parser before it is compiled automatically. <c>0</c> to disable automatic compilation. Default is 0.
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/Parser.TryParse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public abstract partial class Parser<T>
{
private int _invocations = 0;
private int _invocations;
private volatile Parser<T>? _compiledParser;

public T? Parse(string text)
Expand Down
2 changes: 2 additions & 0 deletions src/Parlot/Fluent/Parsers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/Parlot/Fluent/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Parlot.Fluent
{
public sealed class Sequence<T1, T2> : Parser<ValueTuple<T1, T2>>, ICompilable, ISkippableSequenceParser, ISeekable
{
internal readonly Parser<T1> _parser1;
internal readonly Parser<T2> _parser2;
private readonly Parser<T1> _parser1;
private readonly Parser<T2> _parser2;
public Sequence(Parser<T1> parser1, Parser<T2> parser2)
{
_parser1 = parser1 ?? throw new ArgumentNullException(nameof(parser1));
Expand Down Expand Up @@ -70,7 +70,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class Sequence<T1, T2, T3> : Parser<ValueTuple<T1, T2, T3>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2>> _parser;
internal readonly Parser<T3> _lastParser;
private readonly Parser<T3> _lastParser;

public Sequence(Parser<ValueTuple<T1, T2>>
parser,
Expand Down Expand Up @@ -143,7 +143,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class Sequence<T1, T2, T3, T4> : Parser<ValueTuple<T1, T2, T3, T4>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3>> _parser;
internal readonly Parser<T4> _lastParser;
private readonly Parser<T4> _lastParser;

public Sequence(Parser<ValueTuple<T1, T2, T3>> parser, Parser<T4> lastParser)
{
Expand Down Expand Up @@ -214,7 +214,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class Sequence<T1, T2, T3, T4, T5> : Parser<ValueTuple<T1, T2, T3, T4, T5>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4>> _parser;
internal readonly Parser<T5> _lastParser;
private readonly Parser<T5> _lastParser;

public Sequence(Parser<ValueTuple<T1, T2, T3, T4>> parser, Parser<T5> lastParser)
{
Expand Down Expand Up @@ -286,7 +286,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class Sequence<T1, T2, T3, T4, T5, T6> : Parser<ValueTuple<T1, T2, T3, T4, T5, T6>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5>> _parser;
internal readonly Parser<T6> _lastParser;
private readonly Parser<T6> _lastParser;

public Sequence(Parser<ValueTuple<T1, T2, T3, T4, T5>> parser, Parser<T6> lastParser)
{
Expand Down Expand Up @@ -360,7 +360,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class Sequence<T1, T2, T3, T4, T5, T6, T7> : Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> _parser;
internal readonly Parser<T7> _lastParser;
private readonly Parser<T7> _lastParser;

public Sequence(Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> parser, Parser<T7> lastParser)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/SequenceAndSkip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Parlot.Fluent
{
public sealed class SequenceAndSkip<T1, T2> : Parser<T1>, ICompilable, ISkippableSequenceParser, ISeekable
{
internal readonly Parser<T1> _parser1;
internal readonly Parser<T2> _parser2;
private readonly Parser<T1> _parser1;
private readonly Parser<T2> _parser2;

public SequenceAndSkip(Parser<T1> parser1, Parser<T2> parser2)
{
Expand Down Expand Up @@ -129,7 +129,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3> : Parser<ValueTuple<T1, T2>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2>> _parser;
internal readonly Parser<T3> _lastParser;
private readonly Parser<T3> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2>>
parser,
Expand Down Expand Up @@ -201,7 +201,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3, T4> : Parser<ValueTuple<T1, T2, T3>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3>> _parser;
internal readonly Parser<T4> _lastParser;
private readonly Parser<T4> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2, T3>> parser, Parser<T4> lastParser)
{
Expand Down Expand Up @@ -271,7 +271,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3, T4, T5> : Parser<ValueTuple<T1, T2, T3, T4>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4>> _parser;
internal readonly Parser<T5> _lastParser;
private readonly Parser<T5> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2, T3, T4>> parser, Parser<T5> lastParser)
{
Expand Down Expand Up @@ -342,7 +342,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3, T4, T5, T6> : Parser<ValueTuple<T1, T2, T3, T4, T5>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5>> _parser;
internal readonly Parser<T6> _lastParser;
private readonly Parser<T6> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2, T3, T4, T5>> parser, Parser<T6> lastParser)
{
Expand Down Expand Up @@ -416,7 +416,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3, T4, T5, T6, T7> : Parser<ValueTuple<T1, T2, T3, T4, T5, T6>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> _parser;
internal readonly Parser<T7> _lastParser;
private readonly Parser<T7> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> parser, Parser<T7> lastParser)
{
Expand Down Expand Up @@ -491,7 +491,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceAndSkip<T1, T2, T3, T4, T5, T6, T7, T8> : Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> _parser;
internal readonly Parser<T8> _lastParser;
private readonly Parser<T8> _lastParser;

public SequenceAndSkip(Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> parser, Parser<T8> lastParser)
{
Expand Down
16 changes: 8 additions & 8 deletions src/Parlot/Fluent/SequenceSkipAnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ namespace Parlot.Fluent
{
public sealed class SequenceSkipAnd<T1, T2> : Parser<T2>, ICompilable, ISkippableSequenceParser, ISeekable
{
internal readonly Parser<T1> _parser1;
internal readonly Parser<T2> _parser2;
private readonly Parser<T1> _parser1;
private readonly Parser<T2> _parser2;

public SequenceSkipAnd(Parser<T1> parser1, Parser<T2> parser2)
{
Expand Down Expand Up @@ -129,7 +129,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3> : Parser<ValueTuple<T1, T3>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2>> _parser;
internal readonly Parser<T3> _lastParser;
private readonly Parser<T3> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2>>
parser,
Expand Down Expand Up @@ -204,7 +204,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3, T4> : Parser<ValueTuple<T1, T2, T4>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3>> _parser;
internal readonly Parser<T4> _lastParser;
private readonly Parser<T4> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2, T3>> parser, Parser<T4> lastParser)
{
Expand Down Expand Up @@ -277,7 +277,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3, T4, T5> : Parser<ValueTuple<T1, T2, T3, T5>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4>> _parser;
internal readonly Parser<T5> _lastParser;
private readonly Parser<T5> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2, T3, T4>> parser, Parser<T5> lastParser)
{
Expand Down Expand Up @@ -351,7 +351,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3, T4, T5, T6> : Parser<ValueTuple<T1, T2, T3, T4, T6>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5>> _parser;
internal readonly Parser<T6> _lastParser;
private readonly Parser<T6> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2, T3, T4, T5>> parser, Parser<T6> lastParser)
{
Expand Down Expand Up @@ -427,7 +427,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3, T4, T5, T6, T7> : Parser<ValueTuple<T1, T2, T3, T4, T5, T7>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> _parser;
internal readonly Parser<T7> _lastParser;
private readonly Parser<T7> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2, T3, T4, T5, T6>> parser, Parser<T7> lastParser)
{
Expand Down Expand Up @@ -504,7 +504,7 @@ public CompilationResult Compile(CompilationContext context)
public sealed class SequenceSkipAnd<T1, T2, T3, T4, T5, T6, T7, T8> : Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T8>>, ICompilable, ISkippableSequenceParser, ISeekable
{
private readonly Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> _parser;
internal readonly Parser<T8> _lastParser;
private readonly Parser<T8> _lastParser;

public SequenceSkipAnd(Parser<ValueTuple<T1, T2, T3, T4, T5, T6, T7>> parser, Parser<T8> lastParser)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/StringLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TextSpan> result)
{
Expand Down
6 changes: 4 additions & 2 deletions src/Parlot/Fluent/TextLiteral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace Parlot.Fluent
{
using System.Globalization;

public sealed class TextLiteral : Parser<string>, ICompilable, ISeekable
{
private readonly StringComparison _comparisonType;
Expand Down Expand Up @@ -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]];
}
}
}
Expand All @@ -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<string> result)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Parlot/Fluent/Then.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public sealed class Then<T, U> : Parser<U>, ICompilable, ISeekable
{
private readonly Func<T, U>? _action1;
private readonly Func<ParseContext, T, U>? _action2;
private readonly U? _value = default;
private readonly U? _value;
private readonly Parser<T> _parser;

private Then(Parser<T> parser)
Expand Down
1 change: 1 addition & 0 deletions test/Parlot.Benchmarks/Parlot.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<AnalysisLevel>latest-Default</AnalysisLevel>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion test/Parlot.Tests/Parlot.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<AnalysisLevel>latest-Default</AnalysisLevel>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit fab5f77

Please sign in to comment.