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

Update S2325: Promote to SonarWay #9614

Closed
Closed
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
58 changes: 38 additions & 20 deletions analyzers/rspec/cs/S2325.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
<h2>Why is this an issue?</h2>
<p>Methods and properties that don’t access instance data can be <code>static</code> to prevent any misunderstanding about the contract of the
method.</p>
<h3>Noncompliant code example</h3>
<pre>
<p>Methods and properties that don’t access instance data should be marked as <code>static</code> for the following reasons:</p>
<ul>
<li> Clarity and Intent: Marking a method/property as static makes it clear that the method does not depend on instance data and can be called
without creating an instance of the class. This improves the readability of the code by clearly conveying the member’s intended use. </li>
<li> Performance: Instance methods/properties in C# require an instance of the class to be called. This means that even if the it doesn’t use any
instance data, the runtime still needs to pass a reference to the instance during the call. For static methods and properties, this overhead is
avoided, leading to slightly better performance. </li>
<li> Memory Usage: Since instance methods implicitly carry a reference to the instance (the caller object), they can potentially prevent the garbage
collector from collecting the instance whem it is not otherwise referenced. Static members do not carry this overhead, potentially reducing memory
usage. </li>
<li> Testing: Static members can be easier to test since they do not require an instance of the class. This can simplify unit testing and reduce the
amount of boilerplate code needed to set up tests. </li>
</ul>
<h3>Exceptions</h3>
<p>Methods with the following names are excluded because they can’t be made <code>static</code>:</p>
<ul>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication.authenticaterequest">Application_AuthenticateRequest</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication.beginrequest">Application_BeginRequest</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178473(v=vs.100)">Application_End</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.httpapplication.endrequest">Application_EndRequest</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/previous-versions/aspnet/24395wz3(v=vs.100)">Application_Error</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178473(v=vs.100)">Application_Init</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178473(v=vs.100)">Application_Start</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.sessionstate.sessionstatemodule.end">Session_End</a> </li>
<li> <a href="https://learn.microsoft.com/en-us/dotnet/api/system.web.sessionstate.sessionstatemodule.start">Session_Start</a> </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 class Utilities
{
public int MagicNum // Noncompliant
public int MagicNum // Noncompliant - only returns a constant value
{
get
{
Expand All @@ -14,7 +40,7 @@ <h3>Noncompliant code example</h3>
}

private static string magicWord = "please";
public string MagicWord // Noncompliant
public string MagicWord // Noncompliant - only accesses a static field
{
get
{
Expand All @@ -26,14 +52,14 @@ <h3>Noncompliant code example</h3>
}
}

public int Sum(int a, int b) // Noncompliant
public int Sum(int a, int b) // Noncompliant - doesn't access instance data, only the method parameters
{
return a + b;
}
}
</pre>
<h3>Compliant solution</h3>
<pre>
<h4>Compliant solution</h4>
<pre data-diff-id="1" data-diff-type="compliant">
public class Utilities
{
public static int MagicNum
Expand Down Expand Up @@ -63,17 +89,9 @@ <h3>Compliant solution</h3>
}
}
</pre>
<h3>Exceptions</h3>
<p>Methods with the following names are excluded because they can’t be made <code>static</code>:</p>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li> Application_AuthenticateRequest </li>
<li> Application_BeginRequest </li>
<li> Application_End </li>
<li> Application_EndRequest </li>
<li> Application_Error </li>
<li> Application_Init </li>
<li> Application_Start </li>
<li> Session_End </li>
<li> Session_Start </li>
<li> Microsoft Learn - <a href="https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/static">The static modifier</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 @@ -93,6 +93,7 @@
"S2291",
"S2292",
"S2306",
"S2325",
"S2326",
"S2328",
"S2342",
Expand Down
Loading
Loading