Skip to content

Commit

Permalink
Update MA0016 documentation (#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou authored Jul 20, 2023
1 parent 2de2b53 commit df15420
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ If you are already using other analyzers, you can check [which rules are duplica
|[MA0013](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0013.md)|Design|Types should not extend System.ApplicationException|⚠️|✔️||
|[MA0014](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0014.md)|Design|Do not raise System.ApplicationException type|⚠️|✔️||
|[MA0015](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0015.md)|Usage|Specify the parameter name in ArgumentException|⚠️|✔️||
|[MA0016](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0016.md)|Design|Prefer returning collection abstraction instead of implementation|⚠️|✔️||
|[MA0016](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0016.md)|Design|Prefer using collection abstraction instead of implementation|⚠️|✔️||
|[MA0017](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)|Design|Abstract types should not have public or internal constructors|⚠️|✔️|✔️|
|[MA0018](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0018.md)|Design|Do not declare static members on generic types|⚠️|✔️||
|[MA0019](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0019.md)|Usage|Use EventArgs.Empty|⚠️|✔️|✔️|
Expand Down Expand Up @@ -104,7 +104,7 @@ If you are already using other analyzers, you can check [which rules are duplica
|[MA0086](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0086.md)|Design|Do not throw from a finalizer|⚠️|✔️||
|[MA0087](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0087.md)|Design|Parameters with \[DefaultParameterValue\] attributes should also be marked \[Optional\]|⚠️|✔️||
|[MA0088](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0088.md)|Design|Use \[DefaultParameterValue\] instead of \[DefaultValue\]|⚠️|✔️||
|[MA0089](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0089.md)|Performance|Optimize string method usage|ℹ️|✔️||
|[MA0089](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0089.md)|Performance|Optimize string method usage|ℹ️|✔️|✔️|
|[MA0090](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0090.md)|Design|Remove empty else/finally block|ℹ️|✔️||
|[MA0091](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0091.md)|Usage|Sender should be 'this' for instance events|⚠️|✔️|✔️|
|[MA0092](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0092.md)|Usage|Sender should be 'null' for static events|⚠️|✔️||
Expand Down
8 changes: 4 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
|[MA0013](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0013.md)|Design|Types should not extend System.ApplicationException|<span title='Warning'>⚠️</span>|✔️||
|[MA0014](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0014.md)|Design|Do not raise System.ApplicationException type|<span title='Warning'>⚠️</span>|✔️||
|[MA0015](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0015.md)|Usage|Specify the parameter name in ArgumentException|<span title='Warning'>⚠️</span>|✔️||
|[MA0016](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0016.md)|Design|Prefer returning collection abstraction instead of implementation|<span title='Warning'>⚠️</span>|✔️||
|[MA0016](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0016.md)|Design|Prefer using collection abstraction instead of implementation|<span title='Warning'>⚠️</span>|✔️||
|[MA0017](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0017.md)|Design|Abstract types should not have public or internal constructors|<span title='Warning'>⚠️</span>|✔️|✔️|
|[MA0018](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0018.md)|Design|Do not declare static members on generic types|<span title='Warning'>⚠️</span>|✔️||
|[MA0019](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0019.md)|Usage|Use EventArgs.Empty|<span title='Warning'>⚠️</span>|✔️|✔️|
Expand Down Expand Up @@ -88,7 +88,7 @@
|[MA0086](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0086.md)|Design|Do not throw from a finalizer|<span title='Warning'>⚠️</span>|✔️||
|[MA0087](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0087.md)|Design|Parameters with \[DefaultParameterValue\] attributes should also be marked \[Optional\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0088](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0088.md)|Design|Use \[DefaultParameterValue\] instead of \[DefaultValue\]|<span title='Warning'>⚠️</span>|✔️||
|[MA0089](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0089.md)|Performance|Optimize string method usage|<span title='Info'>ℹ️</span>|✔️||
|[MA0089](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0089.md)|Performance|Optimize string method usage|<span title='Info'>ℹ️</span>|✔️|✔️|
|[MA0090](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0090.md)|Design|Remove empty else/finally block|<span title='Info'>ℹ️</span>|✔️||
|[MA0091](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0091.md)|Usage|Sender should be 'this' for instance events|<span title='Warning'>⚠️</span>|✔️|✔️|
|[MA0092](https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0092.md)|Usage|Sender should be 'null' for static events|<span title='Warning'>⚠️</span>|✔️||
Expand Down Expand Up @@ -189,7 +189,7 @@ dotnet_diagnostic.MA0014.severity = warning
# MA0015: Specify the parameter name in ArgumentException
dotnet_diagnostic.MA0015.severity = warning
# MA0016: Prefer returning collection abstraction instead of implementation
# MA0016: Prefer using collection abstraction instead of implementation
dotnet_diagnostic.MA0016.severity = warning
# MA0017: Abstract types should not have public or internal constructors
Expand Down Expand Up @@ -592,7 +592,7 @@ dotnet_diagnostic.MA0014.severity = none
# MA0015: Specify the parameter name in ArgumentException
dotnet_diagnostic.MA0015.severity = none
# MA0016: Prefer returning collection abstraction instead of implementation
# MA0016: Prefer using collection abstraction instead of implementation
dotnet_diagnostic.MA0016.severity = none
# MA0017: Abstract types should not have public or internal constructors
Expand Down
7 changes: 4 additions & 3 deletions docs/Rules/MA0016.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# MA0016 - Prefer returning collection abstraction instead of implementation
# MA0016 - Prefer using collection abstraction instead of implementation

Methods and properties visible to other assemblies (`public` or `protected`) should use collection interfaces instead of collection implementations.

````c#
public List<string> Prop { get; set; } // report diagnostic
public List<string> Prop { get; set; } // non-compliant
public IList<string> Prop { get; set; } // ok
public void Method(Dictionary<string, string> arg) {} // non-compliant
````

The analyzer detects `List<T>`, `HashSet<T>`, `Dictionary<TKey,TValue>`, and `System.Collections.ObjectModel.Collection<T>`. The rules is disabled if the property is decorated with an XML serialization attribute.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public sealed class PreferReturningCollectionAbstractionInsteadOfImplementationA
{
private static readonly DiagnosticDescriptor s_rule = new(
RuleIdentifiers.PreferReturningCollectionAbstractionInsteadOfImplementation,
title: "Prefer returning collection abstraction instead of implementation",
messageFormat: "Prefer returning collection abstraction instead of implementation",
title: "Prefer using collection abstraction instead of implementation",
messageFormat: "Prefer using collection abstraction instead of implementation",
RuleCategories.Design,
DiagnosticSeverity.Warning,
isEnabledByDefault: true,
Expand Down

3 comments on commit df15420

@Micke3rd
Copy link
Contributor

@Micke3rd Micke3rd commented on df15420 Jun 26, 2024

Choose a reason for hiding this comment

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

Hi @meziantou ,
regarding MA0016
if The rules is disabled if the property is decorated with an XML serialization attribute
Shouldn't this (disabling) also apply if the class is decorated with SerializableAttribute ?

@meziantou
Copy link
Owner Author

Choose a reason for hiding this comment

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

I think this is already the case. Here's the code to detect if a property is decorated:

foreach (var attribute in XmlClassAttributeSymbols)
{
if (property.ContainingType.HasAttribute(attribute))
return true;
}

@Micke3rd
Copy link
Contributor

@Micke3rd Micke3rd commented on df15420 Jun 26, 2024

Choose a reason for hiding this comment

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

So far, this is not the case. Since we agree, I have sent a pull request as offer.

Please sign in to comment.