-
Notifications
You must be signed in to change notification settings - Fork 227
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
Rule S4041: Type names should not match namespaces #466
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
...s/expected/akka.net/Akka.Remote.TestKit-{E5957C3E-2B1E-469F-A680-7953B4DEA31B}-S4041.json
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,17 @@ | ||
{ | ||
"issues": [ | ||
{ | ||
"id": "S4041", | ||
"message": "Change the name of type 'Data' to be different from an existing framework namespace.", | ||
"location": { | ||
"uri": "akka.net\src\core\Akka.Remote.TestKit\BarrierCoordinator.cs", | ||
"region": { | ||
"startLine": 51, | ||
"startColumn": 29, | ||
"endLine": 51, | ||
"endColumn": 33 | ||
} | ||
} | ||
} | ||
] | ||
} |
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,25 @@ | ||
<p>When a type name matches the name of a publicly defined namespace, for instance one in the .NET framework class library, it leads to confusion and | ||
makes the library that much harder to use.</p> | ||
<p>This rule raises an issue when a name of a public type matches the name of a .NET Framework namespace, or a namespace of the project assembly, in a | ||
case-insensitive comparison.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
{ | ||
public class Text { // Noncompliant: Collides with System.Text | ||
} | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
{ | ||
public class MyText { | ||
} | ||
} | ||
</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": "Type names should not match namespaces", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "2min" | ||
}, | ||
"tags": [ | ||
"convention" | ||
], | ||
"defaultSeverity": "Minor" | ||
} |
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
113 changes: 113 additions & 0 deletions
113
sonaranalyzer-dotnet/src/SonarAnalyzer.CSharp/Rules/TypeNamesShouldNotMatchNamespaces.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,113 @@ | ||
/* | ||
* 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 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 TypeNamesShouldNotMatchNamespaces : SonarDiagnosticAnalyzer | ||
{ | ||
internal const string DiagnosticId = "S4041"; | ||
private const string MessageFormat = "Change the name of type '{0}' to be different from an existing framework namespace."; | ||
|
||
private static readonly DiagnosticDescriptor rule = | ||
DiagnosticDescriptorBuilder.GetDescriptor(DiagnosticId, MessageFormat, RspecStrings.ResourceManager); | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(rule); | ||
|
||
// Based on https://msdn.microsoft.com/en-us/library/gg145045%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 | ||
private static ISet<string> frameworkNamespaces = | ||
new HashSet<string> | ||
{ | ||
"accessibility", "activities", "addin", "build", "codedom", "collections", | ||
"componentmodel", "configuration", "csharp", "custommarshalers", "data", | ||
"dataflow", "deployment", "device", "diagnostics", "directoryservices", | ||
"drawing", "dynamic", "enterpriseservices", "globalization", "identitymodel", | ||
"interopservices", "io", "jscript", "linq", "location", "management", "media", | ||
"messaging", "microsoft", "net", "numerics", "printing", "reflection", "resources", | ||
"runtime", "security", "server", "servicemodel", "serviceprocess", "speech", | ||
"sqlserver", "system", "tasks", "text", "threading", "timers", "transactions", | ||
"uiautomationclientsideproviders", "visualbasic", "visualc", "web", "win32", | ||
"windows", "workflow", "xaml", "xamlgeneratednamespace", "xml" | ||
}; | ||
|
||
protected override void Initialize(SonarAnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeActionInNonGenerated(c => | ||
{ | ||
if (IsDeclaredPublic(c.Node, c.SemanticModel)) | ||
{ | ||
ReportIfNameClashesWithFrameworkNamespace(GetIdentifier(c.Node), c); | ||
} | ||
}, | ||
SyntaxKind.ClassDeclaration, | ||
SyntaxKind.StructDeclaration, | ||
SyntaxKind.InterfaceDeclaration, | ||
SyntaxKind.EnumDeclaration, | ||
SyntaxKind.DelegateDeclaration); | ||
} | ||
|
||
private static SyntaxToken? GetIdentifier(SyntaxNode declaration) | ||
{ | ||
var baseTypeDeclaration = declaration as BaseTypeDeclarationSyntax; | ||
if (baseTypeDeclaration != null) | ||
{ | ||
return baseTypeDeclaration.Identifier; | ||
} | ||
|
||
var delegateDeclaration = declaration as DelegateDeclarationSyntax; | ||
if (delegateDeclaration != null) | ||
{ | ||
return delegateDeclaration.Identifier; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private static void ReportIfNameClashesWithFrameworkNamespace(SyntaxToken? identifier, | ||
SyntaxNodeAnalysisContext context) | ||
{ | ||
string typeName = identifier?.ValueText; | ||
var typeNameLocation = identifier?.GetLocation(); | ||
|
||
bool isNameClash = typeName != null && | ||
typeNameLocation != null && | ||
frameworkNamespaces.Contains(typeName.ToLowerInvariant()); | ||
|
||
if (isNameClash) | ||
{ | ||
context.ReportDiagnostic(Diagnostic.Create(rule, typeNameLocation, typeName)); | ||
} | ||
} | ||
|
||
private static bool IsDeclaredPublic(SyntaxNode declaration, SemanticModel semanticModel) | ||
{ | ||
return semanticModel.GetDeclaredSymbol(declaration)?.DeclaredAccessibility == Accessibility.Public; | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
sonaranalyzer-dotnet/src/SonarAnalyzer.Utilities/Rules.Description/S4041.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,25 @@ | ||
<p>When a type name matches the name of a publicly defined namespace, for instance one in the .NET framework class library, it leads to confusion and | ||
makes the library that much harder to use.</p> | ||
<p>This rule raises an issue when a name of a public type matches the name of a .NET Framework namespace, or a namespace of the project assembly, in a | ||
case-insensitive comparison.</p> | ||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
{ | ||
public class Text { // Noncompliant: Collides with System.Text | ||
} | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
using System; | ||
|
||
namespace MyLibrary | ||
{ | ||
public class MyText { | ||
} | ||
} | ||
</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
...er-dotnet/src/Tests/SonarAnalyzer.UnitTest/Rules/TypeNamesShouldNotMatchNamespacesTest.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 TypeNamesShouldNotMatchNamespacesTest | ||
{ | ||
[TestMethod] | ||
[TestCategory("Rule")] | ||
public void TypeNamesShouldNotMatchNamespaces() | ||
{ | ||
Verifier.VerifyAnalyzer(@"TestCases\TypeNamesShouldNotMatchNamespaces.cs", | ||
new TypeNamesShouldNotMatchNamespaces()); | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...er-dotnet/src/Tests/SonarAnalyzer.UnitTest/TestCases/TypeNamesShouldNotMatchNamespaces.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,88 @@ | ||
using System; | ||
|
||
namespace Tests.Diagnostics | ||
{ | ||
public class Web { } // Noncompliant {{Change the name of type 'Web' to be different from an existing framework namespace.}} | ||
// ^^^ | ||
public enum IO { x }; | ||
// ^^ | ||
|
||
public delegate void Runtime(); // Noncompliant | ||
// ^^^^^^^ | ||
|
||
public interface Linq { } // Noncompliant | ||
// ^^^^ | ||
|
||
public struct Xaml { } // Noncompliant | ||
// ^^^^ | ||
|
||
interface System { } // Compliant (it's not public) | ||
|
||
private interface Data { } // Compliant (it's not public) | ||
|
||
namespace Accessibility { } // Compliant (namespace are not checked) | ||
|
||
interface { } | ||
} | ||
|
||
namespace All_Variants | ||
{ | ||
public class Accessibility { } // Noncompliant | ||
public class Activities { } // Noncompliant | ||
public class Addin { } // Noncompliant | ||
public class Build { } // Noncompliant | ||
public class Codedom { } // Noncompliant | ||
public class Collections { } // Noncompliant | ||
public class Componentmodel { } // Noncompliant | ||
public class Configuration { } // Noncompliant | ||
public class Csharp { } // Noncompliant | ||
public class Custommarshalers { } // Noncompliant | ||
public class Data { } // Noncompliant | ||
public class Dataflow { } // Noncompliant | ||
public class Deployment { } // Noncompliant | ||
public class Device { } // Noncompliant | ||
public class Diagnostics { } // Noncompliant | ||
public class Directoryservices { } // Noncompliant | ||
public class Drawing { } // Noncompliant | ||
public class Dynamic { } // Noncompliant | ||
public class Enterpriseservices { } // Noncompliant | ||
public class Globalization { } // Noncompliant | ||
public class Identitymodel { } // Noncompliant | ||
public class Interopservices { } // Noncompliant | ||
public class Io { } // Noncompliant | ||
public class Jscript { } // Noncompliant | ||
public class Linq { } // Noncompliant | ||
public class Location { } // Noncompliant | ||
public class Management { } // Noncompliant | ||
public class Media { } // Noncompliant | ||
public class Messaging { } // Noncompliant | ||
public class Microsoft { } // Noncompliant | ||
public class Net { } // Noncompliant | ||
public class Numerics { } // Noncompliant | ||
public class Printing { } // Noncompliant | ||
public class Reflection { } // Noncompliant | ||
public class Resources { } // Noncompliant | ||
public class Runtime { } // Noncompliant | ||
public class Security { } // Noncompliant | ||
public class Server { } // Noncompliant | ||
public class Servicemodel { } // Noncompliant | ||
public class Serviceprocess { } // Noncompliant | ||
public class Speech { } // Noncompliant | ||
public class Sqlserver { } // Noncompliant | ||
public class System { } // Noncompliant | ||
public class Tasks { } // Noncompliant | ||
public class Text { } // Noncompliant | ||
public class Threading { } // Noncompliant | ||
public class Timers { } // Noncompliant | ||
public class Transactions { } // Noncompliant | ||
public class Uiautomationclientsideproviders { } // Noncompliant | ||
public class Visualbasic { } // Noncompliant | ||
public class Visualc { } // Noncompliant | ||
public class Web { } // Noncompliant | ||
public class Win32 { } // Noncompliant | ||
public class Windows { } // Noncompliant | ||
public class Workflow { } // Noncompliant | ||
public class Xaml { } // Noncompliant | ||
public class Xamlgeneratednamespace { } // Noncompliant | ||
public class Xml { } // Noncompliant | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is going to be boring but I would add a test case for every name we handle (avoid regression).