-
Notifications
You must be signed in to change notification settings - Fork 232
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
Fix S4158 FN: Learn Empty on Clear #7861
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,27 +412,45 @@ public void Methods_Set_NotEmpty(bool condition) | |
public void Method_Set_Empty(List<int> list, HashSet<int> set, Queue<int> queue, Stack<int> stack, ObservableCollection<int> obs, Dictionary<int, int> dict) | ||
{ | ||
list.Clear(); // Compliant | ||
list.Clear(); // FN | ||
list.Clear(); // Noncompliant | ||
|
||
set.Clear(); // Compliant | ||
set.Clear(); // FN | ||
set.Clear(); // Noncompliant | ||
|
||
queue.Clear(); // Compliant | ||
queue.Clear(); // FN | ||
queue.Clear(); // Noncompliant | ||
|
||
stack.Clear(); // Compliant | ||
stack.Clear(); // FN | ||
stack.Clear(); // Noncompliant | ||
|
||
obs.Clear(); // Compliant | ||
obs.Clear(); // FN | ||
obs.Clear(); // Noncompliant | ||
|
||
dict.Clear(); // Compliant | ||
dict.Clear(); // FN | ||
dict.Clear(); // Noncompliant | ||
|
||
var empty = new List<int>(); | ||
list.Add(5); | ||
list.Intersect(empty); // Compliant | ||
list.RemoveAll(x => true); // Compliant | ||
list.Clear(); // FN | ||
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved
Hide resolved
|
||
list.Add(5); | ||
list.RemoveAll(x => x == 1); // Compliant | ||
list.Clear(); // Compliant | ||
|
||
set.Add(5); | ||
set.RemoveWhere(x => true); // Compliant | ||
set.Clear(); // FN | ||
set.Add(5); | ||
set.RemoveWhere(x => x == 1); // Compliant | ||
set.Clear(); // Compliant | ||
|
||
var empty = new List<int>(); | ||
set.Add(5); | ||
set.IntersectWith(empty); // Compliant | ||
set.Clear(); // FN | ||
var notEmpty = new List<int> { 1 }; | ||
set.Add(5); | ||
set.IntersectWith(notEmpty); // Compliant | ||
set.Clear(); // Compliant | ||
} | ||
} | ||
|
||
|
@@ -531,6 +549,8 @@ public void LearnConditions_Size(bool condition, List<int> arg) | |
var empty = new List<int>(); | ||
var notEmpty = new List<int>() { 1, 2, 3 }; | ||
|
||
// the tests below are messy for as long as we unlearn CollectionConstraints on empty.Count() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand this comment :/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
if ((isNull ?? empty).Count == 0) | ||
{ | ||
empty.Clear(); // Noncompliant | ||
|
@@ -588,11 +608,11 @@ public void LearnConditions_Size(bool condition, List<int> arg) | |
|
||
if (notEmpty.Count(x => condition) == 0) | ||
{ | ||
empty.Clear(); // FN | ||
empty.Clear(); // Noncompliant | ||
} | ||
else | ||
{ | ||
empty.Clear(); // FN | ||
empty.Clear(); // Noncompliant | ||
} | ||
|
||
if (Enumerable.Count(empty) == 0) | ||
|
@@ -736,7 +756,7 @@ public void AddPassedAsParameter() | |
Action<int> add = list.Add; | ||
list.Clear(); // FN | ||
add(5); | ||
list.Clear(); // Compliant, but will break when we learn Empty from Clear() | ||
list.Clear(); // Noncompliant FP | ||
|
||
list = new List<int>(); | ||
Action clear = list.Clear; // We don't raise here to avoid FPs | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick:
Maybe you can retrieve once the
invocation.TargetMethod.Name
and pass it to all the calls as an argument.