Skip to content

Commit

Permalink
S2166: Add C# and VB.NET (#1548)
Browse files Browse the repository at this point in the history
* Add rule description for C#

* Add rule description for VB.NET

---------

Co-authored-by: Andrei Epure <38876598+andrei-epure-sonarsource@users.noreply.github.com>
  • Loading branch information
1 parent ff343a5 commit f3480df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
44 changes: 43 additions & 1 deletion rules/S2166/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -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 inner)
{
// ...
}
}
----

== 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 inner): base(message, inner)
{
// ...
}
}
----

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
Original file line number Diff line number Diff line change
@@ -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 inner 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 inner As Exception)
MyBase.New(message, inner)
' ...
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[]

0 comments on commit f3480df

Please sign in to comment.