From 9336c31da8d76ed36248d86dc926be6bb2a060d7 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Fri, 9 Aug 2024 00:36:23 +0200 Subject: [PATCH 1/3] Promote S3993 to Sonar-way and convert to LayC format --- rules/S3993/csharp/rule.adoc | 55 ++++++++++++++++++++++++++++++- rules/S3993/metadata.json | 2 +- rules/S3993/rule.adoc | 63 ------------------------------------ 3 files changed, 55 insertions(+), 65 deletions(-) delete mode 100644 rules/S3993/rule.adoc diff --git a/rules/S3993/csharp/rule.adoc b/rules/S3993/csharp/rule.adoc index abd359d283e..f30991302ce 100644 --- a/rules/S3993/csharp/rule.adoc +++ b/rules/S3993/csharp/rule.adoc @@ -1,4 +1,57 @@ -include::../rule.adoc[] +== Why is this an issue? + +When defining custom attributes, System.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 + + +== How to fix it + +=== Code examples + +==== Noncompliant code example + +[source,csharp,diff-id=1,diff-type=noncompliant] +---- +public sealed class MyAttribute : Attribute // Noncompliant - AttributeUsage is missing +{ + private string text; + + public MyAttribute(string text) + { + this.text = text; + } + + public string Text => text; +} +---- + +==== Compliant solution + +[source,csharp,diff-id=1,diff-type=compliant] +---- +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)] +public sealed class MyAttribute : Attribute +{ + private string text; + + public MyAttribute(string text) + { + this.text = text; + } + + public string Text => text; +} +---- + +== Resources + +=== Documentation + +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/creating-custom-attributes[Create custom attributes] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute[AttributeUsageAttribute class] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute[AttributeUsageAttribute class] ifdef::env-github,rspecator-view[] diff --git a/rules/S3993/metadata.json b/rules/S3993/metadata.json index 97c321e23ba..d871d480ba3 100644 --- a/rules/S3993/metadata.json +++ b/rules/S3993/metadata.json @@ -28,7 +28,7 @@ "sqKey": "S3993", "scope": "All", "defaultQualityProfiles": [ - + "Sonar way" ], "quickfix": "unknown" } diff --git a/rules/S3993/rule.adoc b/rules/S3993/rule.adoc deleted file mode 100644 index 38d5ed25281..00000000000 --- a/rules/S3993/rule.adoc +++ /dev/null @@ -1,63 +0,0 @@ -== 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 - -[source,text] ----- -using System; - -namespace MyLibrary -{ - - public sealed class MyAttribute :Attribute // Noncompliant - { - string text; - - public MyAttribute(string myText) - { - text = myText; - } - public string Text - { - get - { - return text; - } - } - } -} ----- - - -=== Compliant solution - -[source,text] ----- -using System; - -namespace MyLibrary -{ - - [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Interface | AttributeTargets.Delegate)] - public sealed class MyAttribute :Attribute - { - string text; - - public MyAttribute(string myText) - { - text = myText; - } - public string Text - { - get - { - return text; - } - } - } -} ----- - From dbdfb78d42ee2b9e99dde4b5f957e6ebfd846268 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Fri, 9 Aug 2024 09:29:09 +0200 Subject: [PATCH 2/3] Add missing newline to S2674 --- rules/S2674/csharp/rule.adoc | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/S2674/csharp/rule.adoc b/rules/S2674/csharp/rule.adoc index 86ee6bf44e3..11806bf2307 100644 --- a/rules/S2674/csharp/rule.adoc +++ b/rules/S2674/csharp/rule.adoc @@ -5,6 +5,7 @@ Invoking a stream reading method without verifying the number of bytes read can Neglecting the returned length read can result in a bug that is difficult to reproduce. This rule raises an issue when the returned value is ignored for the following methods: + * https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.read[Stream.Read] * https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readasync[Stream.ReadAsync] * https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.readatleast[Stream.ReadAtLeast] From 1a32bdc595067bfee3f711bdac247d302f630986 Mon Sep 17 00:00:00 2001 From: Zsolt Kolbay Date: Fri, 9 Aug 2024 11:22:31 +0200 Subject: [PATCH 3/3] Address comments --- rules/S3993/csharp/rule.adoc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/rules/S3993/csharp/rule.adoc b/rules/S3993/csharp/rule.adoc index f30991302ce..4a681f0a24d 100644 --- a/rules/S3993/csharp/rule.adoc +++ b/rules/S3993/csharp/rule.adoc @@ -1,11 +1,10 @@ == 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, https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute[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 - == How to fix it === Code examples @@ -51,7 +50,7 @@ public sealed class MyAttribute : Attribute * Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/creating-custom-attributes[Create custom attributes] * Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute[AttributeUsageAttribute class] -* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute[AttributeUsageAttribute class] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.attribute[Attribute class] ifdef::env-github,rspecator-view[]