-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Analyzer] Recommend string.StartsWith(c) instead of string.StartsWith("c") #78392
Comments
Tagging subscribers to this area: @dotnet/area-system-globalization Issue DetailsWe should consider adding an analyzer that recommends using the char-based However, this can have observable behavioral differences. 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 Same for EndsWith.
|
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 |
@Youssef1313 are you interested to look at this one? |
@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 |
@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. |
@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. |
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. |
Performance sounds good to me. I'll edit the description and mark the issue ready for review. |
I forgot to mention this rule should include Last/IndexOf APIs too. |
One thing to note here: globalization analyzers, by default, skip the analysis if |
I don't have a strong preference here too. I am seeing this rule can still be useful to fire with Invariant mode too. I mean with the case when passing |
Looks good as proposed: Target methods:
Three different diagnostic IDs all in the Performance category
|
@Youssef1313 are you interested in helping with this one? |
Sure, but that won't be soon, unfortunately. It will at least be after I finish exams. |
@Youssef1313 thanks! Focus on the exams and forget anything else :-) nothing urgent. good luck! |
Hi. Would like to work on this if no one's on it. |
@mrahhal I haven't yet looked into it. You can go for it! |
@mrahhal I have assigned to you. |
Confirming a few things:
|
Please use new diagnostics IDs for all new cases we are covering here.
Let's look at the
That is fine to implement these in one analyzer as long as getting the results as described in #78392 (comment) |
@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. |
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. |
We should consider adding an analyzer that recommends using the char-based
StartsWith(char)
instead ofStartsWith(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, whereasstring.StartsWith(string)
uses the current culture. This, for example, printsTrue
and thenFalse
, highlighting that there can be semantic differences due to culture:It would be safe to replace
string.StartsWith("c", StringComparison.Ordinal)
withstring.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
Severity =
suggestion
For the case when not passing
StringComparison
parameterSeverity =
none
(off by default)The text was updated successfully, but these errors were encountered: