From beec897f404b35e5fb76610b715fe59c46aaad1d Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Fri, 9 Aug 2024 00:57:18 +0200 Subject: [PATCH 1/2] Modify S3993: Promote C# rule to Sonar-way --- analyzers/rspec/cs/S3993.html | 78 ++++++++++------------- analyzers/rspec/cs/Sonar_way_profile.json | 1 + 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/analyzers/rspec/cs/S3993.html b/analyzers/rspec/cs/S3993.html index dbb59261656..70f0eeed11f 100644 --- a/analyzers/rspec/cs/S3993.html +++ b/analyzers/rspec/cs/S3993.html @@ -1,55 +1,47 @@

Why is this an issue?

-

When defining custom attributes, System.AttributeUsageAttribute must be used to indicate where the attribute can be applied. This will -determine its valid locations in the code.

-

Noncompliant code example

-
-using System;
-
-namespace MyLibrary
+

When defining custom attributes, System.AttributeUsageAttribute must be used to indicate where the attribute can be applied. This will:

+ +

How to fix it

+

Code examples

+

Noncompliant code example

+
+public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing
 {
+    private string text;
 
-   public sealed class MyAttribute :Attribute // Noncompliant
-   {
-      string text;
+    public MyAttribute(string text)
+    {
+        this.text = text;
+    }
 
-      public MyAttribute(string myText)
-      {
-         text = myText;
-      }
-      public string Text
-      {
-         get
-         {
-            return text;
-         }
-      }
-   }
+    public string Text => text;
 }
 
-

Compliant solution

-
-using System;
-
-namespace MyLibrary
+

Compliant solution

+
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
+public sealed class MyAttribute : Attribute
 {
+    private string text;
 
-   [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)]
-   public sealed class MyAttribute :Attribute
-   {
-      string text;
+    public MyAttribute(string text)
+    {
+        this.text = text;
+    }
 
-      public MyAttribute(string myText)
-      {
-         text = myText;
-      }
-      public string Text
-      {
-         get
-         {
-            return text;
-         }
-      }
-   }
+    public string Text => text;
 }
 
+

Resources

+

Documentation

+ diff --git a/analyzers/rspec/cs/Sonar_way_profile.json b/analyzers/rspec/cs/Sonar_way_profile.json index 3450e95492b..a014e25d37c 100644 --- a/analyzers/rspec/cs/Sonar_way_profile.json +++ b/analyzers/rspec/cs/Sonar_way_profile.json @@ -227,6 +227,7 @@ "S3973", "S3981", "S3984", + "S3993", "S3998", "S4015", "S4019", From 1f8c161c9f108a8ed935530041e5a15f27b0c6bd Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Fri, 9 Aug 2024 11:28:31 +0200 Subject: [PATCH 2/2] Update RSPEC --- analyzers/rspec/cs/S3993.html | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/analyzers/rspec/cs/S3993.html b/analyzers/rspec/cs/S3993.html index 70f0eeed11f..7ed722a6f0d 100644 --- a/analyzers/rspec/cs/S3993.html +++ b/analyzers/rspec/cs/S3993.html @@ -1,5 +1,6 @@

Why is this an issue?

-

When defining custom attributes, System.AttributeUsageAttribute must be used to indicate where the attribute can be applied. This will:

+

When defining custom attributes, AttributeUsageAttribute +must be used to indicate where the attribute can be applied. This will:

  • indicate how the attribute can be used
  • prevent it from being used at invalid locations
  • @@ -42,6 +43,6 @@

    Documentation

    href="https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/creating-custom-attributes">Create custom attributes
  • Microsoft Learn - AttributeUsageAttribute class
  • -
  • Microsoft Learn - AttributeUsageAttribute class
  • +
  • Microsoft Learn - Attribute class