Skip to content

Commit

Permalink
Review 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gregory-paidis-sonarsource committed Feb 12, 2024
1 parent 6fddc61 commit 3297ead
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ internal static class CollectionTracker
"TryAdd"
};

private static readonly HashSet<string> RemoveMethods = new()
{
nameof(ICollection<int>.Remove),
nameof(List<int>.RemoveAll),
nameof(List<int>.RemoveAt),
nameof(List<int>.RemoveRange),
nameof(HashSet<int>.ExceptWith),
nameof(HashSet<int>.IntersectWith),
nameof(HashSet<int>.RemoveWhere),
nameof(Queue<int>.Dequeue),
nameof(Stack<int>.Pop)
};

private static readonly HashSet<string> AddOrRemoveMethods = [.. AddMethods, .. RemoveMethods];

public static CollectionConstraint ObjectCreationConstraint(ProgramState state, IObjectCreationOperationWrapper operation)
{
if (operation.Type.IsAny(CollectionTypes))
Expand All @@ -81,10 +96,27 @@ public static CollectionConstraint ArrayCreationConstraint(IArrayCreationOperati
? CollectionConstraint.Empty
: CollectionConstraint.NotEmpty;

public static CollectionConstraint MethodReferenceConstraint(IMethodReferenceOperationWrapper operation) =>
operation.Instance is not null && AddMethods.Contains(operation.Method.Name)
? CollectionConstraint.NotEmpty
: null;
public static ProgramState LearnFrom(ProgramState state, IMethodReferenceOperationWrapper operation)
{
if (operation.Instance is not null
&& operation.Instance.Type.DerivesOrImplementsAny(CollectionTypes)
&& AddOrRemoveMethods.Contains(operation.Method.Name))
{
state = state[operation.Instance] is { } operationValue
? state.SetOperationValue(operation, operationValue.WithoutConstraint<CollectionConstraint>())
: state;

state = operation.Instance.TrackedSymbol(state) is { } symbol && state[symbol] is { } symbolValue
? state.SetSymbolValue(symbol, symbolValue.WithoutConstraint<CollectionConstraint>())
: state;

return state;
}
else
{
return state;
}
}

public static ProgramState LearnFrom(ProgramState state, IPropertyReferenceOperationWrapper operation, ISymbol instanceSymbol)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,5 @@ protected override IMethodReferenceOperationWrapper Convert(IOperation operation
IMethodReferenceOperationWrapper.FromOperation(operation);

protected override ProgramState Process(SymbolicContext context, IMethodReferenceOperationWrapper operation) =>
CollectionTracker.MethodReferenceConstraint(operation) is { } constraint
&& operation.Instance.TrackedSymbol(context.State) is { } symbol
? context.State.SetSymbolConstraint(symbol, constraint)
: context.State;
CollectionTracker.LearnFrom(context.State, operation);
}

0 comments on commit 3297ead

Please sign in to comment.