You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
Further, because some methods require that the argument remain unmodified during the execution, passing a collection to itself can result in undefined behavior.
Code Example
var myList = new List<int>();
myList.AddRange(myList); // Noncompliant
myList.Concat(myList); // Noncompliant
myList.Union(myList); // Noncompliant; always returns myList
myList.Except(myList); // Noncompliant; always empty
myList.Intersect(myList); // Noncompliant; always myList
myList.SequenceEqual(myList); // Noncompliant; always true
var set = new HashSet<int>();
set.UnionWith(set); // Noncompliant; no changes
set.ExceptWith(set); // Noncompliant; use Clear instead
Evangelink
changed the title
Update S1764: Detect same expression on Except() and Union() methods
Rule S2114: Detect same expression on collection methods
Jul 18, 2017
valhristov
changed the title
Rule S2114: Detect same expression on collection methods
Rule S2114: Collections should not be passed as arguments to their own methods
Aug 2, 2017
Passing a collection as an argument to the collection's own method is either an error - some other argument was intended - or simply nonsensical code.
Further, because some methods require that the argument remain unmodified during the execution, passing a collection to itself can result in undefined behavior.
Code Example
Implements RSPEC-2114
The text was updated successfully, but these errors were encountered: