Skip to content

Commit

Permalink
Modify S4052: Promote to Sonar-way (#9629)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource authored Aug 9, 2024
1 parent 6ffc193 commit ca7bacd
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 49 deletions.
34 changes: 18 additions & 16 deletions analyzers/rspec/cs/S4052.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
<h2>Why is this an issue?</h2>
<p>With the advent of .NET framework version 2, certain practices have become obsolete.</p>
<p>With the advent of .NET Framework 2.0, certain practices and types have become obsolete.</p>
<p>In particular, exceptions should now extend <code>System.Exception</code> instead of <code>System.ApplicationException</code>. Similarly, generic
collections should be used instead of the older, non-generic, ones. Finally when creating an XML view, you should not extend
<code>System.Xml.XmlDocument</code>.</p>
<p>This rule raises an issue when an externally visible type extends one of these types:</p>
<code>System.Xml.XmlDocument</code>. This rule raises an issue when an externally visible type extends one of these types:</p>
<ul>
<li> <code>System.ApplicationException</code> </li>
<li> <code>System.Xml.XmlDocument</code> </li>
<li> <code>System.Collections.CollectionBase</code> </li>
<li> <code>System.Collections.DictionaryBase</code> </li>
<li> <code>System.Collections.Queue</code> </li>
<li> <code>System.Collections.ReadOnlyCollectionBase</code> </li>
<li> <code>System.Collections.SortedList</code> </li>
<li> <code>System.Collections.Stack</code> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.applicationexception">System.ApplicationException</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmldocument">System.Xml.XmlDocument</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.collectionbase">System.Collections.CollectionBase</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.dictionarybase">System.Collections.DictionaryBase</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.queue">System.Collections.Queue</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.readonlycollectionbase">System.Collections.ReadOnlyCollectionBase</a>
</li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.sortedlist">System.Collections.SortedList</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.collections.stack">System.Collections.Stack</a> </li>
</ul>
<h3>Noncompliant code example</h3>
<pre>
<h2>How to fix it</h2>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
using System;
using System.Collections;

Expand All @@ -26,10 +28,10 @@ <h3>Noncompliant code example</h3>
}
}
</pre>
<h3>Compliant solution</h3>
<pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
using System;
using System.Collections;
using System.Collections.ObjectModel;

namespace MyLibrary
{
Expand Down
1 change: 1 addition & 0 deletions analyzers/rspec/cs/Sonar_way_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@
"S4019",
"S4035",
"S4036",
"S4052",
"S4061",
"S4070",
"S4136",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,42 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

namespace SonarAnalyzer.Rules.CSharp
namespace SonarAnalyzer.Rules.CSharp;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TypesShouldNotExtendOutdatedBaseTypes : SonarDiagnosticAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class TypesShouldNotExtendOutdatedBaseTypes : SonarDiagnosticAnalyzer
{
private const string DiagnosticId = "S4052";
private const string MessageFormat = "Refactor this type not to derive from an outdated type '{0}'.";
private const string DiagnosticId = "S4052";
private const string MessageFormat = "Refactor this type not to derive from an outdated type '{0}'.";

private static readonly DiagnosticDescriptor Rule =
DescriptorFactory.Create(DiagnosticId, MessageFormat);
private static readonly DiagnosticDescriptor Rule =
DescriptorFactory.Create(DiagnosticId, MessageFormat);

private static readonly ImmutableArray<KnownType> OutdatedTypes =
ImmutableArray.Create(
KnownType.System_ApplicationException,
KnownType.System_Xml_XmlDocument,
KnownType.System_Collections_CollectionBase,
KnownType.System_Collections_DictionaryBase,
KnownType.System_Collections_Queue,
KnownType.System_Collections_ReadOnlyCollectionBase,
KnownType.System_Collections_SortedList,
KnownType.System_Collections_Stack);
private static readonly ImmutableArray<KnownType> OutdatedTypes =
ImmutableArray.Create(
KnownType.System_ApplicationException,
KnownType.System_Xml_XmlDocument,
KnownType.System_Collections_CollectionBase,
KnownType.System_Collections_DictionaryBase,
KnownType.System_Collections_Queue,
KnownType.System_Collections_ReadOnlyCollectionBase,
KnownType.System_Collections_SortedList,
KnownType.System_Collections_Stack);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(Rule);

protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
var classDeclaration = (ClassDeclarationSyntax)c.Node;
var classSymbol = (INamedTypeSymbol)c.ContainingSymbol;
protected override void Initialize(SonarAnalysisContext context) =>
context.RegisterNodeAction(c =>
{
var classDeclaration = (ClassDeclarationSyntax)c.Node;
var classSymbol = (INamedTypeSymbol)c.ContainingSymbol;
if (!classDeclaration.Identifier.IsMissing
&& classSymbol.BaseType.IsAny(OutdatedTypes))
{
c.ReportIssue(Rule, classDeclaration.Identifier, messageArgs: classSymbol.BaseType.ToDisplayString());
}
},
// The rule is not applicable for records as at the current moment all the outdated types are classes and records cannot inherit classes.
SyntaxKind.ClassDeclaration);
}
if (!classDeclaration.Identifier.IsMissing
&& classSymbol.BaseType.IsAny(OutdatedTypes))
{
c.ReportIssue(Rule, classDeclaration.Identifier, messageArgs: classSymbol.BaseType.ToDisplayString());
}
},
// The rule is not applicable for records as at the current moment all the outdated types are classes and records cannot inherit classes.
SyntaxKind.ClassDeclaration);
}

0 comments on commit ca7bacd

Please sign in to comment.