Modify S3257: Migrate to LayC (#3267)

This commit is contained in:
Victor 2023-10-16 11:06:00 +02:00 committed by GitHub
parent e887caa47f
commit 95d63e2cec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 72 deletions

View File

@ -1,18 +0,0 @@
=== on 9 Jul 2015, 13:54:52 Ann Campbell wrote:
\[~tamas.vajk] please add a ``++Nullable<T>++`` example to the code samples
=== on 20 Jul 2015, 11:32:42 Tamas Vajk wrote:
\[~ann.campbell.2] Modified the samples a bit.
=== on 20 Jul 2015, 14:15:56 Ann Campbell wrote:
thanks [~tamas.vajk]
=== on 9 Dec 2015, 18:39:35 Ann Campbell wrote:
\[~tamas.vajk] because of the char limit in the R# mapping field, I truncated/expanded to map this rule to "RedundantExplicit.*"
Given the scope of this RSpec, I think it's an okay mapping, we just may come across other cases that we'll have to add in the future. But we would have done that anyway.
=== on 10 Dec 2015, 09:02:12 Tamas Vajk wrote:
\[~ann.campbell.2] LGTM

View File

@ -1,9 +1,10 @@
== Why is this an issue?
Unnecessarily verbose declarations and initializations make it harder to read the code, and should be simplified.
In C#, the type of a variable can often be inferred by the compiler. The use of the [var keyword](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables) allows you to avoid repeating the type name in a variable declaration and object instantiation because the declared type can often be inferred by the compiler.
Additionally, initializations providing the default value can also be omitted, helping to make the code more concise and readable.
Specifically the following should be omitted when they can be inferred:
Unnecessarily verbose declarations and initializations should be simplified. Specifically, the following should be omitted when they can be inferred:
* array element type
* array size
@ -13,10 +14,14 @@ Specifically the following should be omitted when they can be inferred:
* type of lambda expression parameters
* parameter declarations of anonymous methods when the parameters are not used.
== How to fix it
=== Noncompliant code example
Remove any unneeded code. C# provides many features designed to help you write more concise code.
[source,csharp]
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
var l = new List<int>() {}; // Noncompliant, {} can be removed
var o = new object() {}; // Noncompliant, {} can be removed
@ -25,7 +30,7 @@ var ints = new int[] {1, 2, 3}; // Noncompliant, int can be omitted
ints = new int[3] {1, 2, 3}; // Noncompliant, the size specification can be removed
int? i = new int?(5); // Noncompliant new int? could be omitted, it can be inferred from the declaration, and there's implicit conversion from T to T?
var j = new int?(5);
var j = new int?(5);
Func<int, int> f1 = (int i) => 1; //Noncompliant, can be simplified
@ -40,10 +45,9 @@ class Class
}
----
==== Compliant solution
=== Compliant solution
[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
var l = new List<int>();
var o = new object();
@ -67,21 +71,7 @@ class Class
}
----
== Resources
=== Documentation
ifdef::env-github,rspecator-view[]
'''
== 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[]
* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations[Declaration statements]

View File

@ -1,4 +0,0 @@
=== Highlighting
The bit to be removed/replaced

View File

@ -1,10 +1,18 @@
== Why is this an issue?
Unnecessarily verbose declarations and initializations make it harder to read the code, and should be simplified. Specifically, primitive (``++number++``, ``++string++``, ``++boolean++`` and others) types should be omitted from variable and parameter declaration when they can be easily inferred from the initialized or defaulted value.
TypeScript supports type inference, a mechanism that automatically infers the type of a variable based on its initial value. This means that if you initialize a variable with a particular value, TypeScript will assume that this variable should always hold that type of value.
=== Noncompliant code example
Unnecessarily verbose declarations and initializations make it harder to read the code and should be simplified. Therefore, type annotations should be omitted from variable and parameter declarations when they can be easily inferred from the initialized or defaulted value.
[source,javascript]
== How to fix it
Omit explicit type annotations in declarations whenever the type can be inferred from the context.
=== Code examples
==== Noncompliant code example
[source,javascript,diff-id=1,diff-type=noncompliant]
----
const n: number = 1; // Noncompliant, "number" can be omitted
@ -15,9 +23,9 @@ class Bar {
}
----
=== Compliant solution
==== Compliant solution
[source,javascript]
[source,javascript,diff-id=1,diff-type=compliant]
----
const n = 1;
@ -28,20 +36,7 @@ class Bar {
}
----
ifdef::env-github,rspecator-view[]
== Resources
=== Documentation
'''
== 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[]
* TypeScript - https://www.typescriptlang.org/docs/handbook/type-inference.html[Type Inference]

View File

@ -1,4 +0,0 @@
=== Message
Remove XXX; it is redundant.