Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify S3993: Promote C# rule to Sonar-way #9626

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading