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

S2166: Add C# and VB.NET #1548

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
44 changes: 43 additions & 1 deletion rules/S2166/csharp/rule.adoc
@@ -1,5 +1,47 @@
include::../rule.adoc[]

== Noncompliant Code Example

[source,csharp]
----
public class FruitException // Noncompliant - this has nothing to do with Exception
{
private Fruit expected;
private string unusualCharacteristics;
private bool appropriateForCommercialExploitation;
// ...
}

public class CarException // Noncompliant - does not derive from any Exception-based class
{
public CarException(string message, Exception innerException)
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
// ...
}
}
----

== Compliant Solution

[source,csharp]
----
public class FruitSport // Compliant - class name does not end with 'Exception'
{
private Fruit expected;
private string unusualCharacteristics;
private bool appropriateForCommercialExploitation;
// ...
}

public class CarException: Exception // Compliant - correctly extends System.Exception
{
public CarException(string message, Exception innerException): base(message, innerException)
zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
{
// ...
}
}
----

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -13,4 +55,4 @@ include::../message.adoc[]
(visible only on this page)

include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]
40 changes: 39 additions & 1 deletion rules/S2166/vbnet/rule.adoc
@@ -1,5 +1,43 @@
include::../rule.adoc[]

== Noncompliant Code Example

[source,vbnet]
----
Public Class FruitException ' Noncompliant - this has nothing to do with Exception
Private expected As Fruit
Private unusualCharacteristics As String
Private appropriateForCommercialExploitation As Boolean
' ...
End Class

Public Class CarException ' Noncompliant - does not derive from any Exception-based class
Public Sub New(ByVal message As String, ByVal innerException As Exception)
' ...
End Sub
End Class
----

== Compliant Solution

[source,vbnet]
----
Public Class FruitSport ' Compliant - class name does not end with 'Exception'
Private expected As Fruit
Private unusualCharacteristics As String
Private appropriateForCommercialExploitation As Boolean
' ...
End Class

Public Class CarException Inherits Exception ' Compliant - correctly extends System.Exception
Public Sub New(ByVal message As String, ByVal innerException As Exception)
MyBase.New(message, innerException)
' ...
End Sub
End Class

----

ifdef::env-github,rspecator-view[]

'''
Expand All @@ -13,4 +51,4 @@ include::../message.adoc[]
(visible only on this page)

include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
endif::env-github,rspecator-view[]