You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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));
Expected behavior
I didn't expect any diagnostics to be emitted.
Actual behavior
False positive.
Known workarounds
#pragma warning disable S3267
Related information
SonarCloud.io
JetBrains Rider 2024.1.1
dotnet 8.0.403
SonarScanner for .NET version 2.3.2
RedHat
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: