Skip to content

Commit

Permalink
Modify S3993: Promote C# rule to Sonar-way (#9626)
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource authored Aug 9, 2024
1 parent 593fc71 commit 6ffc193
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
79 changes: 36 additions & 43 deletions analyzers/rspec/cs/S3993.html
Original file line number Diff line number Diff line change
@@ -1,55 +1,48 @@
<h2>Why is this an issue?</h2>
<p>When defining custom attributes, <code>System.AttributeUsageAttribute</code> must be used to indicate where the attribute can be applied. This will
determine its valid locations in the code.</p>
<h3>Noncompliant code example</h3>
<pre>
using System;

namespace MyLibrary
<p>When defining custom attributes, <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute">AttributeUsageAttribute</a>
must be used to indicate where the attribute can be applied. This will:</p>
<ul>
<li> indicate how the attribute can be used </li>
<li> prevent it from being used at invalid locations </li>
</ul>
<h2>How to fix it</h2>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
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 =&gt; text;
}
</pre>
<h3>Compliant solution</h3>
<pre>
using System;

namespace MyLibrary
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
[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 =&gt; text;
}
</pre>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> Microsoft Learn - <a
href="https://learn.microsoft.com/en-us/dotnet/csharp/advanced-topics/reflection-and-attributes/creating-custom-attributes">Create custom
attributes</a> </li>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attributeusageattribute">AttributeUsageAttribute class</a> </li>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/api/system.attribute">Attribute class</a> </li>
</ul>

1 change: 1 addition & 0 deletions analyzers/rspec/cs/Sonar_way_profile.json
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
"S3973",
"S3981",
"S3984",
"S3993",
"S3998",
"S4015",
"S4019",
Expand Down

0 comments on commit 6ffc193

Please sign in to comment.