Dispatch icon indicating copy to clipboard operation
Dispatch copied to clipboard

Add Lint rules warning against accessing the Dispatchers object or creating CoroutineScope without a DispatcherProvider

Open RBusarow opened this issue 6 years ago • 0 comments

Any access to the Dispatchers object breaks tests. This happens if we create a CoroutineScope without giving it a DispatcherProvider, or if we hard-code stuff like this:

class SomeClass {

  // We can still use `.dispatcherProvider` with this coroutineScope,
  // but it always returns a default implementation
  val coroutineScope = CoroutineScope(Job() + Dispatchers.Main)

  // This obviously accesses a hard-coded dispatcher
  fun foo() = coroutineScope.launch(Dispatchers.Default) { ... }
}

This is relatively easy to catch with Lint rules, but adding that rule to the main artifact will immediately create a warning for any code which has not been refactored.

Open Items

  • Should a Lint rule go into its own artifact, effectively allowing the user to opt in by adding the dependency? Or should it just be published as part of the core artifact?

RBusarow avatar Jan 20 '20 14:01 RBusarow