SBK0001: Slice.FromStringAscii on text that cannot be encoded

Analyzer SnowBank.Core
Severity Error
Code fix Yes

Message

'{0}' contains characters above 0xFF, which this method rejects at run time. Use Slice.FromStringUtf8.

Why

Slice.FromStringAscii writes one byte per character and checks that every character fits in a byte (0 to 0xFF). A character above 0xFF makes the call throw FormatException, so a literal with such a character fails every time the line runs.

The rule reports a Slice.FromStringAscii call whose argument is a compile-time constant (a literal, a const, or a concatenation of them) with a character above 0xFF. Text known only at run time is not reported.

Bad

Slice key = Slice.FromStringAscii("café 中");

Good

Slice key = Slice.FromStringUtf8("café 中");

Slice.FromStringUtf8 encodes every character. Characters between 0x80 and 0xFF stay accepted by Slice.FromStringAscii: they round-trip through ToByteString(), not through ToStringUtf8().

Code fix

The fix renames FromStringAscii to FromStringUtf8.

Suppress

#pragma warning disable SBK0001 // reason

or, for a whole project, in .editorconfig:

dotnet_diagnostic.SBK0001.severity = none