diff --git a/rules/S3466/csharp/rule.adoc b/rules/S3466/csharp/rule.adoc index c45be3615f..dd455d0de9 100644 --- a/rules/S3466/csharp/rule.adoc +++ b/rules/S3466/csharp/rule.adoc @@ -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[] diff --git a/rules/S3466/description.adoc b/rules/S3466/description.adoc index 481f3f501d..2c407de5f2 100644 --- a/rules/S3466/description.adoc +++ b/rules/S3466/description.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. \ No newline at end of file diff --git a/rules/S3466/highlighting.adoc b/rules/S3466/highlighting.adoc index 8d09d7c3e5..713c6fbce6 100644 --- a/rules/S3466/highlighting.adoc +++ b/rules/S3466/highlighting.adoc @@ -1,4 +1,4 @@ === Highlighting -``++base.Xxx(y)++`` +`base.Xxx(y)` diff --git a/rules/S3466/metadata.json b/rules/S3466/metadata.json index fcd1703dcb..6fa08a1313 100644 --- a/rules/S3466/metadata.json +++ b/rules/S3466/metadata.json @@ -24,5 +24,5 @@ "defaultQualityProfiles": [ "Sonar way" ], - "quickfix": "unknown" + "quickfix": "targeted" } diff --git a/rules/S3466/rspecator.adoc b/rules/S3466/rspecator.adoc new file mode 100644 index 0000000000..5915957457 --- /dev/null +++ b/rules/S3466/rspecator.adoc @@ -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[] \ No newline at end of file diff --git a/rules/S3466/rule.adoc b/rules/S3466/rule.adoc deleted file mode 100644 index c64365857f..0000000000 --- a/rules/S3466/rule.adoc +++ /dev/null @@ -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 - } -} ----- - diff --git a/rules/S3466/vbnet/rule.adoc b/rules/S3466/vbnet/rule.adoc index 68fe54ebfe..35c0e54439 100644 --- a/rules/S3466/vbnet/rule.adoc +++ b/rules/S3466/vbnet/rule.adoc @@ -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[]