Compare commits

...

5 Commits

Author SHA1 Message Date
Mary Georgiou
5100e4c27b nitpicks 2024-11-01 10:44:10 +01:00
Mary Georgiou
d1c7916e80 add links 2024-11-01 09:16:17 +01:00
Mary Georgiou
ad05cfc0c9 review 1 2024-11-01 09:10:50 +01:00
Mary Georgiou
d6180e0f9e new rule 2024-10-31 15:46:41 +01:00
mary-georgiou-sonarsource
0d0c2657cb Create rule S7135 2024-10-31 08:28:53 +00:00
5 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,23 @@
{
"title": "Conditional access should not be gratuitous",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "1min"
},
"tags": [
],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-7135",
"sqKey": "S7135",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
}
}

View File

@ -0,0 +1,37 @@
Using https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-[conditional access] (`?.`) in C# is a common practice to safely handle potentially null objects and avoid https://learn.microsoft.com/en-us/dotnet/api/system.nullreferenceexception[NullReferenceException]. However, if you are certain that an object is not null, you should consider not using conditional access and opt for the https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving[null-forgiving operator] (`!`) instead if needed under https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references[nullable context].
== Why is this an issue?
Not using conditional access when you know an object is not `null` makes the code intent clearer and easier to maintain. It communicates to others reading your code that nullability is not a concern in that particular context. Also, while the performance impact is minimal, conditional access does introduce a slight overhead as it introduces an extra check.
Finally, since the conditional access is actually an if statement, it introduces a condition that cannot be fully tested, impacting the code coverage.
== How to fix it
If the https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references[nullable context] is enabled and compiler raises a warning, you can replace the ? with !, otherwise you should just remove it.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
string s = "NotNull";
_ = s?.ToString(); // Noncompliant
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
string s = "NotNull";
_ = s.ToString(); // Compliant
----
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/member-access-operators#null-conditional-operators--and-[Conditional access]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-forgiving[null-forgiving operator]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references[nullable context]
include::../rspecator.adoc[]

7
rules/S7135/message.adoc Normal file
View File

@ -0,0 +1,7 @@
=== Message
xxx is not null, remove the conditional access.
Under nullable context:
xxx is not null, remove the conditional access and use `!` if needed.

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,9 @@
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]