Modify rule S2365: Migrate to new educational format (#2085)

This commit is contained in:
Gregory Paidis 2023-06-08 16:46:44 +02:00 committed by GitHub
parent 5a1eeabf0f
commit 1f7b894f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 74 additions and 86 deletions

View File

@ -1,3 +1,3 @@
{
}
"quickfix": "infeasible"
}

View File

@ -1,64 +1,43 @@
== Why is this an issue?
include::../why-dotnet.adoc[]
Most developers expect property access to be as efficient as field access. However, if a property returns a copy of an array or collection, it will be much slower than simple field access, contrary to the caller's likely expectations. Therefore, such properties should be refactored into methods so that callers are not surprised by the unexpectedly poor performance.
== How to fix it
=== Code examples
This rule detects calls to ``++ToList++``, ``++ToArray++`` and array ``++Clone++``.
==== Noncompliant code example
=== Noncompliant code example
[source,csharp]
[source,csharp,diff-id=1,diff-type=noncompliant]
----
private List<string> _foo = new List<string> { "a", "b", "c" };
public IEnumerable<string> Foo // Noncompliant
public IEnumerable<string> Foo // Noncompliant: expensive ToList call
{
get
{
return _foo.ToList();
return (string[])_foo.Clone();
}
}
private string[] _bar = new string[] { "a", "b", "c" };
public IEnumerable<string> Bar // Noncompliant
{
get
{
return (string[])_bar.Clone();
}
}
----
=== Compliant solution
==== Compliant solution
[source,csharp]
[source,csharp,diff-id=1,diff-type=compliant]
----
private List<string> _foo = new List<string> { "a", "b", "c" };
private string[] _bar = new string[] { "a", "b", "c" };
public IEnumerable<string> GetFoo()
{
return _foo.ToList();
}
public IEnumerable<string> GetBar()
{
return (string[])_bar.Clone();
return (string[])_foo.Clone();
}
----
ifdef::env-github,rspecator-view[]
== Resources
'''
== Implementation Specification
(visible only on this page)
=== Documentation
include::../message.adoc[]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties[Properties (C# Programming Guide)]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/fields[Fields (C# Programming Guide)]
* https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections[Collections (C#)]
* https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist[Enumerable.ToList]
* https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.toarray[Enumerable.ToArray]
* https://learn.microsoft.com/en-us/dotnet/api/system.array.clone[Array.Clone]
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -0,0 +1,16 @@
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[]

View File

@ -1,3 +1,3 @@
{
}
"quickfix": "infeasible"
}

View File

@ -1,55 +1,39 @@
== Why is this an issue?
include::../why-dotnet.adoc[]
Most developers expect property access to be as efficient as field access. However, if a property returns a copy of an array or collection, it will be much slower than a simple field access, contrary to the caller's likely expectations. Therefore, such properties should be refactored into methods so that callers are not surprised by unexpectedly poor performance.
== How to fix it
=== Code examples
=== Noncompliant code example
==== Noncompliant code example
[source,vbnet]
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
Module Module1
' Internal state
Dim array = {"apple", "banana", "orange", "pineapple", "strawberry"}
ReadOnly Property Foo() As String() ' Noncompliant
Get
Dim copy = array.Clone ' Expensive call
Return copy
End Get
End Property
End Module
----
=== Compliant solution
[source,vbnet]
----
Module Module1
' Internal state
Dim array = {"apple", "banana", "orange", "pineapple", "strawberry"}
Function GetFoo() As String() ' Compliant
Dim copy = array.Clone
Dim foo = {"a", "b", "c" }
Property Foo() As String() ' Noncompliant
Get
Dim copy = foo.Clone ' Expensive call
Return copy
End Function
End Module
End Get
End Property
----
==== Compliant solution
ifdef::env-github,rspecator-view[]
[source,vbnet,diff-id=1,diff-type=compliant]
----
Dim foo = {"a", "b", "c" }
Function GetFoo() As String()
Dim copy = foo.Clone
Return copy
End Function
----
'''
== Implementation Specification
(visible only on this page)
== Resources
include::../message.adoc[]
=== Documentation
include::../highlighting.adoc[]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/properties[Properties (Visual Basic)]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/objects-and-classes/#fields-and-properties[Fields and properties]
* https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/collections[Collections (Visual Basic)]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]
include::../rspecator.adoc[]

View File

@ -0,0 +1,9 @@
== Why is this an issue?
Most developers expect property access to be as efficient as field access. However, if a property returns a copy of an array or collection, it will be much slower than a simple field access, contrary to the caller's likely expectations. Therefore, such properties should be refactored into methods so that callers are not surprised by the unexpectedly poor performance.
This rule tracks calls to the following methods inside properties:
* https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist[Enumerable.ToList]
* https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.toarray[Enumerable.ToArray]
* https://learn.microsoft.com/en-us/dotnet/api/system.array.clone[Array.Clone]