How to add warning message before module is uninstalled
Before a module is uninstalled, Drupal invokes all the services implementing UninstallValidatorInterface and tagged with module_install.uninstall_validator. Their validate() method (see BookUninstallValidator::validate(), for example) returns an array of strings explaining why the module cannot be uninstalled, which are then shown in the user interface.

Notice that the validate() method receives the name of the module as parameter. While normally the module implementing the UninstallValidatorInterface service checks if itself can be uninstalled, a UninstallValidatorInterface service can avoid any module is uninstalled.

In Drupal 7, there isn't anything similar. The only way to achieve this is altering the page showing the list of the installed modules to show a message near a module.