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
Somewhat more generally, any time string.Substring is used as an argument to something where there's an equivalent overload that takes a ReadOnlySpan<char> (e.g. StringBuilder.Append(string) vs StringBuilder.Append(ReadOnlySpan<char>)), the case can be flagged to be changed to use AsSpan instead.
We implemented a very similar analyzer for Stream.ReadAsync and Stream.WriteAsync that suggested preferring the memory-based overloads. The logic can be very similar here so whoever works on this can use that analyzer for reference: dotnet/roslyn-analyzers#3592
staticvoidMain(){intn=0;strings="My string";// Substring overload with 1 argument// BeforeMyMethod(s.Substring(1));// AfterMyMethod(s.AsSpan(1));// Substring overload with 2 arguments// BeforeMyMethod(s.Substring(1,3));// AfterMyMethod(s.AsSpan(1,3));// Extra arguments, find overload with same types except string->ROS// BeforeMyMethod(n,s.Substring(1));// AfterMyMethod(n,s.AsSpan(1));// Named arguments, match overload with string->ROS in same position// BeforeMyMethod(s:s.Substring(1),n:n);// AfterMyMethod(ros:s.AsSpan(1),n:n);// Named arguments, multiple strings, match overload with strings->ROS in same positions// BeforeMyMethod(s2:s.Substring(1),n:n,s1:s.Substring(2));// AfterMyMethod(ros2:s.AsSpan(1),n:n,ros1:s.Substring(2));}staticvoidMyMethod(strings);staticvoidMyMethod(ReadOnlySpan<char>ros);staticvoidMyMethod(intn,strings);staticvoidMyMethod(intn,ReadOnlySpan<char>ros);staticvoidMyMethod(intn,strings1,strings2);staticvoidMyMethod(intn,ReadOnlySpan<char>ros1,ReadOnlySpan<char>ros2);
I don't think any purely perf-focused rules like this should break a build (which warning can do if warning-as-error is enabled). It should likely be Info.
Reacted by Carlos Sánchez López, Levi Broderick and Konstantin Safonov
@terrajobst I just noticed this issue is almost identical to #33784.
The only difference is that the other issue is specifically targeted to substrings passed as arguments to Parse methods of primitive types, while this issue is specifically targeted to substrings passed as arguments to any methods that take a string.
Is there a reason I'm missing for which there are two separate issues? Couldn't we have one analyzer target all methods?
Note: The other issue was approved back in November, and is already assigned to someone working on it. So if we have to close one, it should be this one.
Activity
jeffhandley commentedon Mar 21, 2020
Estimates:
carlossanlop commentedon Jan 15, 2021
Suggested severity: Info
We implemented a very similar analyzer for Stream.ReadAsync and Stream.WriteAsync that suggested preferring the memory-based overloads. The logic can be very similar here so whoever works on this can use that analyzer for reference: dotnet/roslyn-analyzers#3592
stephentoub commentedon Jan 15, 2021
I don't think any purely perf-focused rules like this should break a build (which warning can do if warning-as-error is enabled). It should likely be Info.
terrajobst commentedon Feb 2, 2021
Video
carlossanlop commentedon Feb 5, 2021
@terrajobst I just noticed this issue is almost identical to #33784.
The only difference is that the other issue is specifically targeted to substrings passed as arguments to
Parsemethods of primitive types, while this issue is specifically targeted to substrings passed as arguments to any methods that take a string.Is there a reason I'm missing for which there are two separate issues? Couldn't we have one analyzer target all methods?
Note: The other issue was approved back in November, and is already assigned to someone working on it. So if we have to close one, it should be this one.
carlossanlop commentedon Feb 5, 2021
Talked to @terrajobst about this. This is a duplicate approval of #33784. Since the other one was approved first, I'll keep that one open.