{"record":{"id":"7f1e2d2c4d2b43c4","repo":"ReactiveX/RxJava","slug":"unsafecreate-single-should-be-upgraded","errorCode":null,"errorMessage":"unsafeCreate(Single) should be upgraded","messagePattern":"unsafeCreate\\(Single\\) should be upgraded","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/io/reactivex/rxjava4/core/Single.java","lineNumber":1383,"sourceCode":"     * <dt><b>Scheduler:</b></dt>\n     * <dd>{@code unsafeCreate} does not operate by default on a particular {@link Scheduler}.</dd>\n     * </dl>\n     * @param <T> the value type\n     * @param onSubscribe the function that is called with the subscribing {@code SingleObserver}\n     * @return the new {@code Single} instance\n     * @throws NullPointerException if {@code onSubscribe} is {@code null}\n     * @throws IllegalArgumentException if {@code source} is a subclass of {@code Single}; such\n     * instances don't need conversion and is possibly a port remnant from 1.x or one should use {@link #hide()}\n     * instead.\n     * @since 2.0\n     */\n    @CheckReturnValue\n    @NonNull\n    @SchedulerSupport(SchedulerSupport.NONE)\n    public static <@NonNull T> Single<T> unsafeCreate(@NonNull SingleSource<T> onSubscribe) {\n        Objects.requireNonNull(onSubscribe, \"onSubscribe is null\");\n        if (onSubscribe instanceof Single) {\n            throw new IllegalArgumentException(\"unsafeCreate(Single) should be upgraded\");\n        }\n        return RxJavaPlugins.onAssembly(new SingleFromUnsafeSource<>(onSubscribe));\n    }\n\n    /**\n     * Allows using and disposing a resource while running a {@link SingleSource} instance generated from\n     * that resource (similar to a try-with-resources).\n     * <p>\n     * <img width=\"640\" height=\"380\" src=\"https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/Single.using.v3.png\" alt=\"\">\n     * <dl>\n     * <dt><b>Scheduler:</b></dt>\n     * <dd>{@code using} does not operate by default on a particular {@link Scheduler}.</dd>\n     * </dl>\n     * @param <T> the value type of the {@code SingleSource} generated\n     * @param <U> the resource type\n     * @param resourceSupplier the {@link Supplier} called for each {@link SingleObserver} to generate a resource object\n     * @param sourceSupplier the function called with the returned resource\n     *                  object from {@code resourceSupplier} and should return a {@code SingleSource} instance","sourceCodeStart":1365,"sourceCodeEnd":1401,"githubUrl":"https://github.com/ReactiveX/RxJava/blob/a8ab5356143f37a8f1dd6d76c79191bec5fa343b/src/main/java/io/reactivex/rxjava4/core/Single.java#L1365-L1401","documentation":"Thrown by Single.unsafeCreate when the argument is already a Single instance. unsafeCreate exists to bridge foreign SingleSource implementations; wrapping a real Single is redundant, likely a port remnant, and the docs suggest using hide() if you genuinely need to obscure identity. The NPE check runs before this guard, so a null source throws NPE first.","triggerScenarios":"Calling Single.unsafeCreate(someSingle) where the runtime type is Single. Common when an upstream helper returns Single and the caller wraps it unconditionally.","commonSituations":"RxJava 1.x-to-3.x/4.x migration; generic adapters that wrap any source without checking its concrete type.","solutions":["Use the Single directly instead of wrapping: drop the unsafeCreate call.","If you need to hide identity, call source.hide().","Restrict the call site so unsafeCreate only receives non-Single SingleSource instances."],"exampleFix":"// before\nSingle<String> src = loadSingle();\nSingle<String> wrapped = Single.unsafeCreate(src);\n\n// after\nSingle<String> wrapped = src;\n// or, to hide identity:\nSingle<String> hidden = src.hide();","handlingStrategy":"type-guard","validationCode":"SingleSource<T> source = loadSource();\nif (source instanceof Single) {\n    // unsafeCreate would throw; use directly or hide()\n    return;\n}","typeGuard":"static <T> boolean needsUnsafeCreate(SingleSource<T> s) {\n    return s != null && !(s instanceof Single);\n}","tryCatchPattern":"try {\n    return Single.unsafeCreate(source);\n} catch (IllegalArgumentException e) {\n    return source instanceof Single ? (Single<T>) source : Single.error(e);\n}","preventionTips":["Use unsafeCreate only for foreign SingleSource implementations, not for Single.","Prefer hide() when you need to obscure Single identity.","Type-guard generic adapters that accept SingleSource."],"tags":["rxjava","single","unsafe-create","api-misuse","argument-validation"],"backgroundTag":null,"analyzedSha":"a8ab5356143f37a8f1dd6d76c79191bec5fa343b","analyzedAt":"2026-08-13T23:25:30.069Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}