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

Fix to improve code fixer heuristic employed by CA1835 #4269

Merged
merged 13 commits into from
Oct 3, 2020

Conversation

carlossanlop
Copy link
Member

@carlossanlop carlossanlop commented Oct 1, 2020

Fixes #4235

For invocations of ReadAsync/WriteAsync that consider the full buffer (from 0 to buffer.Length):

await stream.ReadAsync(buffer, 0, buffer.Length);

Instead of suggesting this:

await stream.ReadAsync(buffer.AsMemory(0, buffer.Length));

We now want to suggest this:

await stream.ReadAsync(buffer);

@codecov

This comment has been minimized.

Copy link
Contributor

@mavasani mavasani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. You can improve the heuristic by using SemanticModel.GetConstantValue API.

@carlossanlop
Copy link
Member Author

carlossanlop commented Oct 2, 2020

FYI I opened a proposal to suggest simplifying TryCast calls in VB: https://github.com/dotnet/roslyn-analyzers/issues/4276

Edit: I moved it to the roslyn repo: dotnet/roslyn#48239

…the Value without having to cast to integer first.

Use existing WellKnownMemberName instead "Length".
@stephentoub
Copy link
Member

Thanks for improving this.

@carlossanlop carlossanlop merged commit 6c243f3 into dotnet:master Oct 3, 2020
@carlossanlop carlossanlop deleted the ImproveCA1835 branch October 3, 2020 02:14
Evangelink pushed a commit to Evangelink/roslyn-analyzers that referenced this pull request Oct 12, 2020
* Shared Fixer: detect arguments 0 and buffer.Length.

* CSharp fixer: Implement method that detects 0 and buffer.Length syntax nodes.

* VB fixer: Implement method that detects 0 and buffer.Length syntax nodes.

* Test data:

Add new test cases to handle partial buffer read/write.

Adjust existing test cases that use the full buffer to avoid adding the AsMemory invocation.

* ReadAsync tests: Consume the new test data.

* WriteAsync tests: Consume the new test data.

* Suggestions by Evangelink.

* Pass the semantic model to the syntax-specific method.

* Adapt the C# syntax-specific method to take the semantic model instance.

* Adapt the VB syntax-specific method to take the semantic model instance.

* Fix some failing new test cases.

* Update the unit tests.

* Simplify check for 0 by directly using the constant pattern check on the Value without having to cast to integer first.
Use existing WellKnownMemberName instead "Length".

Co-authored-by: carlossanlop <carlossanlop@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve code fixer heuristic employed by CA1835
5 participants