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

New rule S2166: Classes named like "Exception" should extend "Exception" or a subclass #6727

Merged
merged 21 commits into from Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
12c49d4
Initial scaffolding
zsolt-kolbay-sonarsource Feb 6, 2023
43d058f
Add test cases
zsolt-kolbay-sonarsource Feb 6, 2023
78dd3fc
Implement C# rule
zsolt-kolbay-sonarsource Feb 6, 2023
b2d8e63
Update RSPEC
zsolt-kolbay-sonarsource Feb 6, 2023
da7f219
Add VB test cases
zsolt-kolbay-sonarsource Feb 7, 2023
af479a1
Fix indentation
zsolt-kolbay-sonarsource Feb 7, 2023
a8e4e12
Add test case for generic classes
zsolt-kolbay-sonarsource Feb 7, 2023
5215eca
Different handling of static classes / modules
zsolt-kolbay-sonarsource Feb 7, 2023
6818afe
Add record class test case
zsolt-kolbay-sonarsource Feb 7, 2023
fb60d96
Add intentional finding test cases
zsolt-kolbay-sonarsource Feb 7, 2023
c49813e
Fix unit tests
zsolt-kolbay-sonarsource Feb 7, 2023
df85f92
Remove empty line
zsolt-kolbay-sonarsource Feb 8, 2023
8d95675
Introduce ClassAndModuleDeclarations to ISyntaxKindFacade
zsolt-kolbay-sonarsource Feb 8, 2023
3a755e4
Remove empty line
zsolt-kolbay-sonarsource Feb 8, 2023
540c20b
Update RSPEC and rule message
zsolt-kolbay-sonarsource Feb 9, 2023
8b1e18b
Update test cases
zsolt-kolbay-sonarsource Feb 9, 2023
5715d6d
Add explanation to test cases
zsolt-kolbay-sonarsource Feb 9, 2023
fa18201
Add compilation error test cases
zsolt-kolbay-sonarsource Feb 9, 2023
0ad6700
Add test case for System.Runtime.InteropServices._Exception
zsolt-kolbay-sonarsource Feb 9, 2023
044ce8b
Move up ClassDeclaration in CSharpSyntaxKindFacade
zsolt-kolbay-sonarsource Feb 9, 2023
8e0317d
Fix formatting
zsolt-kolbay-sonarsource Feb 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -18,6 +18,8 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using Microsoft.CodeAnalysis.CSharp;

namespace SonarAnalyzer.Helpers.Facade;

internal sealed class CSharpSyntaxKindFacade : ISyntaxKindFacade<SyntaxKind>
Expand All @@ -29,6 +31,7 @@ internal sealed class CSharpSyntaxKindFacade : ISyntaxKindFacade<SyntaxKind>
SyntaxKindEx.RecordClassDeclaration,
};
public SyntaxKind ClassDeclaration => SyntaxKind.ClassDeclaration;
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: now ClassDeclaration is between ClassAndRecordDeclaration and ClassAndModuleDeclarations, which for me makes it a bit harder to read

Could you move ClassDeclaration above ClassAndRecordDeclaration? (also in interface etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved it.

Copy link
Contributor

Choose a reason for hiding this comment

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

note: by "(also in interface etc)" I meant in all files where this applies

public SyntaxKind[] ClassAndModuleDeclarations => new[] { SyntaxKind.ClassDeclaration };
public SyntaxKind[] CommentTrivia => new[]
{
SyntaxKind.SingleLineCommentTrivia,
Expand Down
Expand Up @@ -24,6 +24,4 @@ namespace SonarAnalyzer.Rules.CSharp;
public sealed class ClassNamedException : ClassNamedExceptionBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => CSharpFacade.Instance;

protected override SyntaxKind[] AnalyzedSyntaxKinds => new[] { SyntaxKind.ClassDeclaration };
}
Expand Up @@ -26,6 +26,7 @@ public interface ISyntaxKindFacade<out TSyntaxKind>
abstract TSyntaxKind Attribute { get; }
abstract TSyntaxKind[] ClassAndRecordDeclaration { get; }
abstract TSyntaxKind ClassDeclaration { get; }
Copy link
Contributor

Choose a reason for hiding this comment

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

please move ClassDeclaration above ClassAndRecordDeclaration

abstract TSyntaxKind[] ClassAndModuleDeclarations { get; }
abstract TSyntaxKind[] CommentTrivia { get; }
abstract TSyntaxKind[] ComparisonKinds { get; }
abstract TSyntaxKind ConstructorDeclaration { get; }
Expand Down
Expand Up @@ -25,7 +25,6 @@ public abstract class ClassNamedExceptionBase<TSyntaxKind> : SonarDiagnosticAnal
{
private const string DiagnosticId = "S2166";

protected abstract TSyntaxKind[] AnalyzedSyntaxKinds { get; }
protected override string MessageFormat => "Rename this class to remove \"(e|E)xception\" or correct its inheritance.";
andrei-epure-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

protected ClassNamedExceptionBase() : base(DiagnosticId) { }
Expand All @@ -43,6 +42,6 @@ public abstract class ClassNamedExceptionBase<TSyntaxKind> : SonarDiagnosticAnal
c.ReportIssue(Diagnostic.Create(Rule, classIdentifier.GetLocation()));
}
},
AnalyzedSyntaxKinds);
Language.SyntaxKind.ClassAndModuleDeclarations);

}
Expand Up @@ -25,6 +25,11 @@ internal sealed class VisualBasicSyntaxKindFacade : ISyntaxKindFacade<SyntaxKind
public SyntaxKind Attribute => SyntaxKind.Attribute;
public SyntaxKind[] ClassAndRecordDeclaration => new[] { SyntaxKind.ClassBlock };
public SyntaxKind ClassDeclaration => SyntaxKind.ClassBlock;
Copy link
Contributor

Choose a reason for hiding this comment

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

please move ClassDeclaration above ClassAndRecordDeclaration

public SyntaxKind[] ClassAndModuleDeclarations => new[]
{
SyntaxKind.ClassBlock,
SyntaxKind.ModuleBlock
};
public SyntaxKind[] CommentTrivia => new[]
{
SyntaxKind.CommentTrivia,
Expand Down
Expand Up @@ -24,6 +24,4 @@ namespace SonarAnalyzer.Rules.VisualBasic;
public sealed class ClassNamedException : ClassNamedExceptionBase<SyntaxKind>
{
protected override ILanguageFacade<SyntaxKind> Language => VisualBasicFacade.Instance;

protected override SyntaxKind[] AnalyzedSyntaxKinds => new[] { SyntaxKind.ClassBlock, SyntaxKind.ModuleBlock };
}