-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Add documentation for CA2024 #44401
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
Add documentation for CA2024 #44401
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
docs/fundamentals/code-analysis/quality-rules/ca2024.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| --- | ||
| title: "CA2024: Do not use StreamReader.EndOfStream in async methods" | ||
| description: "Learn about code analysis rule CA2024 - Do not use StreamReader.EndOfStream in async methods" | ||
| ms.date: 01/16/2025 | ||
| ms.topic: reference | ||
| f1_keywords: | ||
| - CA2024 | ||
| - DoNotUseEndOfStreamInAsyncMethodsAnalyzer | ||
| helpviewer_keywords: | ||
| - CA2024 | ||
| author: mpidash | ||
| dev_langs: | ||
| - CSharp | ||
| - VB | ||
| --- | ||
|
|
||
| # CA2024: Do not use StreamReader.EndOfStream in async methods | ||
|
|
||
| | Property | Value | | ||
| |-------------------------------------|------------------------------------------------------| | ||
| | **Rule ID** | CA2024 | | ||
| | **Title** | Do not use StreamReader.EndOfStream in async methods | | ||
| | **Category** | [Reliability](reliability-warnings.md) | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
| | **Enabled by default in .NET 10** | As warning | | ||
|
|
||
| ## Cause | ||
|
|
||
| A call to <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> is made inside an async method. | ||
|
|
||
| ## Rule description | ||
|
|
||
| The property <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> can cause unintended synchronous blocking when no data is buffered. Instead, use <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> directly, which returns `null` when reaching the end of the stream. | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| To fix a violation, directly call <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> and check the return value for `null`. | ||
|
|
||
| ## Example | ||
|
|
||
| The following code snippet shows a violation of CA2024 and the fix for the violation. | ||
|
|
||
| ```csharp | ||
| public async Task Example(StreamReader streamReader) | ||
| { | ||
| while (!streamReader.EndOfStream) | ||
| { | ||
| string? line = await streamReader.ReadLineAsync(); | ||
| // Do something with line. | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```vb | ||
| Public Async Function Example(streamReader As StreamReader) As Task | ||
| While Not streamReader.EndOfStream | ||
| Dim line As String = Await streamReader.ReadLineAsync() | ||
| ' Do something with line. | ||
| End While | ||
| End Function | ||
| ``` | ||
|
|
||
| The following code snippet fixes the violation: | ||
|
|
||
| ```csharp | ||
| public async Task Example(StreamReader streamReader) | ||
| { | ||
| string? line; | ||
| while ((line = await streamReader.ReadLineAsync()) is not null) | ||
| { | ||
| // Do something with line. | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```vb | ||
| Public Async Function Example(streamReader As StreamReader) As Task | ||
| Dim line As String = Await streamReader.ReadLineAsync() | ||
| While line IsNot Nothing | ||
| ' Do something with line. | ||
| line = Await streamReader.ReadLineAsync() | ||
| End While | ||
| End Function | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| You shouldn't suppress warnings from this rule, as your app might hang if you don't fix the violations. | ||
|
|
||
| ## Suppress a warning | ||
|
|
||
| If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
|
||
| ```csharp | ||
| #pragma warning disable CA2024 | ||
| // The code that's violating the rule is on this line. | ||
| #pragma warning restore CA2024 | ||
| ``` | ||
|
|
||
| To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
|
||
| ```ini | ||
| [*.{cs,vb}] | ||
| dotnet_diagnostic.CA2024.severity = none | ||
| ``` | ||
|
|
||
| For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.