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 #4130

Merged
merged 3 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
1 change: 1 addition & 0 deletions rules/S2674/csharp/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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:

zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
* 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]
Expand Down
54 changes: 53 additions & 1 deletion rules/S3993/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,56 @@
include::../rule.adoc[]
== Why is this an issue?

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

zsolt-kolbay-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
== 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.attribute[Attribute class]

ifdef::env-github,rspecator-view[]

Expand Down
2 changes: 1 addition & 1 deletion rules/S3993/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"sqKey": "S3993",
"scope": "All",
"defaultQualityProfiles": [

"Sonar way"
],
"quickfix": "unknown"
}
63 changes: 0 additions & 63 deletions rules/S3993/rule.adoc

This file was deleted.