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 CA1872: Prefer 'Convert.ToHexString' over 'BitConverter.ToString' #6967
Conversation
This analyzer detects the usage of the pattern `BitConverter.ToString(bytes).Replace("-", "")` to convert an array of bytes to an uppercase hex string (without hyphens in between) and replaces it with a call to `Convert.ToHexString(bytes)`. The analyzer will also try to preserve chaining `ToLower*` in between for a lowercase hex string.
...re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.cs
Show resolved
Hide resolved
...re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.cs
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6967 +/- ##
==========================================
- Coverage 96.47% 96.47% -0.01%
==========================================
Files 1436 1439 +3
Lines 342881 344370 +1489
Branches 11292 11324 +32
==========================================
+ Hits 330797 332230 +1433
- Misses 9230 9272 +42
- Partials 2854 2868 +14 |
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
@buyaa-n I've addressed your feedback and also tried to improve the messages, PTAL 😸. Here are the findings with the new message template:
And an example of an applied code fix: @gewarren: It would be great if you could also take a look at the resource strings, thanks! |
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.
I left some feedback on the resource strings. Thanks for tagging me!
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs
Outdated
Show resolved
Hide resolved
...re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.cs
Outdated
Show resolved
Hide resolved
Thanks for the review @buyaa-n! I've addressed your feedback, PTAL. |
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.
Thank you @mpidash, LGTM.
It's good to go after the #6967 (comment) done
…#6967) * Add CA1872: Prefer 'Convert.ToHexString' over 'BitConverter.ToString' This analyzer detects the usage of the pattern `BitConverter.ToString(bytes).Replace("-", "")` to convert an array of bytes to an uppercase hex string (without hyphens in between) and replaces it with a call to `Convert.ToHexString(bytes)`. The analyzer will also try to preserve chaining `ToLower*` in between for a lowercase hex string. * Use span overload when replacing call with two arguments * Use Convert.ToHexStringLower if available * Improve resource strings * Improve description resource string * Remove redundant helper methods * Change invocation analyze order and remove duplicate work in fixer * Remove temporary Net90 reference assembly
Fixes dotnet/runtime#81796.
This analyzer detects the usage of the pattern
BitConverter.ToString(bytes).Replace("-", "")
to convert an array of bytes to an uppercase hex string (without hyphens in between) and replaces it with a call toConvert.ToHexString(bytes)
.The analyzer will also try to preserve chaining
ToLower*
in between (before and afterstring.Replace
) for a lowercase hex string.I have found 2 warnings in
dotnet/runtime
, 1 warning indotnet/roslyn
and no warnings indotnet/aspnetcore
anddotnet/roslyn-analyzers
: