S3267
Some suggestions to simplify the code by rewriting it using LINQ cannot be applied when ref struct types are involved.
Repro steps
ReadOnlySpan<char> stringToCompare = "example".AsSpan();
string[] stringValues = ["example", "another string"];
var isMatchFound = false;
// S3267: loop should be simplified to LINQ
foreach (var value in stringValues)
{
if (value.AsSpan().Equals(stringToCompare, StringComparison.Ordinal))
{
isMatchFound = true;
break;
}
}
ReadOnlySpan is a ref struct and can not be used inside lambda expressions.
// Cannot use local variable 'stringToCompare' of byref-like type 'ReadOnlySpan<char>' inside lambda expression;
ReadOnlySpan<char> stringToCompare = "example".AsSpan();
string[] stringValues = ["example", "another string"];
var result = stringValues.Any(value => value .AsSpan().Equals(stringToCompare, StringComparison.Ordinal));
Description
S3267 Some suggestions to simplify the code by rewriting it using LINQ cannot be applied when ref struct types are involved.
Repro steps
ReadOnlySpan is a ref struct and can not be used inside lambda expressions.
Expected behavior
I didn't expect any diagnostics to be emitted.
Actual behavior
False positive.
Known workarounds
Related information