From f3480df4d9610b9308b063f4509e6cb60ed9b3a1 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com> Date: Mon, 6 Feb 2023 14:10:38 +0100 Subject: [PATCH] S2166: Add C# and VB.NET (#1548) * Add rule description for C# * Add rule description for VB.NET --------- Co-authored-by: Andrei Epure <38876598+andrei-epure-sonarsource@users.noreply.github.com> --- rules/S2166/csharp/rule.adoc | 44 +++++++++++++++++++++++++++++++++++- rules/S2166/vbnet/rule.adoc | 40 +++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 2 deletions(-) diff --git a/rules/S2166/csharp/rule.adoc b/rules/S2166/csharp/rule.adoc index d4f3cbe4fa7..b13b7dd0fdf 100644 --- a/rules/S2166/csharp/rule.adoc +++ b/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 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[] ''' @@ -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[] \ No newline at end of file diff --git a/rules/S2166/vbnet/rule.adoc b/rules/S2166/vbnet/rule.adoc index d4f3cbe4fa7..df7992c1fd7 100644 --- a/rules/S2166/vbnet/rule.adoc +++ b/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 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[] ''' @@ -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[] \ No newline at end of file