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

CA1002: Do not expose generic lists #3387

Merged
merged 6 commits into from
Apr 7, 2020
Merged

Conversation

Evangelink
Copy link
Member

Fix #369

@Evangelink Evangelink requested a review from a team as a code owner March 18, 2020 12:00
{
public static class Helper
{
public static bool Ext1(this List<int> list) => true;
Copy link
Member Author

Choose a reason for hiding this comment

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

This exclusion is not part of the original implementation but that seems weird to get a penalty to extend List<T> so I added an exclusion. Please feel free to let me know if you think this shouldn't be done.

@codecov
Copy link

codecov bot commented Mar 18, 2020

Codecov Report

Merging #3387 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff            @@
##           master    #3387    +/-   ##
========================================
  Coverage   95.30%   95.31%            
========================================
  Files        1029     1031     +2     
  Lines      233279   233669   +390     
  Branches    15092    15107    +15     
========================================
+ Hits       222338   222728   +390     
  Misses       9278     9278            
  Partials     1663     1663            

@mavasani
Copy link
Contributor

FYI: You will need to fix a new RS diagnostic once #3444 goes in.

context.RegisterSymbolAction(context =>
{
var symbol = context.Symbol;
var methodSymbol = symbol as IMethodSymbol;
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you mean to register separate actions for each symbol kind? It seems better to move the shared code in a separate helper and register separate actions for each symbol kind that needs different checks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean replacing

context.RegisterSymbolAction(ctx => {}, SymbolKind.Field, SymbolKind.Property, SymbolKind.Method);

with

context.RegisterSymbolAction(Analyze, SymbolKind.Field);
context.RegisterSymbolAction(Analyze, SymbolKind.Property);
context.RegisterSymbolAction(Analyze, SymbolKind.Method);

private static Analyze() {}

Copy link
Contributor

Choose a reason for hiding this comment

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

I meant having separate AnalyzeMethod, AnalyzeField, etc. for each symbol kind, so you can directly cast to IMethodSymbol in AnalyzeMethod and have method specific logic only in AnalyzeMethod.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok, yeah makes sense I have overused the helpers :)


analysisContext.RegisterCompilationStartAction(context =>
{
var genericListType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemCollectionsGenericList1);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
var genericListType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemCollectionsGenericList1);
if (!context.Compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemCollectionsGenericList1, out var genericListType))
{
return;
}

var symbol = context.Symbol;
var methodSymbol = symbol as IMethodSymbol;

// FxCop compat: only analyze externally visible symbols by default.
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Please update https://github.com/dotnet/roslyn-analyzers/blob/master/docs/Analyzer%20Configuration.md#analyzed-api-surface and file a doc issue to mention api surface configurability for this rule. Same for other rules that you are supporting this option.

@Evangelink Evangelink requested a review from mavasani April 7, 2020 08:23
@mavasani mavasani merged commit 90837b7 into dotnet:master Apr 7, 2020
@Evangelink Evangelink deleted the CA1002 branch April 7, 2020 14:33
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.

Port FxCop rule CA1002: DoNotExposeGenericLists
2 participants