Skip to content

Remove unnecessary CA2022 suppressions #47904

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

Merged
merged 13 commits into from
Apr 11, 2025
Merged

Conversation

Winniexu01
Copy link
Member

@Winniexu01 Winniexu01 commented Mar 26, 2025

Related to dotnet/source-build#4322

Remove all the unnecessary CA2022 suppressions

VMR: https://dev.azure.com/dnceng/internal/_build/results?buildId=2673846&view=results (internal Microsoft link)

@Copilot Copilot AI review requested due to automatic review settings March 26, 2025 08:51
@ghost ghost added Area-Infrastructure untriaged Request triage from a team member labels Mar 26, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request removes the unnecessary CA2022 warning suppressions from test files to clean up the code. The changes update the test files by removing obsolete #pragma warning disable/restore statements.

  • Removed CA2022 suppression in GivenDotnetSlnAdd.cs
  • Removed CA2022 suppression in GivenDotnetSlnRemove.cs

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
test/dotnet-sln.Tests/GivenDotnetSlnAdd.cs Removed obsolete CA2022 suppression lines
test/dotnet-sln.Tests/GivenDotnetSlnRemove.cs Removed obsolete CA2022 suppression lines

@Winniexu01 Winniexu01 marked this pull request as draft March 26, 2025 08:59
@KalleOlaviNiemitalo
Copy link
Contributor

Those are instances of FileStream, rather than MemoryStream or UnmanagedMemoryStream, so the CA2022 warnings correspond to actual bugs in the tests. Do you intend to fix those, e.g. by making the tests call Stream.ReadExactly?

@Winniexu01
Copy link
Member Author

Yes, I'm trying to fix the CA 2022 warnings by calling Stream.ReadExactly.

@Winniexu01 Winniexu01 reopened this Mar 28, 2025
@Winniexu01 Winniexu01 marked this pull request as ready for review March 28, 2025 11:12
@Winniexu01 Winniexu01 requested a review from a team as a code owner March 28, 2025 11:12
@Winniexu01
Copy link
Member Author

Add @dotnet/source-build and @dotnet/product-construction as reviewers.

Comment on lines 174 to 178
#if NET
await Stream.ReadExactlyAsync(Array.Empty<byte>(), 0, 0, cancellationToken);
#else
await Stream.ReadAsync(Array.Empty<byte>(), 0, 0, cancellationToken);
#pragma warning restore CA2022
#endif
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of using #if NET to choose between ReadExactlyAsync and ReadAsync, I think it would be better to conditionally define ReadExactlyAsync as an extension method.

Copy link
Member Author

Choose a reason for hiding this comment

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

Update: 57ed326

#pragma warning disable CA2022 // Avoid inexact read
await Stream.ReadAsync(Array.Empty<byte>(), 0, 0, cancellationToken);
#pragma warning restore CA2022
await ReadExactlyAsync(Stream, Array.Empty<byte>(), 0, 0, cancellationToken);
Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought, this is a special case because the number of bytes requested is 0; the code attempts to poke the pipe to update its state, without actually reading any bytes. Because the number of bytes actually read cannot be negative, it is not possible that the call would read fewer bytes than requested. The analyzer should recognise this zero-length read as a special case but I don't know whether it already does. So this should keep using Stream.ReadAsync and suppress the warning if necessary.

Copy link
Contributor

Choose a reason for hiding this comment

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

ReadExactlyAsync seems a bad choice here because it is not clear whether that passes the zero-length read down to ReadAsync (as is needed here), or just returns 0 immediately (which would not update the status of the pipe).

Copy link
Member Author

@Winniexu01 Winniexu01 Mar 31, 2025

Choose a reason for hiding this comment

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

https://github.com/dotnet/runtime/blob/7cc32192cbb4894bfebbb09a903215993207a3c0/src/libraries/System.Private.CoreLib/src/System/IO/Stream.cs#L431
Zero-length is a valid value, because ReadExactlyAsync will check if the count parameter is exactly zero, which is a valid case that should be handled specially (by immediately returning without performing any I/O operations).

I mad a mistake by adding an equal sign here, now I will remove it to maintain consistency with ReadExactlyAsync behavior.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, a zero-length read is valid. But if ReadExactlyAsync implements it by returning immediately without calling an operating-system API, then it doesn't update the state of the pipe, and .NET SDK won't detect whether the other end has disconnected, according to this comment:

// We have to poll for disconnection by reading, PipeStream.IsConnected isn't reliable unless you
// actually do a read - which will cause it to update its state.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, got it. I will revert the change.

@Winniexu01
Copy link
Member Author

Hi @KalleOlaviNiemitalo , I have reverted the changes for ReadExactlyAsync, could you please do another quick code review for ReadExactly?

@marcpopMSFT marcpopMSFT requested a review from edvilme April 8, 2025 20:32
Copy link
Contributor

@edvilme edvilme left a comment

Choose a reason for hiding this comment

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

This looks reasonable to me :)

@edvilme edvilme removed the untriaged Request triage from a team member label Apr 11, 2025
@akoeplinger akoeplinger merged commit 78080ba into dotnet:main Apr 11, 2025
39 checks passed
@Winniexu01 Winniexu01 deleted the CA2022 branch April 15, 2025 00:55
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.

4 participants