-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule S3909: Collections should implement the generic interface
- Loading branch information
1 parent
c4a5826
commit c4f4a7c
Showing
11 changed files
with
342 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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,43 @@ | ||
<p>The NET Framework 2.0 introduced the generic interface <code>System.Collections.Generic.IEnumerable<T></code> and it should be preferred over | ||
the older, non generic, interfaces.</p> | ||
<p>This rule raises an issue when a public type implements <code>System.Collections.IEnumerable</code>.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
using System; | ||
using System.Collections; | ||
|
||
public class MyData | ||
{ | ||
public MyData() | ||
{ | ||
} | ||
} | ||
|
||
public class MyList : CollectionBase // Noncompliant | ||
{ | ||
public void Add(MyData data) | ||
{ | ||
InnerList.Add(data); | ||
} | ||
|
||
// ... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
using System; | ||
using System.Collections.ObjectModel; | ||
|
||
public class MyData | ||
{ | ||
public MyData() | ||
{ | ||
} | ||
} | ||
|
||
public class MyList : Collection<MyData> | ||
{ | ||
// Implementation... | ||
} | ||
</pre> | ||
|
This file contains 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,13 @@ | ||
{ | ||
"title": "Collections should implement the generic interface", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "15min" | ||
}, | ||
"tags": [ | ||
|
||
], | ||
"defaultSeverity": "Major" | ||
} |
38 changes: 38 additions & 0 deletions
38
sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Helpers/CollectionUtils.cs
This file contains 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,38 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2017 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace SonarAnalyzer.Helpers | ||
{ | ||
public static class CollectionUtils | ||
{ | ||
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> enumerable) | ||
{ | ||
if (enumerable == null) | ||
{ | ||
return new HashSet<T>(); | ||
} | ||
|
||
return new HashSet<T>(enumerable); | ||
} | ||
} | ||
} |
This file contains 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
100 changes: 100 additions & 0 deletions
100
...lyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/CollectionsShouldImplementGenericInterface.cs
This file contains 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,100 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2017 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using SonarAnalyzer.Common; | ||
using SonarAnalyzer.Helpers; | ||
|
||
namespace SonarAnalyzer.Rules.CSharp | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
[Rule(DiagnosticId)] | ||
public sealed class CollectionsShouldImplementGenericInterface : SonarDiagnosticAnalyzer | ||
{ | ||
internal const string DiagnosticId = "S3909"; | ||
private const string MessageFormat = "Refactor this collection to implement '{0}'."; | ||
|
||
private static readonly DiagnosticDescriptor rule = | ||
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule); | ||
|
||
private static readonly Dictionary<KnownType, KnownType> nongenericToGenericMapping = | ||
new Dictionary<KnownType, KnownType> | ||
{ | ||
[KnownType.System_Collections_ICollection] = KnownType.System_Collections_Generic_ICollection_T, | ||
[KnownType.System_Collections_IList] = KnownType.System_Collections_Generic_IList_T, | ||
[KnownType.System_Collections_IEnumerable] = KnownType.System_Collections_Generic_IEnumerable_T, | ||
[KnownType.System_Collections_CollectionBase] = KnownType.System_Collections_ObjectModel_Collection_T, | ||
}; | ||
|
||
private static readonly ISet<KnownType> nongenericTypes = nongenericToGenericMapping.Keys.ToHashSet(); | ||
private static readonly ISet<KnownType> genericTypes = nongenericToGenericMapping.Values.ToHashSet(); | ||
|
||
protected override void Initialize(SonarAnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeActionInNonGenerated( | ||
c => | ||
{ | ||
var classDeclaration = c.Node as ClassDeclarationSyntax; | ||
var issues = new List<Diagnostic>(); | ||
var implementedTypes = classDeclaration?.BaseList?.Types; | ||
if (implementedTypes == null) | ||
{ | ||
return; | ||
} | ||
foreach (var typeSyntax in implementedTypes) | ||
{ | ||
var typeSymbol = c.SemanticModel.GetSymbolInfo(typeSyntax.Type).Symbol?.GetSymbolType(); | ||
var originalDefinition = typeSymbol?.OriginalDefinition; | ||
if (originalDefinition.IsAny(genericTypes)) | ||
{ | ||
return; | ||
} | ||
var nongenericType = ToKnownType(typeSymbol, nongenericTypes); | ||
if (nongenericType != null) | ||
{ | ||
issues.Add(Diagnostic.Create(rule, | ||
classDeclaration.Identifier.GetLocation(), | ||
nongenericToGenericMapping[nongenericType].TypeName)); | ||
} | ||
} | ||
issues.ForEach(i => c.ReportDiagnostic(i)); | ||
}, | ||
SyntaxKind.ClassDeclaration); | ||
} | ||
|
||
private static KnownType ToKnownType(ITypeSymbol typeSymbol, IEnumerable<KnownType> knownTypes) | ||
{ | ||
return knownTypes.FirstOrDefault(t => typeSymbol.Is(t)); | ||
} | ||
} | ||
} |
This file contains 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 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
43 changes: 43 additions & 0 deletions
43
sonaranalyzer-dotnet/src/SonarAnalyzer.Utilities/Rules.Description/S3909.html
This file contains 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,43 @@ | ||
<p>The NET Framework 2.0 introduced the generic interface <code>System.Collections.Generic.IEnumerable<T></code> and it should be preferred over | ||
the older, non generic, interfaces.</p> | ||
<p>This rule raises an issue when a public type implements <code>System.Collections.IEnumerable</code>.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
using System; | ||
using System.Collections; | ||
|
||
public class MyData | ||
{ | ||
public MyData() | ||
{ | ||
} | ||
} | ||
|
||
public class MyList : CollectionBase // Noncompliant | ||
{ | ||
public void Add(MyData data) | ||
{ | ||
InnerList.Add(data); | ||
} | ||
|
||
// ... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
using System; | ||
using System.Collections.ObjectModel; | ||
|
||
public class MyData | ||
{ | ||
public MyData() | ||
{ | ||
} | ||
} | ||
|
||
public class MyList : Collection<MyData> | ||
{ | ||
// Implementation... | ||
} | ||
</pre> | ||
|
This file contains 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
37 changes: 37 additions & 0 deletions
37
.../src/Tests/SonarAnalyzer.UnitTest/Rules/CollectionsShouldImplementGenericInterfaceTest.cs
This file contains 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,37 @@ | ||
/* | ||
* SonarAnalyzer for .NET | ||
* Copyright (C) 2015-2017 SonarSource SA | ||
* mailto: contact AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using SonarAnalyzer.Rules.CSharp; | ||
|
||
namespace SonarAnalyzer.UnitTest.Rules | ||
{ | ||
[TestClass] | ||
public class CollectionsShouldImplementGenericInterfaceTest | ||
{ | ||
[TestMethod] | ||
[TestCategory("Rule")] | ||
public void CollectionsShouldImplementGenericInterface() | ||
{ | ||
Verifier.VerifyAnalyzer(@"TestCases\CollectionsShouldImplementGenericInterface.cs", | ||
new CollectionsShouldImplementGenericInterface()); | ||
} | ||
} | ||
} |
Oops, something went wrong.