Skip to content

Use span-based string.Concat #33777

Closed
Closed

Description

@terrajobst

The rule should flag instances of a pattern like str1 + str2.Substring(...) + str3 or string.Concat(str1, str2.Substring(...), str3) and instead switch to using the span-based string.Concat(str1, str2.AsSpan(...), str3).

Category: Performance
Suggested severity: Info

Activity

  1. added
    api-suggestion
    code-analyzer
    on Mar 19, 2020
  2. added
    untriaged
    on Mar 19, 2020
  3. changed the title [-]Use string.Concat[/-] [+]Use span-based string.Concat[/+] on Mar 20, 2020
  4. added this to the Future milestone on Mar 20, 2020
  5. added
    code-fixer
    on Mar 21, 2020
  6. jeffhandley commented on Mar 21, 2020

    @jeffhandley
    Member

    Estimates:

    • Analyzer: Medium
    • Fixer: Medium
  7. removed
    untriaged
    on Mar 21, 2020
  8. Mrnikbobjeff commented on Sep 3, 2020

    @Mrnikbobjeff

    The invocation of concat with substring can be caught by the string.Substring(x) analyzer which is already proposed

  9. buyaa-n commented on Nov 6, 2020

    @buyaa-n
    Contributor

    Should this analyzer flag a case without Substring() and suggest Concat? like str1 + str2 + str3 => string.Concat(str1, str2, str3)

  10. modified the milestones: Future, 6.0.0 on Nov 6, 2020
  11. added
    api-approved
    api-ready-for-review
    and removed
    api-suggestion
    api-approved
    on Nov 6, 2020
  12. bartonjs commented on Nov 10, 2020

    @bartonjs
    Member

    Video

    • Find calls to str1 + str2.Substring(...) + ... (but not string.Concat(str1, str2.Substring(...), ...), since that's in a different analyzer)
    • If there's an applicable overload of string.Concat that accepts ReadOnlySpan<char> with the correct number of arguments, suggest changing the Substring call to AsSpan and calling string.Concat
    • Don't arbitrarily suggest changing str1 + str2 to string.Concat(str1, str2).

    Category: Performance
    Suggested severity: Info

  13. added
    api-approved
    and removed
    api-ready-for-review
    on Nov 10, 2020
  14. carlossanlop commented on Nov 10, 2020

    @carlossanlop
    Contributor

    @Mrnikbobjeff was this one of the analyzer proposals that you had already started working on? It's been approved, so let us know if you'd like to work on it.

  15. added
    help wanted
    on Nov 11, 2020
  16. NewellClark commented on Jan 25, 2021

    @NewellClark
    Contributor

    I would like to be assigned to this.

  17. NewellClark commented on Jan 29, 2021

    @NewellClark
    Contributor

    Quick question about trivia: if we have foo /*A*/ + /*B*/ bar.Substring(1), do we want to turn it into
    string.Concat(foo, /*A*/ /*B*/ bar.AsSpan(1))
    or string.Concat(foo /*A*/ , /*B*/ bar.AsSpan(1))?
    If we want the first option, do we want to collapse the double spaces between /*A*/ and /*B*/?
    If we want the second option, do we want to collapse the extra spaces to the left of the comma?

  18. removed
    help wanted
    on Feb 4, 2021
  19. NewellClark commented on May 22, 2021

    @NewellClark
    Contributor

    Analyzer/fixer and docs have been completed. This issue can be closed.

  20. jeffhandley commented on May 22, 2021

    @jeffhandley
    Member

    Thanks, @NewellClark!!

  21. ghost locked as resolved and limited conversation to collaborators on Jun 21, 2021
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.Runtimecode-analyzercode-fixer

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions