Skip to content

[Analyzer] Recommend string.StartsWith(c) instead of string.StartsWith("c") #78392

Closed
dotnet/roslyn-analyzers
#6799

Description

@stephentoub

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

Severity = suggestion

For the case when not passing StringComparison parameter

Severity = none (off by default)

Activity

  1. added
    code-analyzer
    code-fixer
    on Nov 15, 2022
  2. ghost added
    untriaged
    on Nov 15, 2022
  3. added this to the Future milestone on Nov 15, 2022
  4. ghost removed
    untriaged
    on Nov 15, 2022
  5. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    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 StringComparer option.

  6. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    @Youssef1313 are you interested to look at this one?

  7. Youssef1313 commented on Nov 15, 2022

    @Youssef1313
    Member

    @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

  8. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    @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.

  9. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    @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 = suggestion

  10. stephentoub commented on Nov 15, 2022

    @stephentoub
    MemberAuthor

    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.

  11. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    Performance sounds good to me. I'll edit the description and mark the issue ready for review.

  12. tarekgh commented on Nov 15, 2022

    @tarekgh
    Member

    I forgot to mention this rule should include Last/IndexOf APIs too.

  13. 10 remaining items

  14. added
    api-approved
    on Nov 22, 2022
  15. tarekgh commented on Jan 13, 2023

    @tarekgh
    Member

    @Youssef1313 are you interested in helping with this one?

  16. Youssef1313 commented on Jan 13, 2023

    @Youssef1313
    Member

    Sure, but that won't be soon, unfortunately. It will at least be after I finish exams.

  17. tarekgh commented on Jan 13, 2023

    @tarekgh
    Member

    @Youssef1313 thanks!

    Focus on the exams and forget anything else :-) nothing urgent. good luck!

  18. added
    help wanted
    on May 30, 2023
  19. mrahhal commented on Jul 21, 2023

    @mrahhal

    Hi. Would like to work on this if no one's on it.

  20. Youssef1313 commented on Jul 21, 2023

    @Youssef1313
    Member

    @mrahhal I haven't yet looked into it. You can go for it!

  21. danmoseley commented on Jul 21, 2023

    @danmoseley
    Contributor

    @mrahhal I have assigned to you.

  22. mrahhal commented on Jul 21, 2023

    @mrahhal

    Confirming a few things:

    1. There's already UseStringContainsCharOverloadWithSingleCharactersAnalyzer covering 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.
      • Related, UseStringContainsCharOverloadWithSingleCharactersAnalyzer does not look at the comparison at all and will always suggest the change. Shouldn't this be updated anyway to match the comparison rules/conditions described in this issue? Otherwise we will have a seemingly arbitrary difference in suggested diags between Contains and StartsWith/etc.
    2. I'm assuming I could implement the different methods and cases in a single analyzer (similar to UseExceptionThrowHelpers for example). Asking because the original comment says "Proposal to have 2 analyzers ..."
  23. tarekgh commented on Jul 23, 2023

    @tarekgh
    Member

    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.

    Please use new diagnostics IDs for all new cases we are covering here.

    Related, UseStringContainsCharOverloadWithSingleCharactersAnalyzer does not look at the comparison at all and will always suggest the change. Shouldn't this be updated anyway to match the comparison rules/conditions described in this issue? Otherwise we will have a seemingly arbitrary difference in suggested diags between Contains and StartsWith/etc.

    Let's look at the UseStringContainsCharOverloadWithSingleCharactersAnalyzer separately and we can fix it as needed. Supporting more cases with existing one may need to be done with different diagnsotic IDs.

    'm assuming I could implement the different methods and cases in a single analyzer (similar to UseExceptionThrowHelpers for example). Asking because the original comment says "Proposal to have 2 analyzers ..."

    That is fine to implement these in one analyzer as long as getting the results as described in #78392 (comment)

  24. sharwell commented on Jul 25, 2023

    @sharwell
    Contributor

    @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.

  25. mrahhal commented on Jul 25, 2023

    @mrahhal

    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.

  26. ghost locked as resolved and limited conversation to collaborators on Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Globalizationcode-analyzercode-fixerhelp wantedpartner-impact

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions