- Notifications
You must be signed in to change notification settings - Fork481
Add CA1872: Prefer 'Convert.ToHexString' over 'BitConverter.ToString'#6967
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This analyzer detects the usage of the pattern`BitConverter.ToString(bytes).Replace("-", "")` to convert an array ofbytes to an uppercase hex string (without hyphens in between) andreplaces it with a call to `Convert.ToHexString(bytes)`.The analyzer will also try to preserve chaining `ToLower*` in betweenfor a lowercase hex string....re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.csShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
codecovbot commentedSep 30, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 |
Uh oh!
There was an error while loading.Please reload this page.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
mpidash commentedMar 8, 2024
@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! |
gewarren left a comment
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 OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...ers/Core/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...re/Microsoft.NetCore.Analyzers/Performance/PreferConvertToHexStringOverBitConverter.Fixer.cs OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
buyaa-n commentedMar 15, 2024
mpidash commentedMar 16, 2024
Thanks for the review@buyaa-n! I've addressed your feedback, PTAL. |
buyaa-n left a comment
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 ofbytes to an uppercase hex string (without hyphens in between) andreplaces it with a call to `Convert.ToHexString(bytes)`.The analyzer will also try to preserve chaining `ToLower*` in betweenfor 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



Fixesdotnet/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/roslynand no warnings indotnet/aspnetcoreanddotnet/roslyn-analyzers: