-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify rule S1696: promote C# to SonarWay (#3693)
- Loading branch information
1 parent
e6ecfb4
commit cbe555b
Showing
4 changed files
with
52 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,51 @@ | ||
== Why is this an issue? | ||
|
||
``++NullReferenceException++`` should be avoided, not caught. Any situation in which ``++NullReferenceException++`` is explicitly caught can easily be converted to a ``++null++`` test, and any behavior being carried out in the catch block can easily be moved to the "is null" branch of the conditional. | ||
Catching `NullReferenceException` is generally considered a bad practice because it can hide bugs in your code. Instead of catching this exception, you should aim to prevent it. This makes your code more robust and easier to understand. | ||
In addition, constantly catching and handling `NullReferenceException` can lead to performance issues. Exceptions are expensive in terms of system resources, so they should be used cautiously and only for exceptional conditions, not for regular control flow. | ||
|
||
=== Noncompliant code example | ||
== How to fix it | ||
|
||
[source,csharp] | ||
Instead of catching NullReferenceException, it's better to prevent it from happening in the first place. You can do this by using null checks or null conditional operators (`?.`) before accessing members of an object. | ||
|
||
=== Code examples | ||
|
||
==== Noncompliant code example | ||
|
||
[source,csharp,diff-id=1,diff-type=noncompliant] | ||
---- | ||
public int GetLengthPlusTwo(string str) | ||
{ | ||
int length = 2; | ||
try | ||
try | ||
{ | ||
length += str.Length; | ||
return str.Length + 2; | ||
} | ||
catch (NullReferenceException e) | ||
catch (NullReferenceException e) | ||
{ | ||
log.info("argument was null"); | ||
return 2; | ||
} | ||
return length; | ||
} | ||
---- | ||
|
||
=== Compliant solution | ||
==== Compliant solution | ||
|
||
[source,csharp] | ||
[source,csharp,diff-id=1,diff-type=compliant] | ||
---- | ||
public int GetLengthPlusTwo(string str) | ||
public int GetLengthPlusTwo(string str) | ||
{ | ||
int length = 2; | ||
if (str != null) | ||
if (str is null) | ||
{ | ||
length += str.Length; | ||
return 2; | ||
} | ||
else | ||
{ | ||
log.info("argument was null"); | ||
} | ||
return length; | ||
return str.Length + 2; | ||
} | ||
---- | ||
|
||
== Resources | ||
|
||
* CWE - https://cwe.mitre.org/data/definitions/395[CWE-395 - Use of NullPointerException Catch to Detect NULL Pointer Dereference] | ||
|
||
ifdef::env-github,rspecator-view[] | ||
=== Documentation | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Do not catch NullReferenceException; test for null instead. | ||
|
||
|
||
=== Highlighting | ||
|
||
throw new NullReferenceException() | ||
|
||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
include::../comments-and-links.adoc[] | ||
* CWE - https://cwe.mitre.org/data/definitions/395[CWE-395 - Use of NullPointerException Catch to Detect NULL Pointer Dereference] | ||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException class] | ||
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-[Null-conditional operators ?. and ?[]] | ||
|
||
endif::env-github,rspecator-view[] | ||
include::../rspecator.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,8 @@ | |
"CWE": [ | ||
395 | ||
] | ||
} | ||
}, | ||
"defaultQualityProfiles": [ | ||
|
||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
] | ||
}, | ||
"defaultQualityProfiles": [ | ||
|
||
"Sonar way" | ||
], | ||
"quickfix": "unknown" | ||
"quickfix": "infeasible" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
ifdef::env-github,rspecator-view[] | ||
|
||
''' | ||
== Implementation Specification | ||
(visible only on this page) | ||
|
||
=== Message | ||
|
||
Do not catch NullReferenceException; test for null instead. | ||
|
||
=== Highlighting | ||
|
||
throw new NullReferenceException() | ||
|
||
''' | ||
== Comments And Links | ||
(visible only on this page) | ||
|
||
include::comments-and-links.adoc[] | ||
|
||
endif::env-github,rspecator-view[] |