Cette page n'est pas encore traduite. La version anglaise est affichée ci-dessous.
FDB1005: Missing key tested with Slice.Empty
| Analyzer | FoundationDB.Client |
| Severity | Warning (correctness) |
| Code fix | Yes |
Message
A missing key reads as Slice.Nil, which is not equal to Slice.Empty. Test it with IsNull, or IsNullOrEmpty to also accept an empty value.
Why
GetAsync returns Slice.Nil when the key does not exist, and Slice.Empty only when the key exists with a zero-length value. Slice.Nil is not equal to Slice.Empty, and Slice.Nil.IsEmpty is false. A missing-key test written with == Slice.Empty or .IsEmpty never matches, so the code takes the "present" branch on a missing key and reads garbage, or skips the "absent" branch that creates the record.
The rule reports a value read with await tr.GetAsync(...), directly or through a local, when it is compared with Slice.Empty (== or !=) or tested with .IsEmpty as the whole condition of an if, a while, or a conditional expression (with or without !).
Bad
var v = await tr.GetAsync(key);
if (v == Slice.Empty) return null;
Good
var v = await tr.GetAsync(key);
if (v.IsNull) return null;
Code fix
The fix rewrites v == Slice.Empty and v.IsEmpty to v.IsNull, and v != Slice.Empty to !v.IsNull. Code that must also treat an empty value as absent uses IsNullOrEmpty by hand.
Suppress
#pragma warning disable FDB1005 // reason
or, for a whole project, in .editorconfig:
dotnet_diagnostic.FDB1005.severity = none