Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule S2114: Collections should not be passed as arguments to their own methods #541

Closed
Evangelink opened this issue Jul 7, 2017 · 0 comments
Assignees
Labels
Type: New Feature This hasn't been here before.
Milestone

Comments

@Evangelink
Copy link
Contributor

Evangelink commented Jul 7, 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

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

Implements RSPEC-2114

@Evangelink Evangelink added Area: RSPEC Type: Improvement Making existing code better. labels Jul 7, 2017
@Evangelink Evangelink added this to the 6.3 milestone Jul 13, 2017
@Evangelink Evangelink self-assigned this Jul 14, 2017
@Evangelink 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
@Evangelink Evangelink added Type: New Feature This hasn't been here before. and removed Type: Improvement Making existing code better. labels Jul 19, 2017
@michalb-sonar michalb-sonar removed their assignment Jul 20, 2017
@valhristov 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: New Feature This hasn't been here before.
Projects
None yet
Development

No branches or pull requests

3 participants