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

CA2022: Do not flag well known reliable stream types #7269

Merged
merged 1 commit into from
Mar 26, 2024

Conversation

mpidash
Copy link
Contributor

@mpidash mpidash commented Mar 26, 2024

Fixes #7268.

I only excluded exact type matches of known reliable stream types (i.e. instances of System.IO.MemoryStream or System.IO.UnmanagedMemoryStream).
Let me know if we want to exclude child classes as well, but it seems based on the comment in the issue (#7268 (comment)) that this could lead to false negatives.

We could detect more cases with flow analysis, but this would mean that the rule is disabled by default (AFAIK).

cc @buyaa-n @stephentoub

@mpidash mpidash requested a review from a team as a code owner March 26, 2024 21:20
Copy link

codecov bot commented Mar 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.48%. Comparing base (0868ca2) to head (d454355).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7269      +/-   ##
==========================================
- Coverage   96.48%   96.48%   -0.01%     
==========================================
  Files        1442     1442              
  Lines      345212   345257      +45     
  Branches    11345    11346       +1     
==========================================
+ Hits       333081   333120      +39     
- Misses       9256     9263       +7     
+ Partials     2875     2874       -1     

@buyaa-n
Copy link
Member

buyaa-n commented Mar 26, 2024

Let me know if we want to exclude child classes as well, but it seems based on the comment in the issue (#7268 (comment)) that this could lead to false negatives.

Based on that comment I would say no, but as the Stream or MemoryStream are not sealed we might want to make it configurable: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/Analyzer%20Configuration.md#excluded-symbols what you think @mpidash @stephentoub?

Copy link
Member

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

The fix LGTM, thank you, I'll wait for opinions for the open question for making it configurable

@stephentoub
Copy link
Member

Let me know if we want to exclude child classes as well, but it seems based on the comment in the issue (#7268 (comment)) that this could lead to false negatives.

Based on that comment I would say no, but as the Stream or MemoryStream are not sealed we might want to make it configurable: https://github.com/dotnet/roslyn-analyzers/blob/main/docs/Analyzer%20Configuration.md#excluded-symbols what you think @mpidash @stephentoub?

We could, but I'd be fine waiting until we have real user cases for it. MemoryStream is one that comes up a lot, especially in tests, and so it's worth reducing noise about.

@buyaa-n
Copy link
Member

buyaa-n commented Mar 26, 2024

Good point, thanks!

@buyaa-n buyaa-n merged commit e110f09 into dotnet:main Mar 26, 2024
14 checks passed
@@ -130,6 +141,11 @@ public bool IsAnyStreamReadAsyncMethod(IMethodSymbol method)
SymbolEqualityComparer.Default.Equals(method, m) || IsOverrideOf(method, m));
}

public bool IsKnownReliableStreamType(ITypeSymbol? type)
{
return _knownReliableStreamTypes.Any(t => SymbolEqualityComparer.Default.Equals(type, t));
Copy link
Member

Choose a reason for hiding this comment

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

Can the set be created with this comparer so the lookup is O(1) rather than O(N)?

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.

CA2022 Special-case MemoryStream and UnmanagedMemoryStream and don't issue diagnostic
4 participants