Compare commits
5 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
5100e4c27b | ||
![]() |
d1c7916e80 | ||
![]() |
ad05cfc0c9 | ||
![]() |
d6180e0f9e | ||
![]() |
0d0c2657cb |
23
rules/S7135/csharp/metadata.json
Normal file
23
rules/S7135/csharp/metadata.json
Normal 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"
|
||||
}
|
||||
}
|
37
rules/S7135/csharp/rule.adoc
Normal file
37
rules/S7135/csharp/rule.adoc
Normal 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
7
rules/S7135/message.adoc
Normal 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.
|
||||
|
2
rules/S7135/metadata.json
Normal file
2
rules/S7135/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
9
rules/S7135/rspecator.adoc
Normal file
9
rules/S7135/rspecator.adoc
Normal 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[]
|
Loading…
x
Reference in New Issue
Block a user