Reference

Trax.Analyzers

Source Trax.Analyzers.Package/, rules.txt

A Roslyn analyzer package for code that builds on Trax .NET, or any other .NET code. It checks for culture and comparer bugs, async and cancellation mistakes, regexes without a timeout, the null-forgiving operator and about 250 other things, and it bans reading the clock, chance or the console directly. Every rule it turns on is an error.

Nothing applies until a project references it. Trax builds all of its own code, samples and the book's project under it (why).

PackageTrax.Analyzers, a development dependency targeting netstandard2.0
Rule idsTRX0001 to TRX0245, plus RS0030 for the bans
Rule pagesTrax.Analyzers/docs/rules/, one per rule
The profileTrax.Analyzers/rules.txt: every rule, with its severity in source and test projects

Turn it on

In each project, or once in Directory.Build.props:

<ItemGroup>
  <PackageReference Include="Trax.Analyzers" PrivateAssets="all" />
</ItemGroup>

PrivateAssets="all" keeps it out of whatever depends on your project. A project made with dotnet new trax-api, trax-scheduler or trax-hub gets the same line with --strict (Project Templates).

The rules apply to test projects too. Expect the first build of an existing codebase to fail in many places; dotnet format analyzers <solution> --diagnostics TRX0004 --severity error applies the automatic fixes for one rule at a time. Read what it changed: a fix can change behaviour, for example RegexOptions.ExplicitCapture on a regex whose groups are read by number.

What it reports

  • The rules, TRX0001 and up. They are a copy of Meziantou.Analyzer (MIT), renamed into Trax's ids, so MA0004 is TRX0004. Every rule that project reports by default is an error here, and so are a few it leaves off, marked added by Trax in rules.txt: TRX0045, for example, reports a blocking call (File.ReadAllText, .Result, Cancel() beside CancelAsync()) even where the method has to become async to avoid it.
  • The bans, through RS0030 from Microsoft.CodeAnalysis.BannedApiAnalyzers, which the package brings with it:
GroupBansUse instead
ClockDateTime.Now, UtcNow, Today, the DateTimeOffset forms, Stopwatch, Environment.TickCountAn injected TimeProvider; TimeProvider.System where the host registers the real clock
RandomRandom.Shared, new Random() without a seedAn injected Random, or a seeded one in a test
ConsoleSystem.ConsoleILogger, or the writers a command is given
Process exitEnvironment.Exit, Environment.FailFastReturn an exit code, or throw
Null-forgivingx! (TRX0240), null! and default! (TRX0191)A real value, required, a null test, or a method that throws when the value is null

Turn part of it off

Each group has an MSBuild property. Set it to false in the project or Directory.Build.props:

Propertyfalse turns off
TraxAnalyzersthe whole profile; the rules stay loaded at the severities they ship with, which is mostly off
TraxAnalyzersTeststhe profile and the bans in test projects (IsTestProject)
TraxBanClockthe clock ban
TraxBanRandomthe random ban
TraxBanConsolethe console ban
TraxBanProcessExitthe process exit ban
TraxBanNullForgivingthe ! operator; null! and default! are rule TRX0191, turned off like any rule

One rule is turned off or down in .editorconfig. The profile is a global configuration at the lowest priority, so any severity you write wins:

[*.cs]
dotnet_diagnostic.TRX0048.severity = none

The package also adds the nearest BannedSyntaxes.txt above a project to the null-forgiving ban, so a repository can ban more syntax of its own. TraxIncludeBannedSyntaxesFile=false stops that.

Pin the level

TraxAnalysisLevel picks the profile. A release that would add errors to an existing build ships them under a new level, and a project gets the newest level unless it pins one:

<PropertyGroup>
  <TraxAnalysisLevel>1</TraxAnalysisLevel>
</PropertyGroup>

latest, or leaving it unset, takes the newest level the installed package has.

Trax.Analyzers.Annotations

Attributes that tune a rule from your own code: [RequireNamedArgument] on a parameter whose arguments must be named, [CultureInsensitiveType] to declare a type's formatting free of culture, [ExcludeFromBlockingCallAnalysis] to name a method TRX0042 and TRX0045 leave alone, and the others in the Trax.Analyzers.Annotations namespace. They are conditional and leave no reference in the compiled assembly.

<PackageReference Include="Trax.Analyzers.Annotations" PrivateAssets="all" />

SDK Reference

Packages | Project Templates | Acknowledgments