Description
We should consider adding an analyzer that recommends using the char-based StartsWith(char) instead of StartsWith(string, ...) when the string is a constant single character. The former is more optimized.
However, this can have observable behavioral differences. string.StartsWith(char) is ordinal, whereas string.StartsWith(string) uses the current culture. This, for example, prints True and then False, highlighting that there can be semantic differences due to culture:
using System.Globalization;
CultureInfo.CurrentCulture = new CultureInfo("en-US");
Console.WriteLine("\u0061\u030a".StartsWith("\u00e5"));
Console.WriteLine("\u0061\u030a".StartsWith('\u00e5'));It would be safe to replace string.StartsWith("c", StringComparison.Ordinal) with string.StartsWith('c') for arbitrary characters. We could do that with one diagnostic ID, and have a separate diagnostic ID (potentially off by default) for the potentially problematic case.
Same for EndsWith, IndexOf, and LastIndexOf.
Proposal to have 2 analyzers with Performance rules Category
For the case when passing StringComparison.Ordinal
For the case when not passing StringComparison parameter
Severity = none (off by default)
Activity
tarekgh commentedon Nov 15, 2022
I support doing the safer version when using Ordinal option. For other cases, I think we have/should have another analyzer checked if using the API without intentionally passing any
StringCompareroption.tarekgh commentedon Nov 15, 2022
@Youssef1313 are you interested to look at this one?
Youssef1313 commentedon Nov 15, 2022
@tarekgh Sure. Is it ready to implement or does it go through design review first?
For reference, there is already a similar analyzer: https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1847
tarekgh commentedon Nov 15, 2022
@Youssef1313 yes we need to take it to the design review. we need to decide first if we need to do that with the Ordinal option only. Also, I think this analyzer should cover Last/IndexOf too.
tarekgh commentedon Nov 15, 2022
@stephentoub I suggest the following for this analyzer rule. If you agree I can add this information to the description, and we can mark the issue ready for review.
Globalization rules Category
Severity =
suggestionstephentoub commentedon Nov 15, 2022
I'd have thought the category would be performance rather than globalization, since it's primarily about reducing overheads rather than fixing globalization-related correctness issues. But, I don't have a strong opinion.
And, yeah, suggestion / info.
Thanks.
tarekgh commentedon Nov 15, 2022
Performance sounds good to me. I'll edit the description and mark the issue ready for review.
tarekgh commentedon Nov 15, 2022
I forgot to mention this rule should include Last/IndexOf APIs too.
10 remaining items
tarekgh commentedon Jan 13, 2023
@Youssef1313 are you interested in helping with this one?
Youssef1313 commentedon Jan 13, 2023
Sure, but that won't be soon, unfortunately. It will at least be after I finish exams.
tarekgh commentedon Jan 13, 2023
@Youssef1313 thanks!
Focus on the exams and forget anything else :-) nothing urgent. good luck!
mrahhal commentedon Jul 21, 2023
Hi. Would like to work on this if no one's on it.
Youssef1313 commentedon Jul 21, 2023
@mrahhal I haven't yet looked into it. You can go for it!
danmoseley commentedon Jul 21, 2023
@mrahhal I have assigned to you.
mrahhal commentedon Jul 21, 2023
Confirming a few things:
string.Contains, which would have been just another target method here. Wondering if we can update this existing analyzer and reuse the same diagnostic ID and extend it to cover the other methods, or if we have to create a new analyzer and a new diagnostic ID.ContainsandStartsWith/etc.tarekgh commentedon Jul 23, 2023
Please use new diagnostics IDs for all new cases we are covering here.
Let's look at the
UseStringContainsCharOverloadWithSingleCharactersAnalyzerseparately and we can fix it as needed. Supporting more cases with existing one may need to be done with different diagnsotic IDs.That is fine to implement these in one analyzer as long as getting the results as described in #78392 (comment)
sharwell commentedon Jul 25, 2023
@stephentoub @danmoseley side note that it may help the review process for these analyzers if, after the design review is complete, the accepted design is added to a new section at the end of the first post of the issue. This would allow review of the implementation relative to just that design without needing to read the entire thread.
mrahhal commentedon Jul 25, 2023
Agreed. It's a bit taxing going back and forth between the original comment and the other comments, especially if the original comment has outdated information, as is the case in this issue.