.Value is read from a nullable value that the method never tests for a value
What it finds
x.Value on an empty int? throws InvalidOperationException. A method that reads it without ever looking at HasValue, comparing x with null, using ??, GetValueOrDefault, a pattern on x or any other use of x that might test it, relies on the value always being there, which is what the type says it may not be. The rule looks at variables declared as nullable built-in value types, DateTime?, TimeSpan? and Guid?.
From the rule's source, nullable_value_unchecked.rs.
using System; class Orders{ int Total(int? count) => count.Value * 2;tx0 reports: count.Value is read without testing count for a value int Safe(int? count) => count.HasValue ? count.Value : 0;} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:nullable-value-unchecked" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:nullable-value-unchecked] reason="..."tx0 explain tx0/csharp:nullable-value-unchecked shows the same in the terminal. Found a false positive? Report it.