From 6ffc193e4c0709cad5d007fb578e2c053d8f2ece Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:05:36 +0200 Subject: [PATCH] Modify S3993: Promote C# rule to Sonar-way (#9626) --- analyzers/rspec/cs/S3993.html | 79 +++++++++++------------ analyzers/rspec/cs/Sonar_way_profile.json | 1 + 2 files changed, 37 insertions(+), 43 deletions(-) diff --git a/analyzers/rspec/cs/S3993.html b/analyzers/rspec/cs/S3993.html index dbb59261656..7ed722a6f0d 100644 --- a/analyzers/rspec/cs/S3993.html +++ b/analyzers/rspec/cs/S3993.html @@ -1,55 +1,48 @@

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, 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",