Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8344159

Add lint warnings for unnecessary warning suppression

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • tools
    • None
    • generic
    • generic

      Add the ability for the compiler to detect and report unnecessary warning suppressions.

      An "unnecessary warning suppression" is when one of the following happens:

      * There is a @SuppressWarnings("foo") annotation, but if it hadn't been there, no foo warning would have been generated within the annotation's scope
      * The compiler is passed -Xlint:-foo, but if it hadn't been, no foo warning would have been generated during the entire compilation

      Motivation

      @SuppressWarnings and -Xlint:-foo are blunt instruments. The latter is maximally blunt: it covers the entire compilation. The former is somewhat blunt, especially when the warning occurs at a specific statement other than a variable declaration and so the annotation has to annotate and cover the entire containing method.

      In practice @SuppressWarnings and -Xlint:-foo are also very sticky: once they get added to a source file or a build process, they are rarely removed, because that would require an audit to determine if the original problem is now resolved (or the compiler behavior has changed), which is tedious.

      Sometimes @SuppressWarnings annotations are never needed in the first place: they're added to the code proactively as the code is written because the developer thinks they might be needed. In this situation, the compiler provides the same feedback either way (i.e. none), so this type of mistake is almost never caught.

      As code evolves over time, newly added bugs that warnings are designed to catch can escape detection if they happen to appear within the scope of a @SuppressWarnings or -Xlint:-foo flag. That problem can't be solved completely, but it can be minimized by ensuring that all @SuppressWarnings annotations and -Xlint:-foo flags that do exist are actually serving some purpose.

      More generally, there is the natural and healthy need to "declutter", and also the "peace of mind" factor: We want to know we're doing everything we reasonably can to prevent bugs... and since the compiler is the thing that generates the warnings in the first place, shouldn't it also be able to detect and report when a warning is being unnecessarily suppressed?

      Caveats

      There are real-world concerns with adding something like this. Lots of people build with -Xlint:all. We don't want to constrict the compiler so tightly that it becomes more frustrating than helpful for people trying to build software in the real world. Warning behavior can differ not only across JDK versions but also across operating systems, so we don't want to force over-complexification of builds.

      There is a balance to strike; the functionality should be easy to disable.

      Unrecognized warnings in @SuppressWarnings annotations should be ignored per JLS recommendation.

            acobbs Archie Cobbs
            acobbs Archie Cobbs
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: