SYSLIB diagnostics for regex source generation

The following table shows the diagnostic IDs for regex source-generation analyzers in .NET 7 and later versions. SYSLIB1045 automatically alerts you to places that you can use source generation to generate the regular expression engine implementation at compile time. The remaining diagnostics alert you to errors related to usage of the source generator.

Diagnostic ID Description
SYSLIB1040 Invalid GeneratedRegexAttribute usage.
SYSLIB1041 Multiple GeneratedRegexAttribute attributes were applied to the same method, but only one is allowed.
SYSLIB1042 The specified regular expression is invalid.
SYSLIB1043 A GeneratedRegexAttribute method must be partial, parameterless, non-generic, and non-abstract, and return Regex.
SYSLIB1044 The regex generator couldn't generate a complete source implementation for the specified regular expression due to an internal limitation. See the explanation in the generated source for more details.
SYSLIB1045 Use GeneratedRegexAttribute to generate the regular expression implementation at compile time.

For more information about source generation for regular expressions, see .NET regular expression source generators.

Suppress warnings

It's recommended that you use one of the workarounds when possible. However, if you cannot change your code, you can suppress the warning through a #pragma directive or a <NoWarn> project setting. If the SYSLIB1XXX source generator diagnostic doesn't surface as an error, you can suppress the warning in code or in your project file.

To suppress the warnings in code:

// Disable the warning.
#pragma warning disable SYSLIB1006

// Code that generates compiler diagnostic.
// ...

// Re-enable the warning.
#pragma warning restore SYSLIB1006

To suppress the warnings in a project file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
   <TargetFramework>net6.0</TargetFramework>
   <!-- NoWarn below suppresses SYSLIB1002 project-wide -->
   <NoWarn>$(NoWarn);SYSLIB1002</NoWarn>
   <!-- To suppress multiple warnings, you can use multiple NoWarn elements -->
   <NoWarn>$(NoWarn);SYSLIB1002</NoWarn>
   <NoWarn>$(NoWarn);SYSLIB1006</NoWarn>
   <!-- Alternatively, you can suppress multiple warnings by using a semicolon-delimited list -->
   <NoWarn>$(NoWarn);SYSLIB1002;SYSLIB1006;SYSLIB1007</NoWarn>
  </PropertyGroup>
</Project>

See also