Open
Description
There is an unnecessary check generated when slicing a span with s.Slice(0, s.Length - 1)
.
Repro:
public static int Test(ReadOnlySpan<char> s)
{
while (s.Length != 0)
{
if (s[^1] != ' ')
break;
s = s.Slice(0, s.Length - 1); // unnecessary check generated
}
return s.Length;
}
Sample:Test(System.ReadOnlySpan`1[ushort]):int (FullOpts):
push rbp
mov rbp, rsp
G_M44937_IG02: ;; offset=0x0004
test esi, esi
je SHORT G_M44937_IG04
lea eax, [rsi-0x01]
mov ecx, eax
cmp word ptr [rdi+2*rcx], 32
jne SHORT G_M44937_IG04
cmp eax, esi ; unnecessary check
ja SHORT G_M44937_IG06 ;
mov esi, eax
jmp SHORT G_M44937_IG02
G_M44937_IG04: ;; offset=0x001C
mov eax, esi
pop rbp
ret
G_M44937_IG06: ;; offset=0x0020
call [System.ThrowHelper:ThrowArgumentOutOfRangeException()]
int3