Search code examples
javascalaintellij-idea

I want to suppress deprecated API usage


Environment: IntelliJ IDEA Ultimate 2022.2 Build 222.3345.118

I have overridden the method with @Deprecated annotation. I want to suppress deprecated API usage on certain methods. But the Option+Enter action has only useless suggestions.

enter image description here

We tried to suppress it by means of SupressWornings and //noinspection ScalaDeprecation. I would like to disable Inspection as a last resort because there are some warnings that I want to suppress and others that I do not want to.


Solution

  • In Scala you can use the @nowarn annotation to this end. You can see a very nice rundown of how to use both this annotation locally or other options on the overall project on this blog post by Lukas Rytz.

    In your case, I believe you can use the following annotation:

    @nowarn("cat=deprecation&origin=your\.package\.YourDeprecatedClass")
    

    You can see an example of how I used this on this commit.

    As you can see, the annotation can be applied even to individual expression via type ascription, but you can also apply it to methods. In general, I like to keep my @nowarns as specific and fine-scoped as possible. You can also tell the compiler to warn you when a @nowarn is not doing anything, so that you don't have to keep unused annotations around the code.

    If you have deprecated APIs that you need to use (e.g. even though they are deprecated, you still need to test them) and the usages are all over the place, you might want to evaluate the option of configuring the warning suppression as a compiler option with something like

    -Wconf:cat=deprecation&origin=your\.package\.YourDeprecatedClass
    

    passed to the compiler. Here is an example of this as well (we use Bazel instead of SBT but I'm fairly sure that setting up SBT for this is quite simple).

    For any other information, the blog post I linked above is an excellent source.