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

CA2025 Do not pass IDisposable instances into unawaited tasks #7549

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor changes in helper methods
  • Loading branch information
steveberdy committed Jan 25, 2025
commit 3e9c4045efaadcae28eb406847ffebeb25ab3ebb
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ internal static class TaskOperationExtensions
public static bool IsAwaited(this IOperation op)
{
return op.GetAncestor<IAwaitOperation>(OperationKind.Await) is not null ||
op.Parent is IInvocationOperation { TargetMethod.Name: nameof(Task<>.Wait) } or
IPropertyReferenceOperation { Property.Name: nameof(Task<>.Result) };
op.Parent is IInvocationOperation { TargetMethod.Name: "Wait" } or
IPropertyReferenceOperation { Property.Name: "Result" };
}

public static bool IsTask(this IOperation op)
Expand All @@ -182,26 +182,20 @@ internal static class EnumerablExtensions
public static bool AnyWhere<T>(this IEnumerable<T> collection, Predicate<T> predicate, out IList<T> matches)
{
bool anyMatches = false;
IEnumerable<T> GetWhere(Action onAny)
IEnumerable<T> GetWhere()
{
Action triggerOnce = () =>
{
onAny();
triggerOnce = () => { };
};

foreach (var item in collection)
{
if (predicate(item))
{
triggerOnce();
anyMatches = true;
yield return item;
}
}
}

// ToList actually evaluates enumerable so anyMatches will be accurate
matches = [.. GetWhere(() => anyMatches = true)];
matches = [.. GetWhere()];
return anyMatches;
}
}
Expand Down