Skip to content

Analyzer proposal: Use String.Contains(char) instead of String.Contains(String) #47180

Closed
dotnet/roslyn-analyzers
#4908

Description

@xtqqczze

Suggested category: Performance
Suggested default severity: Suggestion
Fix is breaking or non-breaking: Non-breaking

Cause

This rule locates calls to Contains(String) when the string consists of a single char, and suggests using Contains(char) instead

Rule description

When Contains(String) is used with a single char, the call can be safely substituted with Contains(char).

Additional context

#24068
dotnet/coreclr#15740
#36310

Activity

  1. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jan 19, 2021
  2. mavasani commented on Jan 19, 2021

    @mavasani

    Tagging @carlossanlop @buyaa-n - .NET API analyzer suggestion.

  3. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    code-analyzerMarks an issue that suggests a Roslyn analyzer
    code-fixerMarks an issue that suggests a Roslyn code fixer
    on Jan 19, 2021
  4. carlossanlop commented on Jan 22, 2021

    @carlossanlop
    Contributor

    Seems pretty straightforward. There was a previous effort to manually replace instances of String.Contains(string) with String.Contains(char), so an analyzer should automatically find any new instances for us from now on.

    Suggested severity: Warning
    Suggested category: Performance
    Suggested milestone: Future (but we welcome contributions for 6.0)
    Analyzer size: Small
    Fixer size: Small

    Should cover all overloads, and only where a literal is passed as argument:

    string str = "whatever";
    string find = "a";
    
    // Yes
    str.Contains("a");
    str.Contains("a", StringComparison.InvariantCultureIgnoreCase);
    
    // No
    str.Contains(find);
    str.Contains(find, StringComparison.InvariantCultureIgnoreCase);
  5. added
    api-ready-for-reviewAPI is ready for review, it is NOT ready for implementation
    and removed
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    untriagedNew issue has not been triaged by the area owner
    on Jan 22, 2021
  6. added this to the Future milestone on Jan 22, 2021
  7. GrabYourPitchforks commented on Jan 26, 2021

    @GrabYourPitchforks
    Member

    This as proposed is not a breaking change, but we need to take care if we extend this analyzer to other methods (IndexOf, etc.). Switching from string to char in those methods is an observable behavioral change.

  8. 21 remaining items

  9. buyaa-n commented on Mar 3, 2021

    @buyaa-n
    Contributor

    IMO, analyzers reporting performance issues should never be a warning by default in the .NET SDK, even if we expect no false positives. Only critical functional or security issues should be candidate for being warnings by default.

    @mavasani that makes sense to me, i will keep that in mind while triaging

  10. buyaa-n commented on Mar 3, 2021

    @buyaa-n
    Contributor

    @stephentoub do we want to fix the warnings found in runtime? Sounds like we need to do ifdef for multitargeted builds

  11. MeikTranel commented on Mar 3, 2021

    @MeikTranel

    I have the fixes ready for runtime locally - it's just one that would actually surface for consumers and another one in testing/infrastructure code. It's pretty late in Germany so I hope y'all can live with me opening up the PR tomorrow 😊

    Should I open a separate issue for that in runtime or is it okay to just throw them the PR?

  12. buyaa-n commented on Mar 3, 2021

    @buyaa-n
    Contributor

    Thanks, @MeikTranel, sounds good, i don't think we need an issue with that, you can reference this issue in your PR

  13. stephentoub commented on Jul 13, 2021

    @stephentoub
    Member

    This was implemented as CA1847. Thanks!

  14. xtqqczze commented on Jul 13, 2021

    @xtqqczze
    ContributorAuthor
  15. ghost locked as resolved and limited conversation to collaborators on Aug 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions