Yassin Kammoun 65743cb622
Modify S3353: Migrate To LayC (#3302)
## 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)

---------

Co-authored-by: Zsolt Kolbay <121798625+zsolt-kolbay-sonarsource@users.noreply.github.com>
2023-10-17 17:44:58 +00:00

87 lines
1.3 KiB
Plaintext

include::../why.adoc[]
include::../how.adoc[]
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public bool Seek(int[] input)
{
var target = 32; // Noncompliant
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
public bool Seek(int[] input)
{
const int target = 32;
foreach (int i in input)
{
if (i == target)
{
return true;
}
}
return false;
}
----
==== Noncompliant code example
[source,csharp,diff-id=2,diff-type=noncompliant]
----
public class Sample
{
public void Method()
{
var context = $"{nameof(Sample)}.{nameof(Method)}"; // Noncompliant (C# 10 and above only)
}
}
----
==== Compliant solution
[source,csharp,diff-id=2,diff-type=compliant]
----
public class Sample
{
public void Method()
{
const string context = $"{nameof(Sample)}.{nameof(Method)}";
}
}
----
== Resources
=== Documentation
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/const[const]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add the 'const' modifier to 'xxx'.
endif::env-github,rspecator-view[]