{"record":{"id":"03d30caffe091289","repo":"dotnet/reactive","slug":"predicate","errorCode":null,"errorMessage":"predicate","messagePattern":"predicate","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"AsyncRx.NET/System.Reactive.Async/Linq/Operators/Last.cs","lineNumber":30,"sourceCode":"        public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate) => Last(source, predicate);\n        public static IAsyncObservable<TSource> LastAsync<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate) => Last(source, predicate);\n\n        public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n\n            return Create(\n                source,\n                (source, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer)));\n        }\n\n        public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source, Func<TSource, bool> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer, predicate)));\n        }\n\n        public static IAsyncObservable<TSource> Last<TSource>(this IAsyncObservable<TSource> source, Func<TSource, ValueTask<bool>> predicate)\n        {\n            if (source == null)\n                throw new ArgumentNullException(nameof(source));\n            if (predicate == null)\n                throw new ArgumentNullException(nameof(predicate));\n\n            return CreateAsyncObservable<TSource>.From(\n                source,\n                predicate,\n                static (source, predicate, observer) => source.SubscribeSafeAsync(AsyncObserver.Last(observer, predicate)));","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/AsyncRx.NET/System.Reactive.Async/Linq/Operators/Last.cs#L12-L48","documentation":"This Last overload takes a predicate to filter which elements qualify as candidates for the last element; a null predicate is rejected because the filter would be undefined. The guard runs in Last, which the LastAsync overloads forward to. Fix: pass a non-null Func<TSource, bool>, or use the parameterless Last(source).","triggerScenarios":"Calling source.Last(null) or passing a predicate variable that was never assigned (e.g. an optional filter parameter defaulting to null).","commonSituations":"Optional filter delegates passed as null from configuration-driven code; refactoring removed a lambda but left the call; an API boundary forwards a caller-supplied null delegate.","solutions":["Pass a valid predicate lambda, e.g. x => true if no filtering is intended.","Null-check or default the predicate before calling Last: predicate ?? (_ => true).","Use the parameterless Last overload when no filtering is needed."],"exampleFix":"// before\nvar last = source.Last(filter); // filter is null\n// after\nvar last = filter == null ? source.Last() : source.Last(filter);","handlingStrategy":"validation","validationCode":"if (predicate is null) predicate = static _ => true;\nvar last = await source.Last(predicate);","typeGuard":"static bool HasPredicate<T>(Func<T, bool>? p) => p is not null;","tryCatchPattern":"try { var last = await source.Last(predicate); }\ncatch (ArgumentNullException ex) when (ex.ParamName == \"predicate\") { last = await source.Last(); }","preventionTips":["Default optional filter delegates to _ => true, never to null.","Use the parameterless Last overload when no filtering is intended.","Check delegate-typed parameters at public API boundaries before forwarding them."],"tags":["argument-null","predicate","linq-operator"],"backgroundTag":"null-argument","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}