-
Notifications
You must be signed in to change notification settings - Fork 465
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 CA1514: Remove redundant length argument #6814
Conversation
This analyzer detects uses of Substring/Slice(start, length) that slice to the end of the buffer. In these cases, the length check is redundant, can be omitted, and the call can be replaced with Substring/Slice(start). The analyzer also detects if the evaluation of the instance or arguments can lead to side effects and does not flag these cases.
Codecov Report
@@ Coverage Diff @@
## main #6814 +/- ##
==========================================
- Coverage 96.42% 96.39% -0.03%
==========================================
Files 1398 1406 +8
Lines 333552 332375 -1177
Branches 11038 10923 -115
==========================================
- Hits 321624 320408 -1216
- Misses 9165 9212 +47
+ Partials 2763 2755 -8 |
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.
Thanks you @mpidash, left a few comments, overall LGTM
...sts/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidLengthCheckWhenSlicingToEndTests.cs
Outdated
Show resolved
Hide resolved
...re/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidLengthCheckWhenSlicingToEnd.Fixer.cs
Outdated
Show resolved
Hide resolved
...etAnalyzers/Core/Microsoft.CodeQuality.Analyzers/MicrosoftCodeQualityAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...etAnalyzers/Core/Microsoft.CodeQuality.Analyzers/MicrosoftCodeQualityAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
...ers/Core/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidLengthCheckWhenSlicingToEnd.cs
Outdated
Show resolved
Hide resolved
Thanks for broad testing, all looks valid and worth fixing. |
Co-authored-by: Buyaa Namnan <[email protected]>
Also use the new code fix title for the fixer and rerun msbuild pack.
Thanks for the review @buyaa-n, I've addressed your review, merged main, and rerun |
Seems like |
Fixes dotnet/runtime#68946.
This analyzer detects uses of
Substring/Slice(start, length)
that slice to the end of the buffer.In these cases, the length check is redundant, can be omitted, and the call can be replaced with
Substring/Slice(start)
.The analyzer also detects if the evaluation of the instance or arguments can lead to side effects and does not flag these cases.
I have found no warning when running the analyzer against
dotnet/runtime
anddotnet/roslyn-analyzers
, 1 warning indotnet/roslyn
and 8 warnings indotnet/aspnetcore
:Not sure about the resource strings, would appreciate any suggestions.