Modify rule S3466: LayC format (#2343)
## Review A dedicated reviewer checked the rule description successfully for: - [ ] logical errors and incorrect information - [ ] information gaps and missing content - [ ] text style and tone - [ ] PR summary and labels follow [the guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
This commit is contained in:
parent
b4e2cf709f
commit
816550ad28
@ -1,19 +1,69 @@
|
||||
include::../rule.adoc[]
|
||||
include::../description.adoc[]
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
== How to fix it
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
=== Code examples
|
||||
|
||||
include::../message.adoc[]
|
||||
==== Noncompliant code example
|
||||
|
||||
include::../highlighting.adoc[]
|
||||
[source,csharp,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
public class BaseClass
|
||||
{
|
||||
public virtual void MyMethod(int i = 1)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
public class DerivedClass : BaseClass
|
||||
{
|
||||
public override void MyMethod(int i = 1)
|
||||
{
|
||||
// ...
|
||||
base.MyMethod(); // Noncompliant: caller's value is ignored
|
||||
}
|
||||
|
||||
include::../comments-and-links.adoc[]
|
||||
static int Main(string[] args)
|
||||
{
|
||||
DerivedClass dc = new DerivedClass();
|
||||
dc.MyMethod(12); // prints 1
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
==== Compliant solution
|
||||
|
||||
[source,csharp,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
public class BaseClass
|
||||
{
|
||||
public virtual void MyMethod(int i = 1)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
public class DerivedClass : BaseClass
|
||||
{
|
||||
public override void MyMethod(int i = 1)
|
||||
{
|
||||
// ...
|
||||
base.MyMethod(i);
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
DerivedClass dc = new DerivedClass();
|
||||
dc.MyMethod(12); // prints 12
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
|
||||
=== Documentation
|
||||
|
||||
Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/named-and-optional-arguments#optional-arguments[Optional Arguments]
|
||||
|
||||
include::../rspecator.adoc[]
|
||||
|
@ -1 +1,3 @@
|
||||
Generally, writing the least code that will _readably_ do the job is a good thing, so omitting default parameter values seems to make sense. Unfortunately, when you omit them from the ``++base++`` call in an override, you're not actually getting the job done thoroughly, because you're ignoring the value the caller passed in. The result will likely not be what the caller expected.
|
||||
== Why is this an issue?
|
||||
|
||||
When optional parameter values are not passed to base method calls, the value passed in by the caller is ignored. This can cause the function to behave differently than expected, leading to errors and making the code difficult to debug.
|
@ -1,4 +1,4 @@
|
||||
=== Highlighting
|
||||
|
||||
``++base.Xxx(y)++``
|
||||
`base.Xxx(y)`
|
||||
|
||||
|
@ -24,5 +24,5 @@
|
||||
"defaultQualityProfiles": [
|
||||
"Sonar way"
|
||||
],
|
||||
"quickfix": "unknown"
|
||||
"quickfix": "targeted"
|
||||
}
|
||||
|
17
rules/S3466/rspecator.adoc
Normal file
17
rules/S3466/rspecator.adoc
Normal file
@ -0,0 +1,17 @@
|
||||
ifdef::env-github,rspecator-view,env-vscode[]
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
|
||||
include::message.adoc[]
|
||||
|
||||
include::highlighting.adoc[]
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
include::comments-and-links.adoc[]
|
||||
|
||||
endif::env-github,rspecator-view,env-vscode[]
|
@ -1,61 +0,0 @@
|
||||
== Why is this an issue?
|
||||
|
||||
include::description.adoc[]
|
||||
|
||||
=== Noncompliant code example
|
||||
|
||||
[source,text]
|
||||
----
|
||||
public class BaseClass
|
||||
{
|
||||
public virtual void MyMethod(int i = 1)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
public class DerivedClass : BaseClass
|
||||
{
|
||||
public override void MyMethod(int i = 1)
|
||||
{
|
||||
// ...
|
||||
base.MyMethod(); // Noncompliant; caller's value is ignored
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
DerivedClass dc = new DerivedClass();
|
||||
dc.MyMethod(12); // prints 1
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
|
||||
=== Compliant solution
|
||||
|
||||
[source,text]
|
||||
----
|
||||
public class BaseClass
|
||||
{
|
||||
public virtual void MyMethod(int i = 1)
|
||||
{
|
||||
Console.WriteLine(i);
|
||||
}
|
||||
}
|
||||
|
||||
public class DerivedClass : BaseClass
|
||||
{
|
||||
public override void MyMethod(int i = 1)
|
||||
{
|
||||
// ...
|
||||
base.MyMethod(i);
|
||||
}
|
||||
|
||||
static int Main(string[] args)
|
||||
{
|
||||
DerivedClass dc = new DerivedClass();
|
||||
dc.MyMethod(12); // prints 12
|
||||
}
|
||||
}
|
||||
----
|
||||
|
@ -1,10 +1,12 @@
|
||||
== Why is this an issue?
|
||||
|
||||
include::../description.adoc[]
|
||||
|
||||
=== Noncompliant code example
|
||||
== How to fix it
|
||||
|
||||
[source,vbnet]
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,vbnet,diff-id=1,diff-type=noncompliant]]
|
||||
----
|
||||
Public Class BaseClass
|
||||
Public Overridable Sub MyMethod(ByVal Optional i As Integer = 1)
|
||||
@ -17,7 +19,7 @@ Public Class DerivedClass
|
||||
|
||||
Public Overrides Sub MyMethod(ByVal Optional i As Integer = 1)
|
||||
' ...
|
||||
MyBase.MyMethod() ' Noncompliant; caller's value is ignored
|
||||
MyBase.MyMethod() ' Noncompliant: caller's value is ignored
|
||||
End Sub
|
||||
|
||||
Private Shared Function Main(ByVal args As String()) As Integer
|
||||
@ -27,9 +29,9 @@ Public Class DerivedClass
|
||||
End Class
|
||||
----
|
||||
|
||||
=== Compliant solution
|
||||
==== Compliant solution
|
||||
|
||||
[source,vbnet]
|
||||
[source,vbnet,diff-id=1,diff-type=compliant]]
|
||||
----
|
||||
Public Class BaseClass
|
||||
Public Overridable Sub MyMethod(ByVal Optional i As Integer = 1)
|
||||
@ -52,20 +54,10 @@ Public Class DerivedClass
|
||||
End Class
|
||||
----
|
||||
|
||||
ifdef::env-github,rspecator-view[]
|
||||
== Resources
|
||||
|
||||
'''
|
||||
== Implementation Specification
|
||||
(visible only on this page)
|
||||
=== Documentation
|
||||
|
||||
include::../message.adoc[]
|
||||
Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/optional-parameters[Optional Arguments (Visual Basic)]
|
||||
|
||||
include::../highlighting.adoc[]
|
||||
|
||||
'''
|
||||
== Comments And Links
|
||||
(visible only on this page)
|
||||
|
||||
include::../comments-and-links.adoc[]
|
||||
|
||||
endif::env-github,rspecator-view[]
|
||||
include::../rspecator.adoc[]
|
||||
|
Loading…
x
Reference in New Issue
Block a user