-
Notifications
You must be signed in to change notification settings - Fork 468
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
Add Analyzer & Codefix - CA1847 - Use String.Contains(Char) instead of String.Contains(String) with single characters #4908
Add Analyzer & Codefix - CA1847 - Use String.Contains(Char) instead of String.Contains(String) with single characters #4908
Conversation
Codecov Report
@@ Coverage Diff @@
## release/6.0.1xx #4908 +/- ##
===================================================
- Coverage 95.59% 95.59% -0.01%
===================================================
Files 1226 1231 +5
Lines 280904 281221 +317
Branches 16872 16905 +33
===================================================
+ Hits 268532 268830 +298
- Misses 10096 10108 +12
- Partials 2276 2283 +7 |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharacters.Fixer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersTests.cs
Outdated
Show resolved
Hide resolved
...soft.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersTests.cs
Outdated
Show resolved
Hide resolved
@sharwell / @Youssef1313
|
adf5fbc
to
47f104b
Compare
Two outstanding in this repo violations remain - in the issue we decided that the information severity is probably better suited. The specific violations in question are part of the shproj compile items, which have lots of different frameworks they're compiled against (See comment for reference #4908 (comment)) - much like the runtime repo i dont think the change would have been worth it, but i'm not firm on that stance - if y'all prefer the ol' As for the other repos:
Docs PR: dotnet/docs#23377 |
...soft.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersTests.cs
Outdated
Show resolved
Hide resolved
Personally I think this can be done, on TFM's that does not have In fact I would like it if that was added to the resource description on this rule that is being added. First of all we need to calculate what TFMs do not contain the #if /*Frameworks that supply the String.Contains(char) overload. */
// the actual fix to change that here.
#else
// the rest of the code but instead replace it with "String.IndexOf(char) > -1".
#endif I would actually prefer this as it then would guard people who multitarget from scratching their heads and wondering what to do and then decide to revert the code fix and then suppress it in their |
@AraHaan Like in this PR? dotnet/runtime#49094
I am unsure - are you proposing we add this as an addendum to the I can see value in the documentation part, but i'm unsure if this is worth setting the precedent for nor am sure if its even possible to detect the context beyond the single target framework compilation. |
Yes but does that also apply to netcoreapp3.1+ as well too since .NET Core 3.1 implements stardard2.1? |
@sharwell can you take a look at this again - i think a resolved all issues raised so far and it feels like the re-request for review went under your radar 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you target the PR to release/6.0.1xx-preview3
branch?
...ore.Analyzers/Performance/CSharpUseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...oft.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharacters.Fixer.cs
Outdated
Show resolved
Hide resolved
...Core.Analyzers/Performance/BasicUseStringContainsCharOverloadWithSingleCharactersAnalyzer.vb
Outdated
Show resolved
Hide resolved
Yes, that is very common. We have large number of community contributors, and bunch of WIP new analyzer PRs. If you prefer, you can hold off bumping up the rule ID until you have received a complete review/approval and then do a final merge resolution, which should require bumping the rule ID |
@MeikTranel Just FYI: There are 3 more new analyzer PRs opened prior to yours that need a review/approval. I am slowly getting through them, but please expect a bit of delay/merge conflicts as we wade through them. |
Haha no issue. My ID update chops have been honed by now 🤣 |
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
...t.NetCore.Analyzers/Performance/UseStringContainsCharOverloadWithSingleCharactersAnalyzer.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MeikTranel I think your PR is closest to completion amongst the open PRs. If you can quickly address the last set of review comments, I should be able to merge your PR :). Thanks against for your patience.
@mavasani We resolved the numbering problem in StyleCop Analyzers by assigning IDs at the time analyzer proposals were approved. We sometimes end up with a gap if a later proposal gets implemented first, but it avoids the conflicts. |
@mavasani i was able to move this out successfully like we talked about - theres still some outstanding conflicts but those are ID conflicts again - i can fix those once the regular review stuff has been resolved. Just make sure to tag me so my phone gives me a notification 🙃 |
@MeikTranel this is ready to merge once you resolve the merge conflicts. |
Fan out to different implementations for VB and CSharp since named argument have no IOperation abstraction
fec82f6
to
cb17df3
Compare
should be good to go now ;) |
{ | ||
public bool TestMethod(string input) | ||
{ | ||
return ""testString"".Contains([|""a""|]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably not very important, but consider adding a test for constant interpolated strings, i.e return "testString".Contains($"a");
or "testString.Contains($"{CONST_CHAR_DEFINED_ABOVE}");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That use case was specifically excluded in the source issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless roslyn somehow optimizes constant values in a way that the interpolation with a constant value would end up classifying as a LiteralSyntax
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MeikTranel I couldn't see anything about it in the comments. Was that said in the API review video?
Resolves dotnet/runtime#47180
Outstanding Work:
roslyn-analyzer violations: Addressed here Add Analyzer & Codefix - CA1847 - Use String.Contains(Char) instead of String.Contains(String) with single characters #4908 (comment)
roslyn-analyzers/src/Utilities/Compiler/Options/SymbolNamesWithValueOption.cs
Line 112 in 802b2a7
roslyn-analyzers/src/Utilities/Compiler/RulesetToEditorconfigConverter.cs
Line 163 in 802b2a7
runtime PR (two small violations of the rule): Use string.Contains(char) wherever possible - fallback to IndexOf if possible runtime#49094
docs PR: Added documentation for new rule CA1847 - Use string.Contains(char) w… docs#23377
The roslyn-analyzer violations are tricky - they're part of shared code that is used by both frameworks that have String.Contains(char) and those that don't - is it really worth the #if extravaganza here or should i just ignore them?