-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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 proposal: Use String.Contains(char) instead of String.Contains(String) #47180
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging @carlossanlop @buyaa-n - .NET API analyzer suggestion. |
Seems pretty straightforward. There was a previous effort to manually replace instances of Suggested severity: Warning 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); |
This as proposed is not a breaking change, but we need to take care if we extend this analyzer to other methods ( |
|
@carlossanlop i'd like to help with this one 🙋♂️ |
@MeikTranel thanks, assigned to you |
I'm pretty much done except two remaining tasks:
hoping to finish this by this weekend... just one quick question: How do we handle analyzer tests that are only reasonable on certain frameworks? The obvious solution would be to just remove them from the compiled test assembly by using |
You can specify Anyway, after you add the "handling the short circuit when the |
The draft PR is now up and mostly ready to pass things back to the team - one last requirements question remains tho... |
@MeikTranel can you share the error output you get if you turn on this analyzer in the runtime repo and then build it? To test your analyzer in runtime:
We should keep in mind this analyzer may improve the runtime performance if we fix all the suggested instances, so a warning severity makes sense. |
@carlossanlop I thought the initial bar for analyzers to be warnings by default was that they need to flag critical functional issues. Performance improvement analyzers should not be warnings by default. Has this stance changed recently? Can you confirm in the dotnet/runtime triage meeting? |
@MeikTranel isn't the analyzer bail out when the overload not exist? If it does then i wouldn't expect it to warn or make noise for lower versions |
Yes and that works as you expected but when a single project builds against two TFMs where one offers the overload and the other doesn't, you're left with a fixer that'll immediately generate a build error for the older target framework. This is a general issue with analyzers that remind of better alternatives that were only introduced in later successions of the frameworks. |
Oh sorry, I misunderstood, that makes sense, now i see the original description had For the failures in the runtime repo, i am not sure if we want to |
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. |
This should not be a warning. |
@mavasani that makes sense to me, i will keep that in mind while triaging |
@stephentoub do we want to fix the warnings found in runtime? Sounds like we need to do |
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? |
Thanks, @MeikTranel, sounds good, i don't think we need an issue with that, you can reference this issue in your PR |
…the overload isnt available string.Contains(char) offers better performance since by design it only needs a single iteration loop. In preparation for consuming a new analyzer we want this to be fixed in the runtime beforehand. Discussion: dotnet#47180
This was implemented as CA1847. Thanks! |
Fixed by dotnet/roslyn-analyzers#4908. |
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 singlechar
, and suggests usingContains(char)
insteadRule description
When
Contains(String)
is used with a singlechar
, the call can be safely substituted withContains(char)
.Additional context
#24068
dotnet/coreclr#15740
#36310
The text was updated successfully, but these errors were encountered: