Skip to content
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

Merged
merged 7 commits into from
Sep 22, 2023
Merged

Conversation

mpidash
Copy link
Contributor

@mpidash mpidash commented Jul 28, 2023

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 and dotnet/roslyn-analyzers, 1 warning in dotnet/roslyn and 8 warnings in dotnet/aspnetcore:

  1. ContainedLanguageCodeSupport.cs(427,38): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  2. EmbeddedFileProvider.cs(95,23): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  3. RoutePatternParserTests.cs(112,17): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  4. MultipartReader.cs(124,25): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  5. AsciiDecoding.cs(108,78): info CA1514: 'System.Span.Slice(int, int)' uses a length check that can be avoided
  6. AsciiDecoding.cs(109,75): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  7. Program.cs(465,69): info CA1514: 'string.Substring(int, int)' uses a length check that can be avoided
  8. Utf8BufferTextWriter.cs(199,23): info CA1514: 'System.Memory.Slice(int, int)' uses a length check that can be avoided
  9. Utf8BufferTextWriterTests.cs(381,20): info CA1514: 'System.Memory.Slice(int, int)' uses a length check that can be avoided

Not sure about the resource strings, would appreciate any suggestions.

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.
@mpidash mpidash requested a review from a team as a code owner July 28, 2023 00:21
@codecov
Copy link

codecov bot commented Jul 28, 2023

Codecov Report

Merging #6814 (9164b28) into main (4aefee9) will decrease coverage by 0.03%.
The diff coverage is 97.25%.

❗ Current head 9164b28 differs from pull request most recent head fa03bad. Consider uploading reports for the commit fa03bad to get more accurate results

@@            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     

Copy link
Contributor

@buyaa-n buyaa-n left a 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

@buyaa-n
Copy link
Contributor

buyaa-n commented Sep 21, 2023

I have found no warning when running the analyzer against dotnet/runtime and dotnet/roslyn-analyzers, 1 warning in dotnet/roslyn and 8 warnings in dotnet/aspnetcore:

Thanks for broad testing, all looks valid and worth fixing.

mpidash and others added 4 commits September 22, 2023 19:31
@mpidash
Copy link
Contributor Author

mpidash commented Sep 22, 2023

Thanks for the review @buyaa-n, I've addressed your review, merged main, and rerun msbuild pack, PTAL :)

@mpidash
Copy link
Contributor Author

mpidash commented Sep 22, 2023

Seems like msbuild pack does not add a trailing newline to AnalyzerReleases.Unshipped.md, I've added it manually.

@buyaa-n buyaa-n enabled auto-merge (squash) September 22, 2023 20:45
@buyaa-n buyaa-n merged commit 39ccb5b into dotnet:main Sep 22, 2023
11 checks passed
@mpidash mpidash deleted the issue-68946 branch September 23, 2023 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Analyzer proposal]: Simplify calls to string.Substring and Memory/Span.Slice
2 participants